Skip to content

Commit

Permalink
add func: SetBufferPool
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Jul 25, 2024
1 parent b85d0de commit 4cdceff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ package gws
import "github.com/lxzan/gws/internal"

var (
framePadding = frameHeader{} // 帧头填充物
binaryPool = internal.NewBufferPool(128, 256*1024) // 缓冲池
defaultLogger = new(stdLogger) // 默认日志工具
framePadding = frameHeader{} // 帧头填充物
binaryPool = new(internal.BufferPool) // 内存池
defaultLogger = new(stdLogger) // 默认日志工具
)

func init() {
SetBufferPool(256 * 1024)
}

// SetBufferPool set up the memory pool, any memory that exceeds maxSize will not be reclaimed.
// 设置内存池, 超过maxSize将不会被回收.
func SetBufferPool(maxSize uint32) {
binaryPool = internal.NewBufferPool(128, maxSize)
}
2 changes: 1 addition & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *Conn) genFrame(opcode Opcode, payload internal.Payload, isBroadcast boo
return nil, internal.CloseMessageTooLarge
}

var buf = binaryPool.Get(n*11/10 + frameHeaderSize)
var buf = binaryPool.Get(n*105/100 + frameHeaderSize)
buf.Write(framePadding[0:])

if c.pd.Enabled && opcode.isDataFrame() && n >= c.pd.Threshold {
Expand Down

0 comments on commit 4cdceff

Please sign in to comment.