Skip to content

Commit

Permalink
fix race
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Sep 1, 2023
1 parent 6d9b1fc commit 7cd1892
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
#run: env GOARCH=386 go test -v -coverprofile='coverage.out' -covermode=count ./...

- name: Test-amd64
run: env GOARCH=amd64 go test -v -coverprofile='coverage.out' -covermode=count ./...
run: env GOARCH=amd64 go test -race -v -coverprofile='coverage.out' -covermode=count ./...


- name: Upload Coverage report
Expand Down
10 changes: 6 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Conn struct {
once sync.Once

fr fixedreader.FixedReader
fw fixedwriter.FixedWriter
// fw fixedwriter.FixedWriter
bp bytespool.BytesPool // 实验某些特性加的字段

delayWrite
Expand Down Expand Up @@ -389,7 +389,8 @@ func (c *Conn) WriteMessage(op Opcode, writeBuf []byte) (err error) {
maskValue = rand.Uint32()
}

return frame.WriteFrame(&c.fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
var fw fixedwriter.FixedWriter
return frame.WriteFrame(&fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
}

func (c *Conn) SetWriteDeadline(t time.Time) error {
Expand Down Expand Up @@ -460,16 +461,17 @@ func (c *Conn) writeFragment(op Opcode, writeBuf []byte, maxFragment int /*单
maskValue = rand.Uint32()
}

var fw fixedwriter.FixedWriter
for len(writeBuf) > 0 {
if len(writeBuf) > maxFragment {
if err := frame.WriteFrame(&c.fw, c.c, writeBuf[:maxFragment], false, rsv1, c.client, op, maskValue); err != nil {
if err := frame.WriteFrame(&fw, c.c, writeBuf[:maxFragment], false, rsv1, c.client, op, maskValue); err != nil {
return err
}
writeBuf = writeBuf[maxFragment:]
op = Continuation
continue
}
return frame.WriteFrame(&c.fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
return frame.WriteFrame(&fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
}
return nil
}
Expand Down
12 changes: 8 additions & 4 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"
"time"

"github.com/antlabs/wsutil/fixedwriter"
"github.com/antlabs/wsutil/frame"
"github.com/antlabs/wsutil/opcode"
)
Expand Down Expand Up @@ -225,7 +226,8 @@ func Test_ReadMessage(t *testing.T) {

// err = con.WriteMessage(Binary, []byte("hello"))
maskValue := rand.Uint32()
err = frame.WriteFrame(&con.fw, con.c, []byte("hello"), true, true, con.client, Binary, maskValue)
var fw fixedwriter.FixedWriter
err = frame.WriteFrame(&fw, con.c, []byte("hello"), true, true, con.client, Binary, maskValue)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -276,7 +278,8 @@ func Test_ReadMessage(t *testing.T) {

// err = con.WriteMessage(Binary, []byte("hello"))
maskValue := rand.Uint32()
err = frame.WriteFrame(&con.fw, con.c, []byte("hello"), true, true, con.client, Ping, maskValue)
var fw fixedwriter.FixedWriter
err = frame.WriteFrame(&fw, con.c, []byte("hello"), true, true, con.client, Ping, maskValue)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -397,12 +400,13 @@ func TestFragmentFrame(t *testing.T) {
// con.writeFragment(Ping, []byte("hello"), 1)

maskValue := rand.Uint32()
err = frame.WriteFrame(&con.fw, con.c, []byte("h"), false, false, con.client, Text, maskValue)
var fw fixedwriter.FixedWriter
err = frame.WriteFrame(&fw, con.c, []byte("h"), false, false, con.client, Text, maskValue)
if err != nil {
t.Error(err)
}
maskValue = rand.Uint32()
err = frame.WriteFrame(&con.fw, con.c, []byte{}, true, false, con.client, Text, maskValue)
err = frame.WriteFrame(&fw, con.c, []byte{}, true, false, con.client, Text, maskValue)
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/antlabs/wsutil/bufio2"
"github.com/antlabs/wsutil/bytespool"
"github.com/antlabs/wsutil/fixedreader"
"github.com/antlabs/wsutil/rsp"
)

type UpgradeServer struct {
Expand Down Expand Up @@ -73,7 +72,8 @@ func upgradeInner(w http.ResponseWriter, r *http.Request, conf *Config) (c *Conn
if !conf.disableBufioClearHack {
bufio2.ClearReadWriter(rw)
}
rsp.ClearRsp(w)
// TODO
// rsp.ClearRsp(w)
rw = nil
} else {
var rw *bufio.ReadWriter
Expand Down

0 comments on commit 7cd1892

Please sign in to comment.