Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
fix the concurrent race of reuse of fasthttp resp body
Browse files Browse the repository at this point in the history
Signed-off-by: allen.wq <allen.wq@alipay.com>
  • Loading branch information
wangforthinker committed Mar 13, 2020
1 parent 35d3d7e commit 5da96fe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/httputils/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ func do(url string, headers map[string]string, timeout time.Duration, rsf reques
}

statusCode = resp.StatusCode()
body = resp.Body()
data := resp.Body()
body = make([]byte, len(data))
copy(body, data)
return
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/httputils/http_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"math/rand"
"net"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -215,6 +216,22 @@ func (s *HTTPUtilTestSuite) TestGetRangeSE(c *check.C) {
}
}

func (s *HTTPUtilTestSuite) TestConcurrencyPostJson(c *check.C) {
wg := &sync.WaitGroup{}
wg.Add(100)

for i := 0; i < 100; i++ {
go func(x, y int) {
defer wg.Done()
code, body, e := PostJSON("http://"+s.host, req(x, y), 1*time.Second)
time.Sleep(20 * time.Millisecond)
checkOk(c, code, body, e, x+y)
}(i, i)
}

wg.Wait()
}

// ----------------------------------------------------------------------------
// helper functions and structures

Expand Down

0 comments on commit 5da96fe

Please sign in to comment.