From c530175b21b40223da69bfc08120fcaae9028f07 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Fri, 18 Aug 2023 13:17:15 +0800 Subject: [PATCH] up --- config.go | 6 +++--- config_test.go | 15 +++++++++++++++ conn_test.go | 4 ++-- upgrade.go | 3 +-- 4 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 config_test.go diff --git a/config.go b/config.go index a2dc1b6..6a1b29e 100644 --- a/config.go +++ b/config.go @@ -30,8 +30,8 @@ type Config struct { disableBufioClearHack bool // 关闭bufio的clear hack优化 utf8Check func([]byte) bool // utf8检查 readTimeout time.Duration - windowsMultipleTimesPayloadSize float32 // 设置几倍的payload大小 - bufioMultipleTimesPayloadSize float32 // 设置几倍的payload大小 + windowsMultipleTimesPayloadSize float32 // 设置几倍(1024+14)的payload大小 + bufioMultipleTimesPayloadSize float32 // 设置几倍(1024)的payload大小 parseMode parseMode // 解析模式 maxDelayWriteNum int32 // 最大延迟包的个数, 默认值为10 delayWriteInitBufferSize int32 // 延迟写入的初始缓冲区大小, 默认值是8k @@ -39,7 +39,7 @@ type Config struct { } func (c *Config) initPayloadSize() int { - return int(1024.0 + float32(enum.MaxFrameHeaderSize)*c.windowsMultipleTimesPayloadSize) + return int((1024.0 + float32(enum.MaxFrameHeaderSize)) * c.windowsMultipleTimesPayloadSize) } // 默认设置 diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..f5e89fb --- /dev/null +++ b/config_test.go @@ -0,0 +1,15 @@ +package quickws + +import "testing" + +func Test_InitPayloadSize(t *testing.T) { + t.Run("InitPayload", func(t *testing.T) { + var c Config + for i := 1; i < 32; i++ { + c.windowsMultipleTimesPayloadSize = float32(i) + if c.initPayloadSize() != i*(1024+14) { + t.Errorf("initPayloadSize() = %d, want %d", c.initPayloadSize(), i*(1024+14)) + } + } + }) +} diff --git a/conn_test.go b/conn_test.go index 4ede716..8a62fd9 100644 --- a/conn_test.go +++ b/conn_test.go @@ -338,7 +338,7 @@ func TestFragmentFrame(t *testing.T) { t.Run("FragmentFrame-Compression", func(t *testing.T) { run := int32(0) data := make(chan string, 1) - upgrade := NewUpgrade(WithServerBufioParseMode(), WithServerOnMessageFunc(func(c *Conn, op Opcode, payload []byte) { + upgrade := NewUpgrade(WithServerBufioParseMode(), WithServerDecompression(), WithServerOnMessageFunc(func(c *Conn, op Opcode, payload []byte) { c.WriteMessage(op, payload) })) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -352,7 +352,7 @@ func TestFragmentFrame(t *testing.T) { defer ts.Close() url := strings.ReplaceAll(ts.URL, "http", "ws") - con, err := Dial(url, WithClientDisableBufioClearHack(), WithClientOnMessageFunc(func(c *Conn, mt Opcode, payload []byte) { + con, err := Dial(url, WithClientDisableBufioClearHack(), WithClientDecompressAndCompress(), WithClientOnMessageFunc(func(c *Conn, mt Opcode, payload []byte) { atomic.AddInt32(&run, int32(1)) data <- string(payload) })) diff --git a/upgrade.go b/upgrade.go index c17857f..01010ce 100644 --- a/upgrade.go +++ b/upgrade.go @@ -23,7 +23,6 @@ import ( "github.com/antlabs/wsutil/bufio2" "github.com/antlabs/wsutil/bytespool" - "github.com/antlabs/wsutil/enum" "github.com/antlabs/wsutil/fixedreader" "github.com/antlabs/wsutil/rsp" ) @@ -111,7 +110,7 @@ func upgradeInner(w http.ResponseWriter, r *http.Request, conf *Config) (c *Conn var bp bytespool.BytesPool bp.Init() if conf.parseMode == ParseModeWindows { - fr.Init(conn, bytespool.GetBytes(conf.initPayloadSize()+enum.MaxFrameHeaderSize)) + fr.Init(conn, bytespool.GetBytes(conf.initPayloadSize())) } conn.SetDeadline(time.Time{})