package mtprotoimport ()func ( *Conn) ( context.Context, int64, int32, bin.Encoder) error {return .write(, false, , , )}// writeServiceMessage writes a service message. Its msg_id and seqno are// allocated after the exchange lock is taken, so a message that waited for a// key exchange is numbered in the new session.func ( *Conn) ( context.Context, bin.Encoder) error {return .write(, true, 0, 0, )}varbufPool = bin.NewPool(0)// slowWriteThreshold is the duration after which a single write (lock// acquisition + encrypt + transport send) is considered suspiciously slow and// logged at warning level. A blocked or slow write is the primary signature of// a half-open connection where updates are still received but requests can no// longer be issued.constslowWriteThreshold = 3 * time.Second// write encrypts and sends a message. If service is set, msgID and seqNo are// ignored and allocated under the exchange lock.func ( *Conn) ( context.Context, bool, int64, int32, bin.Encoder) error { := .clock.Now()// Grab shared lock for writing. // It prevents message sending during key regeneration if server forgot current auth key. // // Note: if key exchange holds the write lock (see createAuthKey), every // send — including pings, acks and content requests — blocks here. Tracing // the lock acquisition separately lets us distinguish "stuck on exchange // lock" from "stuck on transport send". .exchangeLock.RLock()defer .exchangeLock.RUnlock()if := .Err(); != nil {// Canceled while waiting for the lock, e.g. a retry of a request that // was reset with its session during key exchange.return }if { , = .nextMsgSeq(false) } elseif .beforeSession() {// The msg_id and seqno were allocated in a session that is gone: // sending them in the new one would break its seqno sequence.returnerrSessionReset } := .clock.Now()if := .Sub(); > slowWriteThreshold { .logWithTypeID(peekID()).Warn(, "Slow write: waited for exchange lock",log.Int64("msg_id", ),log.Int32("seq_no", ),log.Duration("waited", ), ) } := bufPool.Get()deferbufPool.Put()if := .newEncryptedMessage(, , , ); != nil {return } := .logWithTypeID(peekID()).With(log.Int64("msg_id", ),log.Int32("seq_no", ),log.Int("size_bytes", .Len()), ) .Debug(, "Sending message")if := .conn.Send(, ); != nil { .Debug(, "Send failed", log.Error(), log.Duration("elapsed", .clock.Now().Sub()))return }if := .clock.Now().Sub(); > slowWriteThreshold { .Warn(, "Slow write: transport send took too long", log.Duration("elapsed", )) }returnnil}// peekID returns the type id of an encodable message for logging, or 0 if it// cannot be determined without encoding.func ( bin.Encoder) uint32 {if , := .(interface{ () uint32 }); {return .() }return0}func ( *Conn) ( bool) ( int64, int32) { .reqMux.Lock()defer .reqMux.Unlock() = .newMessageID()// Computing current sequence number (seqno). // This should be serialized with new message id generation. // // See https://github.com/gotd/td/issues/245 for reference. = .sentContentMessages * 2if { ++ .sentContentMessages++ }return}// beforeSession reports whether msgID was allocated before the current// session was started by resetSession.func ( *Conn) ( int64) bool {return < .sessionStartMsgID.Load()}
The pages are generated with Goldsv0.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.