From 6066232e91aeef2607d71121667ea47e970639a3 Mon Sep 17 00:00:00 2001 From: lxzan Date: Thu, 12 Oct 2023 21:33:01 +0800 Subject: [PATCH] exception recovery --- benchmark_test.go | 6 ++---- conn.go | 3 +-- reader.go | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index dea11094..82a691bf 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -67,7 +67,6 @@ func BenchmarkConn_ReadMessage(b *testing.B) { config: upgrader.option.getConfig(), } var buf, _, _ = conn1.genFrame(OpcodeText, githubData) - var closer = &bufferWrapper{} var reader = bytes.NewBuffer(buf.Bytes()) var conn2 = &Conn{ @@ -80,12 +79,11 @@ func BenchmarkConn_ReadMessage(b *testing.B) { for i := 0; i < b.N; i++ { internal.BufferReset(reader, buf.Bytes()) conn2.br.Reset(reader) - _ = conn2.readMessage(closer) + _ = conn2.readMessage() } }) b.Run("compress enabled", func(b *testing.B) { - var closer = &bufferWrapper{} var upgrader = NewUpgrader(handler, &ServerOption{CompressEnabled: true}) var config = upgrader.option.getConfig() var conn1 = &Conn{ @@ -112,7 +110,7 @@ func BenchmarkConn_ReadMessage(b *testing.B) { for i := 0; i < b.N; i++ { internal.BufferReset(reader, buf.Bytes()) conn2.br.Reset(reader) - _ = conn2.readMessage(closer) + _ = conn2.readMessage() } }) } diff --git a/conn.go b/conn.go index 58ac18af..60283459 100644 --- a/conn.go +++ b/conn.go @@ -52,9 +52,8 @@ func (c *Conn) init() *Conn { // If HTTP Server is reused, it is recommended to enable goroutine, as blocking will prevent the context from being GC. func (c *Conn) ReadLoop() { c.handler.OnOpen(c) - closer := &bufferWrapper{} for { - if err := c.readMessage(closer); err != nil { + if err := c.readMessage(); err != nil { c.emitError(err) break } diff --git a/reader.go b/reader.go index af045e5b..e637c297 100644 --- a/reader.go +++ b/reader.go @@ -56,7 +56,7 @@ func (c *Conn) readControl() error { } } -func (c *Conn) readMessage(closer *bufferWrapper) error { +func (c *Conn) readMessage() error { if c.isClosed() { return internal.CloseNormalClosure } @@ -95,7 +95,7 @@ func (c *Conn) readMessage(closer *bufferWrapper) error { var fin = c.fh.GetFIN() var buf, index = binaryPool.Get(contentLength) var p = buf.Bytes()[:contentLength] - closer.Buffer, closer.index = buf, index + var closer = bufferWrapper{buf, index} defer closer.Close() if err := internal.ReadN(c.br, p); err != nil {