Skip to content

Commit

Permalink
optimize memory release
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Sep 20, 2023
1 parent 828116b commit 376c264
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
- [x] Dial via Proxy
- [x] IO Multiplexing
- [x] Concurrent Write
- [x] Zero Allocs Read/Write (Compression Disabled)
- [x] Zero Allocs Read/Write
- [x] Passes WebSocket [autobahn-testsuite](https://lxzan.github.io/gws/reports/servers/)

### Attention
Expand Down
13 changes: 9 additions & 4 deletions examples/chatroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"time"
)

const PingInterval = 15 * time.Second // 客户端心跳间隔
const (
PingInterval = 10 * time.Second // 客户端心跳间隔
HeartbeatWaitTimeout = 5 * time.Second // 心跳等待超时时间
)

//go:embed index.html
var html []byte
Expand Down Expand Up @@ -90,7 +93,10 @@ func (c *WebSocket) OnClose(socket *gws.Conn, err error) {
log.Printf("onerror, name=%s, msg=%s\n", name, err.Error())
}

func (c *WebSocket) OnPing(socket *gws.Conn, payload []byte) {}
func (c *WebSocket) OnPing(socket *gws.Conn, payload []byte) {
_ = socket.SetDeadline(time.Now().Add(PingInterval + HeartbeatWaitTimeout))
_ = socket.WriteString("pong")
}

func (c *WebSocket) OnPong(socket *gws.Conn, payload []byte) {}

Expand All @@ -104,8 +110,7 @@ func (c *WebSocket) OnMessage(socket *gws.Conn, message *gws.Message) {

// chrome websocket不支持ping方法, 所以在text frame里面模拟ping
if b := message.Data.Bytes(); len(b) == 4 && string(b) == "ping" {
socket.WriteMessage(gws.OpcodeText, []byte("pong"))
socket.SetDeadline(time.Now().Add(3 * PingInterval))
c.OnPing(socket, nil)
return
}

Expand Down
6 changes: 2 additions & 4 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ type Buffer struct {

//go:nosplit
func BufferReset(b *bytes.Buffer, p []byte) {
buffer := (*Buffer)(unsafe.Pointer(b))
buffer.buf = p
buffer.off = 0
buffer.lastRead = 0
buffer := (*Buffer)(unsafe.Pointer(b)) // 类型强转
buffer.buf = p // 修改后面的属性一定要加偏移量!!!
}
7 changes: 6 additions & 1 deletion updrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (c *responseWriter) Init() *responseWriter {
return c
}

func (c *responseWriter) Close() {
binaryPool.Put(c.b, c.idx)
c.b = nil
}

func (c *responseWriter) WithHeader(k, v string) {
c.b.WriteString(k)
c.b.WriteString(": ")
Expand Down Expand Up @@ -64,7 +69,6 @@ func (c *responseWriter) Write(conn net.Conn, timeout time.Duration) error {
if _, err := c.b.WriteTo(conn); err != nil {
return err
}
binaryPool.Put(c.b, c.idx)
return conn.SetDeadline(time.Time{})
}

Expand Down Expand Up @@ -132,6 +136,7 @@ func (c *Upgrader) doUpgrade(r *http.Request, netConn net.Conn, br *bufio.Reader
}

var rw = new(responseWriter).Init()
defer rw.Close()
if val := r.Header.Get(internal.SecWebSocketExtensions.Key); strings.Contains(val, internal.PermessageDeflate) && c.option.CompressEnabled {
rw.WithHeader(internal.SecWebSocketExtensions.Key, internal.SecWebSocketExtensions.Val)
compressEnabled = true
Expand Down

0 comments on commit 376c264

Please sign in to comment.