mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-15 13:02:14 +08:00
Fix for : fatal error: concurrent map read and map write
This commit is contained in:
parent
63f05bfdc3
commit
470747bd8c
@ -12,6 +12,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
@ -46,6 +47,7 @@ type Context struct {
|
||||
|
||||
engine *Engine
|
||||
Keys map[string]interface{}
|
||||
KeysLocker sync.RWMutex
|
||||
Errors errorMsgs
|
||||
Accepted []string
|
||||
}
|
||||
@ -160,14 +162,18 @@ func (c *Context) Set(key string, value interface{}) {
|
||||
if c.Keys == nil {
|
||||
c.Keys = make(map[string]interface{})
|
||||
}
|
||||
c.KeysLocker.Lock()
|
||||
c.Keys[key] = value
|
||||
c.KeysLocker.Unlock()
|
||||
}
|
||||
|
||||
// Get returns the value for the given key, ie: (value, true).
|
||||
// If the value does not exists it returns (nil, false)
|
||||
func (c *Context) Get(key string) (value interface{}, exists bool) {
|
||||
if c.Keys != nil {
|
||||
c.KeysLocker.RLock()
|
||||
value, exists = c.Keys[key]
|
||||
c.KeysLocker.RUnlock()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user