bot: close stop channels only by their owners

pull/283/head
Demian 4 years ago
parent 860ac059a8
commit 31048be20c

@ -176,7 +176,7 @@ func (b *Bot) Start() {
b.incomingUpdate(&upd)
// call to stop polling
case <-b.stop:
stop <- struct{}{}
close(stop)
return
}
}

@ -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
@ -101,7 +93,6 @@ func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
for {
select {
case <-stop:
close(stop)
return
default:
}

Loading…
Cancel
Save