Compare commits

..

3 Commits

Author SHA1 Message Date
Saksham Arya a9ea3b4b00
Merge 4857f7c66e into e46bd52185 2024-11-20 18:54:01 +05:30
Saksham Arya 4857f7c66e add test case
fix lint
2024-11-20 18:53:35 +05:30
Saksham Arya 98499b58e0 make headers thread safe
add reader lock
2024-11-20 16:18:58 +05:30
2 changed files with 8 additions and 9 deletions

View File

@ -21,6 +21,7 @@ import (
"time" "time"
"github.com/gin-contrib/sse" "github.com/gin-contrib/sse"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin/render" "github.com/gin-gonic/gin/render"
) )
@ -71,9 +72,6 @@ type Context struct {
// This mutex protects Keys map. // This mutex protects Keys map.
mu sync.RWMutex 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 is a key/value pair exclusively for the context of each request.
Keys map[string]any Keys map[string]any
@ -986,8 +984,8 @@ func (c *Context) Status(code int) {
// It writes a header in the response. // It writes a header in the response.
// If value == "", this method removes the header `c.Writer.Header().Del(key)` // If value == "", this method removes the header `c.Writer.Header().Del(key)`
func (c *Context) Header(key, value string) { func (c *Context) Header(key, value string) {
c.hmu.Lock() c.mu.Lock()
defer c.hmu.Unlock() defer c.mu.Unlock()
if value == "" { if value == "" {
c.Writer.Header().Del(key) c.Writer.Header().Del(key)
return return
@ -997,8 +995,8 @@ func (c *Context) Header(key, value string) {
// GetHeader returns value from request headers. // GetHeader returns value from request headers.
func (c *Context) GetHeader(key string) string { func (c *Context) GetHeader(key string) string {
c.hmu.RLock() c.mu.RLock()
defer c.hmu.RUnlock() defer c.mu.RUnlock()
return c.requestHeader(key) return c.requestHeader(key)
} }

View File

@ -27,11 +27,12 @@ import (
"time" "time"
"github.com/gin-contrib/sse" "github.com/gin-contrib/sse"
"github.com/gin-gonic/gin/binding"
testdata "github.com/gin-gonic/gin/testdata/protoexample"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"github.com/gin-gonic/gin/binding"
testdata "github.com/gin-gonic/gin/testdata/protoexample"
) )
var _ context.Context = (*Context)(nil) var _ context.Context = (*Context)(nil)