-
Notifications
You must be signed in to change notification settings - Fork 0
/
polling.go
134 lines (106 loc) · 2.62 KB
/
polling.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package tg
import (
"context"
"log"
"sync"
"time"
)
type Logger interface {
Printf(format string, args ...interface{})
}
type ErrorHandler func(ctx context.Context, update *Update, err error)
// Poller listen to updates using long polling.
type Poller struct {
// Client represents Telegram Bot API client.
Client *Client
// Handler represents Update handler.
Handler Handler
// Options contains options using for polling.
Options PollingOptions
// ErrorLogger used for logging polling errors.
ErrorLogger Logger
// ErrorHandler called ever
ErrorHandler ErrorHandler
// HandlerTimeout define time to process update.
HandlerTimeout time.Duration
// Delay before next request if error happened.
RetryAfter time.Duration
wg sync.WaitGroup
}
func (poller *Poller) onPollingError(ctx context.Context, err error) {
format := "polling error: %#v, retry after %s"
args := []interface{}{
err,
poller.RetryAfter,
}
if ctx.Err() == context.Canceled {
return
}
if poller.ErrorLogger == nil {
log.Printf(format, args...)
} else {
poller.ErrorLogger.Printf(format, args...)
}
}
func (poller *Poller) onHandlerError(ctx context.Context, update *Update, err error) {
if poller.ErrorHandler != nil {
poller.ErrorHandler(ctx, update, err)
} else {
log.Printf("handler error: %#v", err)
}
}
func (poller *Poller) processUpdates(ctx context.Context, updates []*Update) {
if poller.Handler == nil {
return
}
for _, update := range updates {
update := update
poller.wg.Add(1)
go func() {
defer poller.wg.Done()
handlerCtx := ctx
if poller.HandlerTimeout != 0 {
var cancel context.CancelFunc
handlerCtx, cancel = context.WithTimeout(ctx, poller.HandlerTimeout)
defer cancel()
}
if err := poller.Handler.HandleUpdate(handlerCtx, update); err != nil {
poller.onHandlerError(ctx, update, err)
}
}()
}
}
func (poller *Poller) getNextOffset(updates []*Update) UpdateID {
last := updates[len(updates)-1]
return last.ID + 1
}
// Run polling until stopped.
func (poller *Poller) Run(ctx context.Context) {
opts := poller.Options
for {
select {
case <-ctx.Done():
poller.wg.Wait()
return
default:
updates, err := poller.Client.GetUpdates(ctx, &opts)
if err != nil {
poller.onPollingError(ctx, err)
continue
}
if len(updates) > 0 {
opts.Offset = poller.getNextOffset(updates)
go poller.processUpdates(ctx, updates)
}
}
}
}
// Polling updates.
func Polling(ctx context.Context, client *Client, handler Handler) {
poller := Poller{
Client: client,
Handler: handler,
HandlerTimeout: time.Second * 3,
}
poller.Run(ctx)
}