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 Nov 20, 2024
1 parent e46bd52 commit 98499b5
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 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 @@ -983,6 +984,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.mu.Lock()
defer c.mu.Unlock()
if value == "" {
c.Writer.Header().Del(key)
return
Expand All @@ -992,6 +995,8 @@ func (c *Context) Header(key, value string) {

// GetHeader returns value from request headers.
func (c *Context) GetHeader(key string) string {
c.mu.RLock()
defer c.mu.RUnlock()
return c.requestHeader(key)
}

Expand Down

0 comments on commit 98499b5

Please sign in to comment.