package telegram
import (
"context"
"github.com/go-faster/errors"
"go.opentelemetry.io/otel/attribute"
"github.com/gotd/log"
"github.com/gotd/td/bin"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)
const defaultFilePoolSize = 4
func (c *Client ) FileDC (ctx context .Context , dc int ) (tg .Invoker , error ) {
c .fileInvokersMux .Lock ()
inv , ok := c .fileInvokers [dc ]
c .fileInvokersMux .Unlock ()
if ok {
return inv , nil
}
if _ , err := c .cachedDC (ctx , c .filePools , dc ); err != nil {
return nil , errors .Wrapf (err , "create pool to DC %d" , dc )
}
inv = chainMiddlewares (fileDCInvoker {client : c , dc : dc }, c .mw ...)
c .fileInvokersMux .Lock ()
if existing , ok := c .fileInvokers [dc ]; ok {
inv = existing
} else {
c .fileInvokers [dc ] = inv
}
c .fileInvokersMux .Unlock ()
return inv , nil
}
type fileDCInvoker struct {
client *Client
dc int
}
const maxFileMigrations = 2
func (f fileDCInvoker ) Invoke (ctx context .Context , input bin .Encoder , output bin .Decoder ) error {
dc := f .dc
for hop := 0 ; ; hop ++ {
err := f .client .invokeCachedDC (ctx , f .client .filePools , dc , input , output )
rpcErr , ok := tgerr .AsType (err , "FILE_MIGRATE" )
if !ok || hop >= maxFileMigrations || rpcErr .Argument == dc {
return err
}
f .client .log .Debug (ctx , "Following FILE_MIGRATE" ,
log .Int ("from_dc" , dc ),
log .Int ("dc_id" , rpcErr .Argument ),
log .Int ("hop" , hop +1 ),
)
f .client .meters ().fileMigrations .Add (ctx , 1 , dcAttrs (rpcErr .Argument ,
attribute .String ("source" , "file" ),
))
dc = rpcErr .Argument
}
}
The pages are generated with Golds v0.8.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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds .