Skip to content

Commit

Permalink
bot: fix long poller stopping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Apr 26, 2020
1 parent 0910baa commit c14c51a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
14 changes: 5 additions & 9 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,19 @@ func (b *Bot) Start() {
// handle incoming updates
case upd := <-b.Updates:
b.incomingUpdate(&upd)

// call to stop polling
case <-b.stop:
stop <- struct{}{}

// polling has stopped
case <-stop:
return
}
}
}

// Stop gracefully shuts the poller down.
func (b *Bot) Stop() {
b.stop <- struct{}{}
}

func (b *Bot) incomingUpdate(upd *Update) {
if upd.Message != nil {
m := upd.Message
Expand Down Expand Up @@ -505,11 +506,6 @@ func (b *Bot) handleMedia(m *Message) bool {
return true
}

// Stop gracefully shuts the poller down.
func (b *Bot) Stop() {
b.stop <- struct{}{}
}

// Send accepts 2+ arguments, starting with destination chat, followed by
// some Sendable (or string!) and optional send options.
//
Expand Down
12 changes: 7 additions & 5 deletions poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ type LongPoller struct {

// Poll does long polling.
func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
go func(stop chan struct{}) {
<-stop
close(stop)
}(stop)

for {
select {
case <-stop:
close(stop)

This comment has been minimized.

Copy link
@stek29

stek29 Apr 28, 2020

Collaborator

closing channel not “owned” by function is an antipattern in go afaik.

do you avoid closing it in (*Bot).Start() to avoid breaking possible existing pollers (close of closed channel)?

return
default:
}

updates, err := b.getUpdates(p.LastUpdateID+1, p.Limit, p.Timeout, p.AllowedUpdates)
if err != nil {
b.debug(err)
Expand Down

0 comments on commit c14c51a

Please sign in to comment.