mirror of
https://github.com/gin-gonic/gin.git
synced 2025-05-20 10:55:08 +08:00
make headers thread safe
add reader lock
This commit is contained in:
parent
e46bd52185
commit
6ecb89ad63
@ -21,6 +21,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/sse"
|
||||
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/gin-gonic/gin/render"
|
||||
)
|
||||
@ -71,6 +72,9 @@ type Context struct {
|
||||
// This mutex protects Keys map.
|
||||
mu sync.RWMutex
|
||||
|
||||
// This mutex protects headers map
|
||||
hmu sync.RWMutex
|
||||
|
||||
// Keys is a key/value pair exclusively for the context of each request.
|
||||
Keys map[string]any
|
||||
|
||||
@ -954,6 +958,8 @@ func (c *Context) IsWebsocket() bool {
|
||||
}
|
||||
|
||||
func (c *Context) requestHeader(key string) string {
|
||||
c.hmu.Lock()
|
||||
defer c.hmu.Unlock()
|
||||
return c.Request.Header.Get(key)
|
||||
}
|
||||
|
||||
@ -983,6 +989,8 @@ func (c *Context) Status(code int) {
|
||||
// It writes a header in the response.
|
||||
// If value == "", this method removes the header `c.Writer.Header().Del(key)`
|
||||
func (c *Context) Header(key, value string) {
|
||||
c.hmu.Lock()
|
||||
defer c.hmu.Unlock()
|
||||
if value == "" {
|
||||
c.Writer.Header().Del(key)
|
||||
return
|
||||
|
Loading…
x
Reference in New Issue
Block a user