Compare commits

...

7 Commits

Author SHA1 Message Date
Saksham Arya
7fe24a2c5e
Merge 3a07160a182dca294cf264746bbcf4e12073faaa into 19b877fa50cbbb9282763099fb177a1e5cc5c850 2025-12-05 11:18:25 +08:00
Bo-Yi Wu
3a07160a18
Merge branch 'master' into thread-safe-headers 2025-12-05 10:34:33 +08:00
Saksham Arya
14fd129b90 fix lint 2025-08-08 16:05:19 +05:30
Saksham Arya
312b621908 refactor test case 2025-08-08 16:05:19 +05:30
Saksham Arya
cdaceb18a5 comment 2025-08-08 16:05:18 +05:30
Saksham Arya
c7c51af0ec add test case
fix lint
2025-08-08 16:05:18 +05:30
Saksham Arya
ef93bbeb9b make headers thread safe
add reader lock
2025-08-08 16:05:18 +05:30
2 changed files with 49 additions and 0 deletions

View File

@ -82,6 +82,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[any]any
@ -1031,6 +1034,8 @@ func (c *Context) IsWebsocket() bool {
}
func (c *Context) requestHeader(key string) string {
c.hmu.RLock()
defer c.hmu.RUnlock()
return c.Request.Header.Get(key)
}
@ -1060,6 +1065,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

View File

@ -3462,6 +3462,48 @@ func TestContextSetCookieData(t *testing.T) {
})
}
func TestParallelHeaderAccess(t *testing.T) {
t.Parallel()
const iterations = 1000
const goroutines = 8
testCases := []struct {
name string
writerCount int
readerCount int
}{
{"parallel_write_only", goroutines, 0},
{"parallel_write_and_read", goroutines / 2, goroutines / 2},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
wg := sync.WaitGroup{}
for i := 0; i < tc.writerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
c.Header("key", "value")
}
}()
}
for i := 0; i < tc.readerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
_ = c.GetHeader("key")
}
}()
}
wg.Wait()
})
}
}
func TestGetMapFromFormData(t *testing.T) {
testCases := []struct {
name string