Skip to content

Commit

Permalink
bot: close stop channels only by their owners
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed May 1, 2020
1 parent 860ac05 commit 31048be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bot.go
Expand Up @@ -176,7 +176,7 @@ func (b *Bot) Start() {
b.incomingUpdate(&upd)
// call to stop polling
case <-b.stop:
stop <- struct{}{}
close(stop)
return
}
}
Expand Down
17 changes: 4 additions & 13 deletions poller.go
Expand Up @@ -41,28 +41,20 @@ func NewMiddlewarePoller(original Poller, filter func(*Update) bool) *Middleware

// Poll sieves updates through middleware filter.
func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
cap := 1
if p.Capacity > 1 {
cap = p.Capacity
if p.Capacity < 1 {
p.Capacity = 1
}

middle := make(chan Update, cap)
middle := make(chan Update, p.Capacity)
stopPoller := make(chan struct{})

go p.Poller.Poll(b, middle, stopPoller)

for {
select {

// call to stop
case <-stop:
stopPoller <- struct{}{}

// poller is done
case <-stopPoller:
close(stop)
close(stopPoller)
return

case upd := <-middle:
if p.Filter(&upd) {
dest <- upd
Expand Down Expand Up @@ -101,7 +93,6 @@ func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
for {
select {
case <-stop:
close(stop)
return
default:
}
Expand Down

0 comments on commit 31048be

Please sign in to comment.