Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jul 1, 2023
1 parent c3d8c52 commit f325c64
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func Dial(rawUrl string, opts ...OptionClient) (*Conn, error) {
if dial.Header == nil {
dial.Header = make(http.Header)
}

dial.multipleTimesPayloadSize = 1.0
for _, o := range opts {
o(&dial)
}
Expand Down
11 changes: 6 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import "time"

type config struct {
Callback
replyPing bool // 开启自动回复
decompression bool // 开启解压缩功能
compression bool // 开启压缩功能
ignorePong bool // 忽略pong消息
readTimeout time.Duration
replyPing bool // 开启自动回复
decompression bool // 开启解压缩功能
compression bool // 开启压缩功能
ignorePong bool // 忽略pong消息
readTimeout time.Duration
multipleTimesPayloadSize float32 // 设置几倍的payload大小
}
8 changes: 6 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *Conn) readDataFromNet(fixedBuf *fixedreader.FixedReader, headArray *[en
}
}

f, err = frame.ReadFrame(fixedBuf, headArray)
f, err = frame.ReadFrame2(fixedBuf, headArray, c.multipleTimesPayloadSize)
if err != nil {
c.Callback.OnClose(c, err)
return
Expand All @@ -110,6 +110,10 @@ func (c *Conn) readDataFromNet(fixedBuf *fixedreader.FixedReader, headArray *[en
return
}

func (c *Conn) initPayloadSize() int {
return int(1024.0 * c.multipleTimesPayloadSize)
}

// 读取websocket frame.Frame的循环
func (c *Conn) readLoop() error {
var f frame.Frame
Expand All @@ -125,7 +129,7 @@ func (c *Conn) readLoop() error {
fixedBuf = c.fr
} else {
// 默认最小1k + 14
fixedBuf = fixedreader.NewFixedReader(c.c, bytespool.GetBytes(1024+enum.MaxFrameHeaderSize))
fixedBuf = fixedreader.NewFixedReader(c.c, bytespool.GetBytes(c.initPayloadSize()+enum.MaxFrameHeaderSize))
}
defer fixedBuf.Release()

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.3
require github.com/antlabs/wsutil v0.0.4
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.3 h1:h20WHmHkLeonPcdTVXoH55thpLc+kTwD4JS0OdR8YLk=
github.com/antlabs/wsutil v0.0.3/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=
github.com/antlabs/wsutil v0.0.4 h1:xjWfIMQKX0RQlt9vszMqqYHkgAwVMwsAuv7k87DVPwE=
github.com/antlabs/wsutil v0.0.4/go.mod h1:7ec5eUM7nmKW+Oi6F1I58iatOeL9k+yIsfOh1zh910g=
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type ConnOption struct {

func Upgrade(w http.ResponseWriter, r *http.Request, opts ...OptionServer) (c *Conn, err error) {
var conf ConnOption
conf.multipleTimesPayloadSize = 1.0
for _, o := range opts {
o(&conf)
}
Expand Down
11 changes: 11 additions & 0 deletions server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ func WithServerIgnorePong() OptionServer {
o.ignorePong = true
}
}

// 设置几倍payload的缓冲区
func WithMultipleTimesPayloadSize(mt float32) OptionServer {
return func(o *ConnOption) {
// 如果mt < 1.0, 直接panic
if mt < 1.0 {
panic("multipleTimesPayloadSize must >= 1.0")
}
o.multipleTimesPayloadSize = mt
}
}

0 comments on commit f325c64

Please sign in to comment.