package telegram
import (
"context"
"io"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"go.opentelemetry.io/otel/trace"
"go.uber.org/atomic"
"go.uber.org/zap"
"github.com/gotd/td/bin"
"github.com/gotd/td/clock"
"github.com/gotd/td/internal/mtproto"
"github.com/gotd/td/internal/pool"
"github.com/gotd/td/internal/tdsync"
"github.com/gotd/td/oteltg"
"github.com/gotd/td/session"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/telegram/internal/manager"
"github.com/gotd/td/telegram/internal/version"
"github.com/gotd/td/tg"
)
type UpdateHandler interface {
Handle (ctx context .Context , u tg .UpdatesClass ) error
}
type UpdateHandlerFunc func (ctx context .Context , u tg .UpdatesClass ) error
func (f UpdateHandlerFunc ) Handle (ctx context .Context , u tg .UpdatesClass ) error {
return f (ctx , u )
}
type clientStorage interface {
Load (ctx context .Context ) (*session .Data , error )
Save (ctx context .Context , data *session .Data ) error
}
type clientConn interface {
Run (ctx context .Context ) error
Invoke (ctx context .Context , input bin .Encoder , output bin .Decoder ) error
Ping (ctx context .Context ) error
}
type Client struct {
migrationTimeout time .Duration
migration chan struct {}
tg *tg .Client
invoker tg .Invoker
mw []Middleware
device DeviceConfig
opts mtproto .Options
domains map [int ]string
testDC bool
session *pool .SyncSession
cfg *manager .AtomicConfig
conn clientConn
connMux sync .Mutex
create connConstructor
resolver dcs .Resolver
connBackoff func () backoff .BackOff
defaultMode manager .ConnMode
connsCounter atomic .Int64
restart chan struct {}
subConns map [int ]CloseInvoker
subConnsMux sync .Mutex
sessions map [int ]*pool .SyncSession
sessionsMux sync .Mutex
rand io .Reader
log *zap .Logger
clock clock .Clock
ctx context .Context
cancel context .CancelFunc
appID int
appHash string
storage clientStorage
ready *tdsync .ResetReady
updateHandler UpdateHandler
noUpdatesMode bool
tracer trace .Tracer
}
func NewClient (appID int , appHash string , opt Options ) *Client {
opt .setDefaults ()
mode := manager .ConnModeUpdates
if opt .NoUpdates {
mode = manager .ConnModeData
}
client := &Client {
rand : opt .Random ,
log : opt .Logger ,
appID : appID ,
appHash : appHash ,
updateHandler : opt .UpdateHandler ,
session : pool .NewSyncSession (pool .Session {
DC : opt .DC ,
}),
domains : opt .DCList .Domains ,
testDC : opt .DCList .Test ,
cfg : manager .NewAtomicConfig (tg .Config {
DCOptions : opt .DCList .Options ,
}),
create : defaultConstructor (),
resolver : opt .Resolver ,
defaultMode : mode ,
connBackoff : opt .ReconnectionBackoff ,
clock : opt .Clock ,
device : opt .Device ,
migrationTimeout : opt .MigrationTimeout ,
noUpdatesMode : opt .NoUpdates ,
mw : opt .Middlewares ,
}
if opt .TracerProvider != nil {
client .tracer = opt .TracerProvider .Tracer (oteltg .Name )
}
client .init ()
if v := version .GetVersion (); v != "" {
client .log = client .log .With (zap .String ("v" , v ))
}
if opt .SessionStorage != nil {
client .storage = &session .Loader {
Storage : opt .SessionStorage ,
}
}
client .opts = mtproto .Options {
PublicKeys : opt .PublicKeys ,
Random : opt .Random ,
Logger : opt .Logger ,
AckBatchSize : opt .AckBatchSize ,
AckInterval : opt .AckInterval ,
RetryInterval : opt .RetryInterval ,
MaxRetries : opt .MaxRetries ,
CompressThreshold : opt .CompressThreshold ,
MessageID : opt .MessageID ,
ExchangeTimeout : opt .ExchangeTimeout ,
DialTimeout : opt .DialTimeout ,
Clock : opt .Clock ,
Types : getTypesMapping (),
Tracer : client .tracer ,
}
client .conn = client .createPrimaryConn (nil )
return client
}
func (c *Client ) init () {
if c .domains == nil {
c .domains = map [int ]string {}
}
if c .cfg == nil {
c .cfg = manager .NewAtomicConfig (tg .Config {})
}
c .ready = tdsync .NewResetReady ()
c .restart = make (chan struct {})
c .migration = make (chan struct {}, 1 )
c .sessions = map [int ]*pool .SyncSession {}
c .subConns = map [int ]CloseInvoker {}
c .invoker = chainMiddlewares (InvokeFunc (c .invokeDirect ), c .mw ...)
c .tg = tg .NewClient (c .invoker )
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .