-
Notifications
You must be signed in to change notification settings - Fork 1
/
poller.go
50 lines (40 loc) · 890 Bytes
/
poller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package yagolangtbotapi
import (
"time"
"github.com/udev-21/ya-golang-tbot-api/method"
"github.com/udev-21/ya-golang-tbot-api/types"
)
type Poller interface {
Poll(b *BotAPI, dest chan types.Update, stop chan struct{})
}
type internalLongPoller struct {
updateCnf method.GetUpdates
}
func NewLongPoller() Poller {
return &internalLongPoller{}
}
// Poll does long polling.
func (p *internalLongPoller) Poll(b *BotAPI, dest chan types.Update, stop chan struct{}) {
for {
select {
case <-stop:
return
default:
}
updates, err := b.GetUpdates(&p.updateCnf)
if len(updates) == 0 {
time.Sleep(10 * time.Millisecond)
}
if err != nil {
writeLog(LogLevelError, b.logger, err.Error())
continue
}
if len(updates) > 0 {
for _, update := range updates {
p.updateCnf.Offset = update.UpdateID
dest <- update
}
p.updateCnf.Offset++
}
}
}