Skip to content

Commit 608a7f8

Browse files
authored
fix race (#10)
* fix race * up * fix
1 parent 6d9b1fc commit 608a7f8

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

.github/workflows/go.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ jobs:
3838
run: env GOARCH=386 go test -test.run=Test_Retry_sleep -v
3939
#run: env GOARCH=386 go test -v -coverprofile='coverage.out' -covermode=count ./...
4040

41+
- name: Test-Race
42+
run: env GOARCH=amd64 go test -v -race ./...
43+
4144
- name: Test-amd64
4245
run: env GOARCH=amd64 go test -v -coverprofile='coverage.out' -covermode=count ./...
4346

44-
4547
- name: Upload Coverage report
4648
uses: codecov/codecov-action@v1
4749
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*swp
2+
/coverage.out
23
/cover.cov
34
autobahn-testsuite
45
autobahn-client-testsuite-darwin-arm64

conn.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type Conn struct {
6161
once sync.Once
6262

6363
fr fixedreader.FixedReader
64-
fw fixedwriter.FixedWriter
64+
// fw fixedwriter.FixedWriter
6565
bp bytespool.BytesPool // 实验某些特性加的字段
6666

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

392-
return frame.WriteFrame(&c.fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
392+
var fw fixedwriter.FixedWriter
393+
return frame.WriteFrame(&fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
393394
}
394395

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

464+
var fw fixedwriter.FixedWriter
463465
for len(writeBuf) > 0 {
464466
if len(writeBuf) > maxFragment {
465-
if err := frame.WriteFrame(&c.fw, c.c, writeBuf[:maxFragment], false, rsv1, c.client, op, maskValue); err != nil {
467+
if err := frame.WriteFrame(&fw, c.c, writeBuf[:maxFragment], false, rsv1, c.client, op, maskValue); err != nil {
466468
return err
467469
}
468470
writeBuf = writeBuf[maxFragment:]
469471
op = Continuation
470472
continue
471473
}
472-
return frame.WriteFrame(&c.fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
474+
return frame.WriteFrame(&fw, c.c, writeBuf, true, rsv1, c.client, op, maskValue)
473475
}
474476
return nil
475477
}
@@ -478,12 +480,12 @@ func (c *Conn) Close() (err error) {
478480
c.once.Do(func() {
479481
c.bp.Free()
480482
err = c.c.Close()
483+
c.delayMu.Lock()
481484
if c.delayTimeout != nil {
482485
c.delayTimeout.Stop()
483-
c.delayMu.Lock()
484486
c.delayBuf = nil
485-
c.delayMu.Unlock()
486487
}
488+
c.delayMu.Unlock()
487489
atomic.StoreInt32(&c.closed, 1)
488490
})
489491
return
@@ -508,7 +510,10 @@ func (c *Conn) writerDelayBufInner() (err error) {
508510
return
509511
}
510512

511-
// 延迟写消息, 对流量密集型的场景有用 或者开启tcp delay, WithClientTCPDelay WithServerTCPDelay
513+
// 对于流量场景这个版本推荐开启tcp delay 方法:WithClientTCPDelay() WithServerTCPDelay()
514+
515+
// 该函数目前是研究性质的尝试
516+
// 延迟写消息, 对流量密集型的场景有用 或者开启tcp delay,
512517
// 1. 如果缓存的消息超过了多少条数
513518
// 2. 如果缓存的消费超过了多久的时间
514519
// 3. TODO: 最大缓存多少字节

conn_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"testing"
2626
"time"
2727

28+
"github.com/antlabs/wsutil/fixedwriter"
2829
"github.com/antlabs/wsutil/frame"
2930
"github.com/antlabs/wsutil/opcode"
3031
)
@@ -225,7 +226,8 @@ func Test_ReadMessage(t *testing.T) {
225226

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

277279
// err = con.WriteMessage(Binary, []byte("hello"))
278280
maskValue := rand.Uint32()
279-
err = frame.WriteFrame(&con.fw, con.c, []byte("hello"), true, true, con.client, Ping, maskValue)
281+
var fw fixedwriter.FixedWriter
282+
err = frame.WriteFrame(&fw, con.c, []byte("hello"), true, true, con.client, Ping, maskValue)
280283
if err != nil {
281284
t.Error(err)
282285
}
@@ -397,12 +400,13 @@ func TestFragmentFrame(t *testing.T) {
397400
// con.writeFragment(Ping, []byte("hello"), 1)
398401

399402
maskValue := rand.Uint32()
400-
err = frame.WriteFrame(&con.fw, con.c, []byte("h"), false, false, con.client, Text, maskValue)
403+
var fw fixedwriter.FixedWriter
404+
err = frame.WriteFrame(&fw, con.c, []byte("h"), false, false, con.client, Text, maskValue)
401405
if err != nil {
402406
t.Error(err)
403407
}
404408
maskValue = rand.Uint32()
405-
err = frame.WriteFrame(&con.fw, con.c, []byte{}, true, false, con.client, Text, maskValue)
409+
err = frame.WriteFrame(&fw, con.c, []byte{}, true, false, con.client, Text, maskValue)
406410
if err != nil {
407411
t.Error(err)
408412
}

upgrade.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/antlabs/wsutil/bufio2"
2525
"github.com/antlabs/wsutil/bytespool"
2626
"github.com/antlabs/wsutil/fixedreader"
27-
"github.com/antlabs/wsutil/rsp"
2827
)
2928

3029
type UpgradeServer struct {
@@ -73,7 +72,8 @@ func upgradeInner(w http.ResponseWriter, r *http.Request, conf *Config) (c *Conn
7372
if !conf.disableBufioClearHack {
7473
bufio2.ClearReadWriter(rw)
7574
}
76-
rsp.ClearRsp(w)
75+
// TODO
76+
// rsp.ClearRsp(w)
7777
rw = nil
7878
} else {
7979
var rw *bufio.ReadWriter

0 commit comments

Comments
 (0)