package telegram
import (
"context"
"fmt"
"strings"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/gotd/td/bin"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)
func (c *Client ) API () *tg .Client {
return c .tg
}
func (c *Client ) Invoke (ctx context .Context , input bin .Encoder , output bin .Decoder ) error {
if c .tracer != nil {
spanName := "Invoke"
var attrs []attribute .KeyValue
if t , ok := input .(interface { TypeID () uint32 }); ok {
id := t .TypeID ()
attrs = append (attrs ,
attribute .Int64 ("tg.method.id_int" , int64 (id )),
attribute .String ("tg.method.id" , fmt .Sprintf ("%x" , id )),
)
name := c .opts .Types .Get (id )
if name == "" {
name = fmt .Sprintf ("0x%x" , id )
} else {
attrs = append (attrs , attribute .String ("tg.method.name" , name ))
}
spanName = fmt .Sprintf ("Invoke: %s" , name )
}
spanCtx , span := c .tracer .Start (ctx , spanName ,
trace .WithAttributes (attrs ...),
trace .WithSpanKind (trace .SpanKindClient ),
)
ctx = spanCtx
defer span .End ()
}
return c .invoker .Invoke (ctx , input , output )
}
func (c *Client ) invokeDirect (ctx context .Context , input bin .Encoder , output bin .Decoder ) error {
if err := c .invokeConn (ctx , input , output ); err != nil {
if rpcErr , ok := tgerr .As (err ); ok && strings .HasSuffix (rpcErr .Type , "_MIGRATE" ) {
targetDC := rpcErr .Argument
log := c .log .With (
zap .String ("error_type" , rpcErr .Type ),
zap .Int ("target_dc" , targetDC ),
)
if rpcErr .IsOneOf ("FILE_MIGRATE" , "STATS_MIGRATE" ) {
log .Debug ("Invoking on target DC" )
return c .invokeSub (ctx , targetDC , input , output )
}
log .Info ("Migrating to target DC" )
return c .invokeMigrate (ctx , targetDC , input , output )
}
return err
}
return nil
}
func (c *Client ) invokeConn (ctx context .Context , input bin .Encoder , output bin .Decoder ) error {
c .connMux .Lock ()
conn := c .conn
c .connMux .Unlock ()
return conn .Invoke (ctx , input , output )
}
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 .