Skip to content

Commit

Permalink
Introduce an interface RWLock to implement lock free in production mo…
Browse files Browse the repository at this point in the history
…de (#94)

* This PR introduce an interface RWLock to implement lock free in production mode

* Don't expose the interface
  • Loading branch information
lunny committed May 27, 2021
1 parent 03136a8 commit fb5b5ba
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion render.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,31 @@ type HTMLOptions struct {
Funcs template.FuncMap
}

// rwLock represents an interface for sync.RWMutex
type rwLock interface {
Lock()
Unlock()
RLock()
RUnlock()
}

var (
_ rwLock = &sync.RWMutex{}
_ rwLock = emptyLock{}
)

// emptyLock is a dummy RWLock implementation
type emptyLock struct{}

func (emptyLock) Lock() {}
func (emptyLock) Unlock() {}
func (emptyLock) RLock() {}
func (emptyLock) RUnlock() {}

// Render is a service that provides functions for easily writing JSON, XML,
// binary data, and HTML templates out to a HTTP Response.
type Render struct {
lock sync.RWMutex
lock rwLock

// Customize Secure with an Options struct.
opt Options
Expand All @@ -141,6 +162,11 @@ func New(options ...Options) *Render {
r := Render{
opt: o,
}
if o.IsDevelopment {
r.lock = &sync.RWMutex{}
} else {
r.lock = emptyLock{}
}

r.prepareOptions()
r.CompileTemplates()
Expand Down

0 comments on commit fb5b5ba

Please sign in to comment.