|
|
@ -15,7 +15,7 @@ type Poller interface {
|
|
|
|
// Poll is supposed to take the bot object
|
|
|
|
// Poll is supposed to take the bot object
|
|
|
|
// subscription channel and start polling
|
|
|
|
// subscription channel and start polling
|
|
|
|
// for Updates immediately.
|
|
|
|
// for Updates immediately.
|
|
|
|
Poll(b *Bot, dest chan Update)
|
|
|
|
Poll(b *Bot, updates chan Update, stop chan struct{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MiddlewarePoller is a special kind of poller that acts
|
|
|
|
// MiddlewarePoller is a special kind of poller that acts
|
|
|
@ -40,21 +40,28 @@ func Middleware(p Poller, filter func(*Update) bool) *MiddlewarePoller {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Poll sieves updates through middleware filter.
|
|
|
|
// Poll sieves updates through middleware filter.
|
|
|
|
func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update) {
|
|
|
|
func (p *MiddlewarePoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
|
|
|
|
cap := 1
|
|
|
|
cap := 1
|
|
|
|
if p.Capacity > 1 {
|
|
|
|
if p.Capacity > 1 {
|
|
|
|
cap = p.Capacity
|
|
|
|
cap = p.Capacity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
middle := make(chan Update, cap)
|
|
|
|
middle := make(chan Update, cap)
|
|
|
|
|
|
|
|
stop2 := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
|
|
go p.Poller.Poll(b, middle)
|
|
|
|
go p.Poller.Poll(b, middle, stop2)
|
|
|
|
|
|
|
|
|
|
|
|
for upd := range middle {
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-stop:
|
|
|
|
|
|
|
|
close(stop2)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
case upd := <-middle:
|
|
|
|
if p.filter(&upd) {
|
|
|
|
if p.filter(&upd) {
|
|
|
|
dest <- upd
|
|
|
|
dest <- upd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// LongPoller is a classic LongPoller with timeout.
|
|
|
|
// LongPoller is a classic LongPoller with timeout.
|
|
|
@ -63,10 +70,14 @@ type LongPoller struct {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Poll does long polling.
|
|
|
|
// Poll does long polling.
|
|
|
|
func (p *LongPoller) Poll(b *Bot, dest chan Update) {
|
|
|
|
func (p *LongPoller) Poll(b *Bot, dest chan Update, stop chan struct{}) {
|
|
|
|
var latestUpd int
|
|
|
|
var latestUpd int
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-stop:
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
|
|
|
updates, err := b.getUpdates(latestUpd+1, p.Timeout)
|
|
|
|
updates, err := b.getUpdates(latestUpd+1, p.Timeout)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -79,4 +90,5 @@ func (p *LongPoller) Poll(b *Bot, dest chan Update) {
|
|
|
|
dest <- update
|
|
|
|
dest <- update
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|