mirror of
https://github.com/gin-gonic/gin.git
synced 2025-04-26 03:26:56 +08:00
Merge 46b1c57c33966e75f27a7c5adaac47f29e85e0de into 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d
This commit is contained in:
commit
de019156b7
@ -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,6 +72,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[string]any
|
Keys map[string]any
|
||||||
|
|
||||||
@ -954,6 +958,8 @@ func (c *Context) IsWebsocket() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) requestHeader(key string) string {
|
func (c *Context) requestHeader(key string) string {
|
||||||
|
c.hmu.Lock()
|
||||||
|
defer c.hmu.Unlock()
|
||||||
return c.Request.Header.Get(key)
|
return c.Request.Header.Get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -983,6 +989,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
|
||||||
|
@ -3123,3 +3123,23 @@ func TestContextNext(t *testing.T) {
|
|||||||
assert.True(t, exists)
|
assert.True(t, exists)
|
||||||
assert.Equal(t, "value3", value)
|
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()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user