mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 04:08:15 +08:00
Merge 14fd129b90213b0260772169108d6526120c4ee2 into c3d1092b3b48addf6f9cd00fe274ec3bd14650eb
This commit is contained in:
commit
23daf8eca9
@ -73,6 +73,9 @@ 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[any]any
|
Keys map[any]any
|
||||||
|
|
||||||
@ -982,6 +985,8 @@ func (c *Context) IsWebsocket() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) requestHeader(key string) string {
|
func (c *Context) requestHeader(key string) string {
|
||||||
|
c.hmu.RLock()
|
||||||
|
defer c.hmu.RUnlock()
|
||||||
return c.Request.Header.Get(key)
|
return c.Request.Header.Get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1011,6 +1016,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()
|
||||||
|
defer c.hmu.Unlock()
|
||||||
if value == "" {
|
if value == "" {
|
||||||
c.Writer.Header().Del(key)
|
c.Writer.Header().Del(key)
|
||||||
return
|
return
|
||||||
|
@ -3423,6 +3423,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) {
|
func TestGetMapFromFormData(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
name string
|
name string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user