-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
options.go
152 lines (133 loc) · 4.14 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
package telegram
import (
"context"
"io"
"time"
"github.com/cenkalti/backoff/v4"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/gotd/td/clock"
"github.com/gotd/td/internal/crypto"
"github.com/gotd/td/internal/exchange"
"github.com/gotd/td/internal/mtproto"
"github.com/gotd/td/internal/proto"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/tg"
)
type (
// PublicKey is a Telegram server public key.
PublicKey = exchange.PublicKey
)
// Options of Client.
type Options struct {
// PublicKeys of telegram.
//
// If not provided, embedded public keys will be used.
PublicKeys []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.
//
// Enabled by default if no UpdateHandler is provided.
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 wrapping tg.Invoker. Can be useful for metrics,
// tracing, etc. Note that order is important, see ExampleMiddleware.
//
// Middlewares are called in order from first to last.
Middlewares []Middleware
// AckBatchSize is limit of MTProto ACK buffer size.
AckBatchSize int
// AckInterval is maximum time to buffer MTProto ACK.
AckInterval time.Duration
// RetryInterval is duration between send retries.
RetryInterval time.Duration
// MaxRetries is limit of send retries.
MaxRetries int
// ExchangeTimeout is timeout of every key exchange request.
ExchangeTimeout time.Duration
// DialTimeout is timeout of creating connection.
DialTimeout time.Duration
// CompressThreshold is a threshold in bytes to determine that message
// is large enough to be compressed using GZIP.
// If < 0, compression will be disabled.
// If == 0, default value will be used.
CompressThreshold int
// Device is device config.
// Will be sent with session creation request.
Device DeviceConfig
MessageID mtproto.MessageIDSource
Clock clock.Clock
// OpenTelemetry.
TracerProvider trace.TracerProvider
}
func (opt *Options) setDefaults() {
if opt.Resolver == nil {
opt.Resolver = dcs.DefaultResolver()
}
if opt.Random == nil {
opt.Random = crypto.DefaultRand()
}
if opt.Logger == nil {
opt.Logger = zap.NewNop()
}
if opt.DC == 0 {
opt.DC = 2
}
if opt.DCList.Zero() {
opt.DCList = dcs.Prod()
}
// It's okay to use zero value AckBatchSize, mtproto.Options will set defaults.
// It's okay to use zero value AckInterval, mtproto.Options will set defaults.
// It's okay to use zero value RetryInterval, mtproto.Options will set defaults.
// It's okay to use zero value MaxRetries, mtproto.Options will set defaults.
// It's okay to use zero value CompressThreshold, mtproto.Options will set defaults.
opt.Device.SetDefaults()
if opt.Clock == nil {
opt.Clock = clock.System
}
if opt.ReconnectionBackoff == nil {
opt.ReconnectionBackoff = defaultBackoff(opt.Clock)
}
if opt.MigrationTimeout == 0 {
opt.MigrationTimeout = time.Second * 15
}
if opt.MessageID == nil {
opt.MessageID = proto.NewMessageIDGen(opt.Clock.Now)
}
if opt.UpdateHandler == nil {
// No updates handler passed, so no sense to subscribe for updates.
// User should explicitly ignore updates using custom UpdateHandler.
opt.NoUpdates = true
// Using no-op handler.
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
}
}