-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
options.go
131 lines (119 loc) · 3.18 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
package telegram
import (
"context"
"crypto/rand"
"crypto/rsa"
"io"
"time"
"github.com/cenkalti/backoff/v4"
"go.uber.org/zap"
"github.com/gotd/td/clock"
"github.com/gotd/td/internal/mtproto"
"github.com/gotd/td/internal/proto"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/tg"
)
// Options of Client.
type Options struct {
// PublicKeys of telegram.
//
// If not provided, embedded public keys will be used.
PublicKeys []*rsa.PublicKey
// DC ID to connect.
//
// If not provided, 2 will be used by default.
DC int
// DCList is initial list of addresses to connect.
DCList dcs.List
// Resolver to use.
Resolver dcs.Resolver
// NoUpdates enables no updates mode.
NoUpdates bool
// ReconnectionBackoff configures and returns reconnection backoff object.
ReconnectionBackoff func() backoff.BackOff
// MigrationTimeout configures migration timeout.
MigrationTimeout time.Duration
// Random is random source. Defaults to crypto.
Random io.Reader
// Logger is instance of zap.Logger. No logs by default.
Logger *zap.Logger
// SessionStorage will be used to load and save session data.
// NB: Very sensitive data, save with care.
SessionStorage SessionStorage
// UpdateHandler will be called on received update.
UpdateHandler UpdateHandler
// Middlewares list allows to wrap tg.Invoker. Can be useful for metrics,
// tracing, etc. Note that order is important.
Middlewares []Middleware
// AckBatchSize is maximum ack-s to buffer.
AckBatchSize int
// AckInterval is maximum time to buffer ack.
AckInterval time.Duration
RetryInterval time.Duration
MaxRetries int
// ReadConcurrency is a count of workers to decrypt and decode incoming messages.
// Should be more than 2 to make effect. Otherwise ignored.
ReadConcurrency int
// Device is device config.
// Will be sent with session creation request.
Device DeviceConfig
MessageID mtproto.MessageIDSource
Clock clock.Clock
}
func (opt *Options) setDefaults() {
if opt.Resolver == nil {
opt.Resolver = dcs.DefaultResolver()
}
if opt.Random == nil {
opt.Random = rand.Reader
}
if opt.Logger == nil {
opt.Logger = zap.NewNop()
}
if opt.DC == 0 {
opt.DC = 2
}
if opt.DCList.Zero() {
opt.DCList = dcs.Prod()
}
if opt.AckBatchSize == 0 {
opt.AckBatchSize = 20
}
if opt.AckInterval == 0 {
opt.AckInterval = time.Second * 15
}
if opt.RetryInterval == 0 {
opt.RetryInterval = time.Second * 5
}
if opt.MigrationTimeout == 0 {
opt.MigrationTimeout = time.Second * 15
}
if opt.MaxRetries == 0 {
opt.MaxRetries = 5
}
// Keep ReadConcurrency is zero, mtproto.Options will set default value.
opt.Device.SetDefaults()
if opt.Clock == nil {
opt.Clock = clock.System
}
if opt.ReconnectionBackoff == nil {
opt.ReconnectionBackoff = defaultBackoff(opt.Clock)
}
if opt.MessageID == nil {
opt.MessageID = proto.NewMessageIDGen(opt.Clock.Now)
}
if opt.UpdateHandler == nil {
// opt.NoUpdates = true
opt.UpdateHandler = UpdateHandlerFunc(func(ctx context.Context, u tg.UpdatesClass) error {
return nil
})
}
}
func defaultBackoff(c clock.Clock) func() backoff.BackOff {
return func() backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.Clock = c
b.MaxElapsedTime = 0
return b
}
}