-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
migrate_to_dc.go
98 lines (83 loc) · 2.09 KB
/
migrate_to_dc.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
package telegram
import (
"context"
"go.uber.org/zap"
"golang.org/x/xerrors"
"github.com/gotd/td/bin"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/tg"
)
func (c *Client) ensureRestart(ctx context.Context, export *tg.AuthExportedAuthorization) error {
c.log.Debug("Triggering restart")
c.resetReady()
select {
case <-ctx.Done():
return ctx.Err()
case c.restart <- struct{}{}:
c.log.Debug("Restart initialized")
}
if export != nil {
select {
case <-ctx.Done():
return ctx.Err()
case c.exported <- export:
c.log.Debug("Sent export authorization")
}
}
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ready.Ready():
c.log.Info("Restart ensured")
return nil
}
}
func (c *Client) invokeMigrate(ctx context.Context, dcID int, input bin.Encoder, output bin.Decoder) error {
// Acquire or cancel.
select {
case c.migration <- struct{}{}:
case <-ctx.Done():
return ctx.Err()
}
// Release.
defer func() {
<-c.migration
}()
// Check if someone already migrated.
s := c.session.Load()
if s.DC == dcID {
return c.invokeConn(ctx, input, output)
}
mctx, cancel := context.WithTimeout(ctx, c.migrationTimeout)
defer cancel()
if err := c.migrateToDc(mctx, dcID,
// TODO(tdakkota): Is it may be necessary to transfer auth
// if error is not FILE_MIGRATE or STATS_MIGRATE?
false,
); err != nil {
return xerrors.Errorf("migrate to dc: %w", err)
}
// Re-trying request on another connection.
return c.invokeConn(ctx, input, output)
}
func (c *Client) migrateToDc(ctx context.Context, dcID int, transfer bool) error {
cfg := c.cfg.Load()
dcList := dcs.FindPrimaryDCs(cfg.DCOptions, dcID, false)
if len(dcList) == 0 {
return xerrors.Errorf("DC %d not found", dcID)
}
c.log.Info("Selected new DC from config",
zap.Int("dc_id", dcID),
zap.Int("candidates", len(dcList)),
)
var export *tg.AuthExportedAuthorization
if transfer {
exported, err := c.exportAuth(ctx, dcID)
if err != nil && !unauthorized(err) {
return xerrors.Errorf("export auth: %w", err)
}
export = exported
}
c.session.Migrate(dcID)
return c.ensureRestart(ctx, export)
}