Skip to content

Commit

Permalink
bufio模式可以修改buf大小
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Aug 5, 2023
1 parent 7e89161 commit 086e69a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
22 changes: 22 additions & 0 deletions common_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func WithServerIgnorePong() ServerOption {
// 7.
// 设置几倍payload的缓冲区
// 只有解析方式是窗口的时候才有效
// 如果为1.0就是1024 + 14, 如果是2.0就是2048 + 14
func WithServerWindowsMultipleTimesPayloadSize(mt float32) ServerOption {
return func(o *ConnOption) {
if mt < 1.0 {
Expand Down Expand Up @@ -178,3 +179,24 @@ func WithClientDisableBufioClearHack() ClientOption {
o.disableBufioClearHack = true
}
}

// 12 配置多倍payload缓冲区, 1.是1024 2。是2048
// 为何不让用户自己配置呢,可以和底层的buffer池结合起来,/1024就知道命中哪个缓冲区了, 不需要维护index命中的哪个sync.Pool
// 如果用户传些奇奇怪怪的数字,就不好办了
func WithServerBufioMultipleTimesPayloadSize(mt float32) ServerOption {
return func(o *ConnOption) {
if mt <= 0 {
mt = 1.0
}
o.bufioMultipleTimesPayloadSize = mt
}
}

func WithClientBufioMultipleTimesPayloadSize(mt float32) ClientOption {
return func(o *DialOption) {
if mt <= 0 {
mt = 1.0
}
o.bufioMultipleTimesPayloadSize = mt
}
}
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
utf8Check func([]byte) bool // utf8检查
readTimeout time.Duration
windowsMultipleTimesPayloadSize float32 // 设置几倍的payload大小
bufioMultipleTimesPayloadSize float32 // 设置几倍的payload大小
parseMode parseMode // 解析模式
}

Expand Down
7 changes: 7 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"net"
"sync"
"time"
"unsafe"

"github.com/antlabs/wsutil/bufio2"
"github.com/antlabs/wsutil/bytespool"
"github.com/antlabs/wsutil/enum"
"github.com/antlabs/wsutil/fixedreader"
Expand Down Expand Up @@ -171,6 +173,11 @@ func (c *Conn) readLoop() error {

var payload []byte
if c.read != nil {
newSize := int(1024 * c.bufioMultipleTimesPayloadSize)
if c.read.Size() < newSize {
// TODO sync.Pool管理
(*bufio2.Reader2)(unsafe.Pointer(c.read)).ResetBuf(make([]byte, newSize))
}
payload = *bytespool.GetBytes(1024 + enum.MaxFrameHeaderSize)
}
for {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/antlabs/quickws

go 1.20

require github.com/antlabs/wsutil v0.0.12
require github.com/antlabs/wsutil v0.1.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/antlabs/wsutil v0.0.12 h1:YHn8uuwhxYyy0tgpCWgDHNnKouaEhtQUkrX0QNgoDqc=
github.com/antlabs/wsutil v0.0.12/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=
github.com/antlabs/wsutil v0.1.0 h1:421b5YluJ/RJ43smPy2pJoP8IfQ1LRcrBwDLuRTy+0w=
github.com/antlabs/wsutil v0.1.0/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=

0 comments on commit 086e69a

Please sign in to comment.