Skip to content

Commit

Permalink
make headers thread safe
Browse files Browse the repository at this point in the history
add reader lock
  • Loading branch information
saksham-arya-z committed Dec 24, 2024
1 parent e46bd52 commit 6ecb89a
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/gin-contrib/sse"

"github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin/render"
)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6ecb89a

Please sign in to comment.