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 int64Max = int64((^uint64(0)) >> 1)
|
||||||
const defaultChunkSize = 0x1000
|
const defaultChunkSize = 0x1000
|
||||||
|
|
||||||
|
var bufferPool = sync.Pool {
|
||||||
|
New: func() any {
|
||||||
|
return &bytes.Buffer { }
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Party represents a side of a connection.
|
// Party represents a side of a connection.
|
||||||
type Party bool; const (
|
type Party bool; const (
|
||||||
ServerSide Party = false
|
ServerSide Party = false
|
||||||
@ -223,12 +229,12 @@ type transA struct {
|
|||||||
parent *a
|
parent *a
|
||||||
id int64
|
id int64
|
||||||
incoming usync.Gate[incomingMessage]
|
incoming usync.Gate[incomingMessage]
|
||||||
currentReader io.Reader
|
|
||||||
currentWriter io.Closer
|
|
||||||
writeBuffer bytes.Buffer
|
|
||||||
closed atomic.Bool
|
closed atomic.Bool
|
||||||
closeErr error
|
closeErr error
|
||||||
|
|
||||||
|
currentReader io.Reader
|
||||||
|
currentWriter io.Closer
|
||||||
|
|
||||||
deadline *time.Timer
|
deadline *time.Timer
|
||||||
deadlineLock sync.Mutex
|
deadlineLock sync.Mutex
|
||||||
}
|
}
|
||||||
@ -271,7 +277,6 @@ func (this *transA) SendWriter(method uint16) (io.WriteCloser, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create new writer
|
// create new writer
|
||||||
this.writeBuffer.Reset()
|
|
||||||
writer := &writerA {
|
writer := &writerA {
|
||||||
parent: this,
|
parent: this,
|
||||||
// there is only ever one writer at a time, so they can all
|
// 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
|
// 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
|
// size anyway. perhaps create some sort of config value that disables this
|
||||||
// behavior and just uses one buffer
|
// behavior and just uses one buffer
|
||||||
buffer: &this.writeBuffer,
|
buffer: bufferPool.Get().(*bytes.Buffer),
|
||||||
method: method,
|
method: method,
|
||||||
chunkSize: defaultChunkSize,
|
chunkSize: defaultChunkSize,
|
||||||
open: true,
|
open: true,
|
||||||
@ -431,10 +436,18 @@ func (this *writerA) Write(data []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *writerA) Close() error {
|
func (this *writerA) Close() error {
|
||||||
if this.buffer.Len() > 0 {
|
if this.buffer != nil {
|
||||||
this.flush(0)
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user