Skip to content
Myr

chore(performance): Change *sync.RWMutex to sync.RWMutex (#2351)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Bo-Yi Wu · committed by GitHub · parent 4427ca4 · Browse files · 2 files+13 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−16 removed

  • other:+13 added−16 removed

2 files+13 added−16 removed

modified:context.go+12 added-15 removed
52 unmodified lines
5354 engine *Engine5556 // This mutex protect Keys map57 KeysMutex *sync.RWMutex57 mu sync.RWMutex5859 // Keys is a key/value pair exclusively for the context of each request.60 Keys map[string]interface{}61
23 unmodified lines
85 c.Writer = &c.writermem86 c.Params = c.Params[0:0]87 c.handlers = nil88 c.index = -189 c.KeysMutex = &sync.RWMutex{}8990 c.fullPath = ""91 c.Keys = nil92 c.Errors = c.Errors[0:0]93 c.Accepted = nil
3 unmodified lines
9798// Copy returns a copy of the current context that can be safely used outside the request's scope.99// This has to be used when the context has to be passed to a goroutine.100func (c *Context) Copy() *Context {101 var cp = *c101 cp := Context{102 writermem: c.writermem,103 Request: c.Request,104 Params: c.Params,105 engine: c.engine,106 }107 cp.writermem.ResponseWriter = nil108 cp.Writer = &cp.writermem109 cp.index = abortIndex110 cp.handlers = nil
121 unmodified lines
232233// Set is used to store a new key/value pair exclusively for this context.234// It also lazy initializes c.Keys if it was not used previously.235func (c *Context) Set(key string, value interface{}) {231 if c.KeysMutex == nil {232 c.KeysMutex = &sync.RWMutex{}233 }234235 c.KeysMutex.Lock()236 c.mu.Lock()237 if c.Keys == nil {238 c.Keys = make(map[string]interface{})239 }240241 c.Keys[key] = value241 c.KeysMutex.Unlock()242 c.mu.Unlock()243}244245// Get returns the value for the given key, ie: (value, true).246// If the value does not exists it returns (nil, false)247func (c *Context) Get(key string) (value interface{}, exists bool) {247 if c.KeysMutex == nil {248 c.KeysMutex = &sync.RWMutex{}249 }250251 c.KeysMutex.RLock()248 c.mu.RLock()249 value, exists = c.Keys[key]253 c.KeysMutex.RUnlock()250 c.mu.RUnlock()251 return252}253254// MustGet returns the value for the given key if it exists, otherwise it panics.
852 unmodified lines
modified:gin.go+1 added-1 removed
160 unmodified lines
161 return engine162}163164func (engine *Engine) allocateContext() *Context {165 return &Context{engine: engine, KeysMutex: &sync.RWMutex{}}165 return &Context{engine: engine}166}167168// Delims sets template left and right delims and returns a Engine instance.169func (engine *Engine) Delims(left, right string) *Engine {
328 unmodified lines