Skip to content

Commit f237e28

Browse files
committed
improved buffer balance
1 parent f91e4d5 commit f237e28

File tree

12 files changed

+43
-24
lines changed

12 files changed

+43
-24
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
[12]: https://github.com/avelino/awesome-go#networking
3030

3131
- [gws](#gws)
32-
- [Event-Driven Go WebSocket Server \& Client](#event-driven-go-websocket-server--client)
3332
- [Feature](#feature)
3433
- [Attention](#attention)
3534
- [Install](#install)
@@ -274,7 +273,7 @@ BenchmarkConn_ReadMessage/compress_enabled-8 435176 24
274273
> 微信二维码在讨论区不定时更新
275274
276275
<div>
277-
<img src="assets/wechat.jpg" alt="WeChat" width="300" height="300" style="display: inline-block;"/>
276+
<img src="assets/wechat.png" alt="WeChat" width="300" height="300" style="display: inline-block;"/>
278277
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
279278
<img src="assets/qq.jpg" alt="QQ" width="300" height="300" style="display: inline-block"/>
280279
</div>

assets/latency.png

-385 KB
Binary file not shown.
-37.8 KB
Loading

assets/wechat.jpg

-86.8 KB
Binary file not shown.

assets/wechat.png

193 KB
Loading

compress.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"sync/atomic"
1212
)
1313

14-
const compressionRate = 3
15-
1614
type compressors struct {
1715
serial uint64
1816
size uint64
@@ -85,23 +83,38 @@ func (c *decompressors) Select() *decompressor {
8583
}
8684

8785
func newDecompressor() *decompressor {
88-
return &decompressor{fr: flate.NewReader(nil)}
86+
return &decompressor{
87+
b: bytes.NewBuffer(nil),
88+
fr: flate.NewReader(nil),
89+
}
8990
}
9091

9192
type decompressor struct {
9293
sync.Mutex
94+
b *bytes.Buffer
9395
fr io.ReadCloser
9496
}
9597

98+
func (c *decompressor) reset(r io.Reader) {
99+
resetter := c.fr.(flate.Resetter)
100+
_ = resetter.Reset(r, nil) // must return a null pointer
101+
if c.b.Cap() > 256*1024 {
102+
c.b = bytes.NewBuffer(nil)
103+
}
104+
c.b.Reset()
105+
}
106+
96107
// Decompress 解压
97108
func (c *decompressor) Decompress(src *bytes.Buffer) (*bytes.Buffer, int, error) {
98109
c.Lock()
99110
defer c.Unlock()
100111

101112
_, _ = src.Write(internal.FlateTail)
102-
resetter := c.fr.(flate.Resetter)
103-
_ = resetter.Reset(src, nil) // must return a null pointer
104-
var dst, idx = elasticPool.Get(src.Len() * compressionRate)
105-
_, err := c.fr.(io.WriterTo).WriteTo(dst)
106-
return dst, idx, err
113+
c.reset(src)
114+
if _, err := c.fr.(io.WriterTo).WriteTo(c.b); err != nil {
115+
return nil, 0, err
116+
}
117+
var dst, idx = binaryPool.Get(c.b.Len())
118+
_, _ = c.b.WriteTo(dst)
119+
return dst, idx, nil
107120
}

compress_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ func TestFlate(t *testing.T) {
5353
as.Error(err)
5454
})
5555
}
56+
57+
func TestDecompressor_Init(t *testing.T) {
58+
var d = &decompressor{
59+
b: bytes.NewBuffer(internal.AlphabetNumeric.Generate(512 * 1024)),
60+
fr: flate.NewReader(nil),
61+
}
62+
d.reset(nil)
63+
assert.Equal(t, d.b.Cap(), 0)
64+
}

init.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package gws
33
import "github.com/lxzan/gws/internal"
44

55
var (
6-
myPadding = frameHeader{} // 帧头填充物
7-
staticPool = internal.NewBufferPool() // 静态缓冲池
8-
elasticPool = internal.NewBufferPool() // 弹性缓冲池
6+
myPadding = frameHeader{} // 帧头填充物
7+
binaryPool = internal.NewBufferPool() // 静态缓冲池
98
)

internal/pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (p *BufferPool) Put(b *bytes.Buffer, index int) {
4040
if index == 0 || b == nil {
4141
return
4242
}
43-
if b.Cap() <= 5*p.limits[index] {
43+
if b.Cap() <= p.limits[index] {
4444
p.pools[index].Put(b)
4545
}
4646
}

protocol.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ func (c *Message) Bytes() []byte {
246246

247247
// Close recycle buffer
248248
func (c *Message) Close() error {
249-
pool := internal.SelectValue(c.compressed, elasticPool, staticPool)
250-
pool.Put(c.Data, c.index)
249+
binaryPool.Put(c.Data, c.index)
251250
c.Data = nil
252251
return nil
253252
}

0 commit comments

Comments
 (0)