-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
options.go
120 lines (107 loc) · 2.73 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
package telegram
import (
"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"
)
// Transport is MTProto connection creator.
type Transport = dcs.Transport
// 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 []tg.DCOption
// 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
// AckBatchSize is maximum ack-s to buffer.
AckBatchSize int
// AckInterval is maximum time to buffer ack.
AckInterval time.Duration
RetryInterval time.Duration
MaxRetries 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.PlainResolver(dcs.PlainOptions{})
}
if opt.Random == nil {
opt.Random = rand.Reader
}
if opt.Logger == nil {
opt.Logger = zap.NewNop()
}
if opt.DC == 0 {
opt.DC = 2
}
if len(opt.DCList) == 0 {
opt.DCList = dcs.ProdDCs()
}
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
}
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, 100)
}
}
func defaultBackoff(c clock.Clock) func() backoff.BackOff {
return func() backoff.BackOff {
b := backoff.NewExponentialBackOff()
b.Clock = c
b.MaxElapsedTime = 0
return b
}
}