-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
181 lines (144 loc) · 3.85 KB
/
options.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package tgbot
import (
"context"
"fmt"
"runtime"
"runtime/debug"
"time"
)
// UpdatesHandler handler another update.
type UpdatesHandler func(ctx *Context)
// Handler command handler.
type Handler func(ctx *Context) error
// ErrHandler error handler.
type ErrHandler func(err error)
// PanicHandler is panic handler.
type PanicHandler func(*Context, interface{})
type options struct {
ctx context.Context
// timeout is context timeout.
timeout time.Duration
// disableAutoSetupCommands whether automatically set up commands.
disableAutoSetupCommands bool
disableHandleAllUpdateOnStop bool
undefinedCommandHandler Handler
errHandler ErrHandler
updatesHandler UpdatesHandler
panicHandler PanicHandler
// pollUpdatesErrorHandler is the handler that is called when an error occurs in the polling updates.
pollUpdatesErrorHandler ErrHandler
workersNum int
workersPool Pool
// bufSize is updateC chan buffer size.
bufSize int
updateTimeout int
limit int
offset int
allowedUpdates []string
}
func newOptions(opts ...Option) *options {
o := &options{
ctx: context.Background(),
errHandler: func(err error) {},
workersNum: runtime.GOMAXPROCS(0),
updateTimeout: 50, // 50s is maximum timeout.
limit: 100,
}
o.panicHandler = func(ctx *Context, v interface{}) {
o.errHandler(fmt.Errorf("tgbot panic: %v, stack: %s", v, debug.Stack()))
}
o.pollUpdatesErrorHandler = func(err error) {
o.errHandler(fmt.Errorf("failed to get updates, error: %w", err))
time.Sleep(3 * time.Second)
}
for _, opt := range opts {
opt(o)
}
return o
}
type Option func(o *options)
// WithContext with the context.
func WithContext(ctx context.Context) Option {
return func(o *options) {
o.ctx = ctx
}
}
// WithTimeout set context timeout.
func WithTimeout(d time.Duration) Option {
return func(o *options) {
o.timeout = d
}
}
// WithWorkersNum set the number of workers to process updates.
func WithWorkersNum(n int) Option {
return func(o *options) {
o.workersNum = n
}
}
// WithWorkersPool set the worker pool for execute handler if the workersPool is non-nil.
func WithWorkersPool(p Pool) Option {
return func(o *options) {
o.workersPool = p
}
}
// WithUndefinedCmdHandler set how to handle undefined commands.
func WithUndefinedCmdHandler(h Handler) Option {
return func(o *options) {
o.undefinedCommandHandler = h
}
}
// WithErrorHandler set error handler.
func WithErrorHandler(h ErrHandler) Option {
return func(o *options) {
o.errHandler = h
}
}
// WithDisableAutoSetupCommands disable auto setup telegram commands.
func WithDisableAutoSetupCommands(v bool) Option {
return func(o *options) {
o.disableAutoSetupCommands = v
}
}
// WithDisableHandleAllUpdateOnStop disable handle all updates on stop.
func WithDisableHandleAllUpdateOnStop(v bool) Option {
return func(o *options) {
o.disableHandleAllUpdateOnStop = v
}
}
// WithBufferSize set the buffer size for receive updates.
func WithBufferSize(size int) Option {
return func(o *options) {
o.bufSize = size
}
}
// WithUpdatesHandler set the updates handler.
func WithUpdatesHandler(handler UpdatesHandler) Option {
return func(o *options) {
o.updatesHandler = handler
}
}
// WithPanicHandler set panic handler.
func WithPanicHandler(h PanicHandler) Option {
return func(o *options) {
o.panicHandler = h
}
}
// WithGetUpdatesTimeout set the get updates updateTimeout,
// timeout unit is seconds, max is 50 second.
func WithGetUpdatesTimeout(timeout int) Option {
return func(o *options) {
o.updateTimeout = timeout
}
}
// WithGetUpdatesLimit set the get updates limit.
func WithGetUpdatesLimit(limit int) Option {
return func(o *options) {
o.limit = limit
}
}
// WithGetUpdatesAllowedUpdates set allowed updates.
func WithGetUpdatesAllowedUpdates(v ...string) Option {
return func(o *options) {
o.allowedUpdates = v
}
}