From 02ace40c1366e8d6e6f25d75e859e9c3a6d91778 Mon Sep 17 00:00:00 2001 From: Saksham Arya Date: Wed, 20 Nov 2024 16:02:55 +0530 Subject: [PATCH] make headers thread safe add reader lock --- context.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/context.go b/context.go index c724daf344..2406857a96 100644 --- a/context.go +++ b/context.go @@ -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 @@ -983,6 +987,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 @@ -992,6 +998,8 @@ func (c *Context) Header(key, value string) { // GetHeader returns value from request headers. func (c *Context) GetHeader(key string) string { + c.hmu.RLock() + defer c.hmu.RUnlock() return c.requestHeader(key) }