From f5c6a478a0e3271a51649c64661ac90ed3d30bf1 Mon Sep 17 00:00:00 2001 From: Syfaro Date: Fri, 27 Jan 2017 11:40:09 -0600 Subject: [PATCH] Allow specifying a custom buffer size for the updates channel --- bot.go | 13 ++++++++----- configs.go | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/bot.go b/bot.go index bf541a0..c2b4529 100644 --- a/bot.go +++ b/bot.go @@ -21,8 +21,10 @@ import ( // BotAPI allows you to interact with the Telegram Bot API. type BotAPI struct { - Token string `json:"token"` - Debug bool `json:"debug"` + Token string `json:"token"` + Debug bool `json:"debug"` + Buffer int `json:"buffer"` + Self User `json:"-"` Client *http.Client `json:"-"` } @@ -42,11 +44,12 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { bot := &BotAPI{ Token: token, Client: client, + Buffer: 100, } self, err := bot.GetMe() if err != nil { - return &BotAPI{}, err + return nil, err } bot.Self = self @@ -461,7 +464,7 @@ func (bot *BotAPI) GetWebhookInfo() (WebhookInfo, error) { // GetUpdatesChan starts and returns a channel for getting updates. func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { - ch := make(chan Update, 100) + ch := make(chan Update, bot.Buffer) go func() { for { @@ -488,7 +491,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { // ListenForWebhook registers a http handler for a webhook. func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel { - ch := make(chan Update, 100) + ch := make(chan Update, bot.Buffer) http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { bytes, _ := ioutil.ReadAll(r.Body) diff --git a/configs.go b/configs.go index 68c14e9..5352933 100644 --- a/configs.go +++ b/configs.go @@ -768,8 +768,8 @@ type UpdateConfig struct { // WebhookConfig contains information about a SetWebhook request. type WebhookConfig struct { - URL *url.URL - Certificate interface{} + URL *url.URL + Certificate interface{} MaxConnections int }