package mtproto
import (
"context"
"time"
"github.com/gotd/log"
"github.com/gotd/td/bin"
)
func (c *Conn ) writeContentMessage (ctx context .Context , msgID int64 , seqNo int32 , message bin .Encoder ) error {
return c .write (ctx , msgID , seqNo , message )
}
func (c *Conn ) writeServiceMessage (ctx context .Context , message bin .Encoder ) error {
msgID , seqNo := c .nextMsgSeq (false )
return c .write (ctx , msgID , seqNo , message )
}
var bufPool = bin .NewPool (0 )
const slowWriteThreshold = 3 * time .Second
func (c *Conn ) write (ctx context .Context , msgID int64 , seqNo int32 , message bin .Encoder ) error {
start := c .clock .Now ()
c .exchangeLock .RLock ()
defer c .exchangeLock .RUnlock ()
locked := c .clock .Now ()
if waited := locked .Sub (start ); waited > slowWriteThreshold {
c .logWithTypeID (peekID (message )).Warn (ctx , "Slow write: waited for exchange lock" ,
log .Int64 ("msg_id" , msgID ),
log .Int32 ("seq_no" , seqNo ),
log .Duration ("waited" , waited ),
)
}
b := bufPool .Get ()
defer bufPool .Put (b )
if err := c .newEncryptedMessage (msgID , seqNo , message , b ); err != nil {
return err
}
logger := c .logWithTypeID (peekID (message )).With (
log .Int64 ("msg_id" , msgID ),
log .Int32 ("seq_no" , seqNo ),
log .Int ("size_bytes" , b .Len ()),
)
logger .Debug (ctx , "Sending message" )
if err := c .conn .Send (ctx , b ); err != nil {
logger .Debug (ctx , "Send failed" , log .Error (err ), log .Duration ("elapsed" , c .clock .Now ().Sub (locked )))
return err
}
if elapsed := c .clock .Now ().Sub (locked ); elapsed > slowWriteThreshold {
logger .Warn (ctx , "Slow write: transport send took too long" , log .Duration ("elapsed" , elapsed ))
}
return nil
}
func peekID (message bin .Encoder ) uint32 {
if t , ok := message .(interface { TypeID () uint32 }); ok {
return t .TypeID ()
}
return 0
}
func (c *Conn ) nextMsgSeq (content bool ) (msgID int64 , seqNo int32 ) {
c .reqMux .Lock ()
defer c .reqMux .Unlock ()
msgID = c .newMessageID ()
seqNo = c .sentContentMessages * 2
if content {
seqNo ++
c .sentContentMessages ++
}
return
}
The pages are generated with Golds v0.8.4 . (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 .