mirror of
https://github.com/tinode/chat.git
synced 2026-05-07 20:12:42 +00:00
Merge branch 'devel' of https://github.com/tinode/chat into devel
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/tinode/chat/server/logs"
|
||||
"github.com/tinode/chat/server/push"
|
||||
@@ -32,6 +33,20 @@ const (
|
||||
|
||||
var handler Handler
|
||||
|
||||
const maxPooledPostBodyCap = 1 << 16
|
||||
|
||||
var postBodyPool = sync.Pool{
|
||||
New: func() any {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
|
||||
var gzipWriterPool = sync.Pool{
|
||||
New: func() any {
|
||||
return gzip.NewWriter(nil)
|
||||
},
|
||||
}
|
||||
|
||||
// Handler represents state of TNPG push client.
|
||||
type Handler struct {
|
||||
input chan *push.Receipt
|
||||
@@ -155,10 +170,25 @@ func (Handler) Init(jsonconf json.RawMessage) (bool, error) {
|
||||
}
|
||||
|
||||
func postMessage(endpoint string, body any, config *configType) (*batchResponse, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
gzw := gzip.NewWriter(buf)
|
||||
buf := postBodyPool.Get().(*bytes.Buffer)
|
||||
defer func() {
|
||||
buf.Reset()
|
||||
if cap(buf.Bytes()) > maxPooledPostBodyCap {
|
||||
return
|
||||
}
|
||||
postBodyPool.Put(buf)
|
||||
}()
|
||||
|
||||
gzw := gzipWriterPool.Get().(*gzip.Writer)
|
||||
defer func() {
|
||||
gzw.Reset(nil)
|
||||
gzipWriterPool.Put(gzw)
|
||||
}()
|
||||
gzw.Reset(buf)
|
||||
err := json.NewEncoder(gzw).Encode(body)
|
||||
gzw.Close()
|
||||
if closeErr := gzw.Close(); err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user