package telegram
import (
"context"
"go.opentelemetry.io/otel/attribute"
"github.com/gotd/log"
)
type ConnectionState int
const (
ConnectionStateConnecting ConnectionState = iota
ConnectionStateReady
ConnectionStateDisconnected
)
func (s ConnectionState ) String () string {
switch s {
case ConnectionStateConnecting :
return "connecting"
case ConnectionStateReady :
return "ready"
case ConnectionStateDisconnected :
return "disconnected"
default :
return "unknown"
}
}
type ConnKind int
const (
ConnKindPrimary ConnKind = iota
ConnKindData
ConnKindCDN
)
func (k ConnKind ) String () string {
switch k {
case ConnKindPrimary :
return "primary"
case ConnKindData :
return "data"
case ConnKindCDN :
return "cdn"
default :
return "unknown"
}
}
type ConnDeadEvent struct {
DC int
Kind ConnKind
Err error
}
type ConnectionStateEvent struct {
DC int
State ConnectionState
}
func (c *Client ) notifyConnectionState (s ConnectionState , dc int ) {
ctx := context .Background ()
c .meters ().connState .Add (ctx , 1 , dcAttrs (dc , attribute .String ("state" , s .String ())))
c .log .Debug (ctx , "Connection state" ,
log .String ("state" , s .String ()),
log .Int ("dc_id" , dc ),
)
if c .onConnectionState != nil {
c .onConnectionState (s )
}
if c .onConnectionStateEvent != nil {
c .onConnectionStateEvent (ConnectionStateEvent {DC : dc , State : s })
}
}
func (c *Client ) notifyConnDead (e ConnDeadEvent ) {
ctx := context .Background ()
c .meters ().connDead .Add (ctx , 1 , dcAttrs (e .DC , attribute .String ("kind" , e .Kind .String ())))
fields := []log .Attr {
log .String ("kind" , e .Kind .String ()),
log .Int ("dc_id" , e .DC ),
log .Error (e .Err ),
}
if e .Kind == ConnKindPrimary {
c .log .Warn (ctx , "Connection dead" , fields ...)
} else {
c .log .Debug (ctx , "Connection dead" , fields ...)
}
if c .onDead != nil {
c .onDead (e .Err )
}
if c .onConnDead != nil {
c .onConnDead (e )
}
}
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 .