use Pool to manage buffer

master
fangdingjun 6 years ago
parent 8895d6a0ec
commit 1f6cfb8afc

@ -374,14 +374,28 @@ func (c *Conn) getServerName() string {
return name return name
} }
var bufPool = &sync.Pool{
New: func() interface{} {
return make([]byte, 16*1024)
},
}
// onDataReadCallback callback function for gnutls library want to read data from network // onDataReadCallback callback function for gnutls library want to read data from network
// //
//export onDataReadCallback //export onDataReadCallback
func onDataReadCallback(d unsafe.Pointer, cbuf *C.char, bufLen C.int) C.int { func onDataReadCallback(d unsafe.Pointer, cbuf *C.char, bufLen C.int) C.int {
//log.Println("read addr ", uintptr(d)) //log.Println("read addr ", uintptr(d))
conn := (*Conn)(unsafe.Pointer((uintptr(d)))) conn := (*Conn)(unsafe.Pointer((uintptr(d))))
buf := make([]byte, int(bufLen))
n, err := conn.c.Read(buf) //buf := make([]byte, int(bufLen))
_length := int(bufLen)
buf := bufPool.Get().([]byte)
if len(buf) < _length {
buf = make([]byte, _length)
}
defer bufPool.Put(buf)
n, err := conn.c.Read(buf[:_length])
if err != nil { if err != nil {
//log.Println(err) //log.Println(err)
// 0 indicates connection termination // 0 indicates connection termination

Loading…
Cancel
Save