Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(context): make set and get header thread safe #4101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,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 @@ -983,6 +986,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 All @@ -992,6 +997,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)
}

Expand Down
24 changes: 22 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestSaveUploadedFileWithPermission(t *testing.T) {
require.NoError(t, err)
mw.Close()
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request, _ = http.NewRequest(http.MethodPost, "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
require.NoError(t, err)
Expand All @@ -187,7 +187,7 @@ func TestSaveUploadedFileWithPermissionFailed(t *testing.T) {
require.NoError(t, err)
mw.Close()
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request, _ = http.NewRequest(http.MethodPost, "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
require.NoError(t, err)
Expand Down Expand Up @@ -3123,3 +3123,23 @@ func TestContextNext(t *testing.T) {
assert.True(t, exists)
assert.Equal(t, "value3", value)
}

func TestParallelHeaderWrite(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 1000; i++ {
c.Header("key", "value")
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 1000; i++ {
c.Header("key", "value")
}
}()
wg.Wait()
}