Use a buffer pool for METADAPT-A
This commit is contained in:
parent
9651fed635
commit
8c564e4755
29
metadapta.go
29
metadapta.go
@ -18,6 +18,12 @@ const closeMethod = 0xFFFF
|
||||
const int64Max = int64((^uint64(0)) >> 1)
|
||||
const defaultChunkSize = 0x1000
|
||||
|
||||
var bufferPool = sync.Pool {
|
||||
New: func() any {
|
||||
return &bytes.Buffer { }
|
||||
},
|
||||
}
|
||||
|
||||
// Party represents a side of a connection.
|
||||
type Party bool; const (
|
||||
ServerSide Party = false
|
||||
@ -223,12 +229,12 @@ type transA struct {
|
||||
parent *a
|
||||
id int64
|
||||
incoming usync.Gate[incomingMessage]
|
||||
currentReader io.Reader
|
||||
currentWriter io.Closer
|
||||
writeBuffer bytes.Buffer
|
||||
closed atomic.Bool
|
||||
closeErr error
|
||||
|
||||
currentReader io.Reader
|
||||
currentWriter io.Closer
|
||||
|
||||
deadline *time.Timer
|
||||
deadlineLock sync.Mutex
|
||||
}
|
||||
@ -271,7 +277,6 @@ func (this *transA) SendWriter(method uint16) (io.WriteCloser, error) {
|
||||
}
|
||||
|
||||
// create new writer
|
||||
this.writeBuffer.Reset()
|
||||
writer := &writerA {
|
||||
parent: this,
|
||||
// there is only ever one writer at a time, so they can all
|
||||
@ -280,7 +285,7 @@ func (this *transA) SendWriter(method uint16) (io.WriteCloser, error) {
|
||||
// back in. it will work just fine bc we dont ever allocate more than the chunk
|
||||
// size anyway. perhaps create some sort of config value that disables this
|
||||
// behavior and just uses one buffer
|
||||
buffer: &this.writeBuffer,
|
||||
buffer: bufferPool.Get().(*bytes.Buffer),
|
||||
method: method,
|
||||
chunkSize: defaultChunkSize,
|
||||
open: true,
|
||||
@ -431,10 +436,18 @@ func (this *writerA) Write(data []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (this *writerA) Close() error {
|
||||
if this.buffer.Len() > 0 {
|
||||
this.flush(0)
|
||||
if this.buffer != nil {
|
||||
// flush if needed
|
||||
if this.buffer.Len() > 0 {
|
||||
this.flush(0)
|
||||
}
|
||||
this.open = false
|
||||
|
||||
// reset the buffer and put it back in the pool
|
||||
this.buffer.Reset()
|
||||
bufferPool.Put(this.buffer)
|
||||
this.buffer = nil
|
||||
}
|
||||
this.open = false
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user