Skip to content

Commit

Permalink
feat: add TGStore.fileHeadPool
Browse files Browse the repository at this point in the history
  • Loading branch information
aofei committed Jan 12, 2021
1 parent 53c6650 commit 655b47a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tgstore.go
Expand Up @@ -91,6 +91,7 @@ type TGStore struct {
loadError error
bot *telebot.Bot
chat *telebot.Chat
fileHeadPool sync.Pool
objectMaxContentBytes int64
objectMinContentBytes int64
objectMetadataCache *bigcache.BigCache
Expand All @@ -106,6 +107,11 @@ func New() *TGStore {
MaxFileBytes: 20 << 20,
MaxObjectMetadataCacheBytes: 1 << 20,
HTTPClient: http.DefaultClient,
fileHeadPool: sync.Pool{
New: func() interface{} {
return make([]byte, 1<<20)
},
},
}
}

Expand Down Expand Up @@ -490,7 +496,9 @@ func (tgs *TGStore) uploadTelegramFile(
ctx context.Context,
content io.Reader,
) (string, error) {
head := make([]byte, 1<<20)
head := tgs.fileHeadPool.Get().([]byte)[:1<<20]
defer tgs.fileHeadPool.Put(head)

if n, err := io.ReadFull(content, head); err != nil {
switch {
case errors.Is(err, io.EOF):
Expand Down

0 comments on commit 655b47a

Please sign in to comment.