package rpcimport ()// Engine handles RPC requests.typeEnginestruct {sendSenddropDropHandlermuxsync.Mutexrpcmap[int64]func(*bin.Buffer, error) errorackmap[int64]chanstruct{}clockclock.Clockloglog.HelperretryIntervaltime.DurationmaxRetriesint// Canceling pending requests in ForceClose.reqCtxcontext.ContextreqCancelcontext.CancelCauseFuncwgsync.WaitGroup// closed is guarded by mux: it must be set and observed atomically with // respect to wg.Add in Do, otherwise Close may observe a zero WaitGroup // counter and return from Wait concurrently with a new Add.closedbool}// New creates new rpc Engine.func ( Send, Options) *Engine { .setDefaults() := log.For(.Logger) .Info(context.Background(), "Initialized",log.Duration("retry_interval", .RetryInterval),log.Int("max_retries", .MaxRetries), ) , := context.WithCancelCause(context.Background())return &Engine{rpc: map[int64]func(*bin.Buffer, error) error{},ack: map[int64]chanstruct{}{},send: ,drop: .DropHandler,log: ,maxRetries: .MaxRetries,retryInterval: .RetryInterval,clock: .Clock,reqCtx: ,reqCancel: , }}// Request represents client RPC request.typeRequeststruct {MsgIDint64SeqNoint32Inputbin.EncoderOutputbin.Decoder}// Do sends request to server and blocks until response is received, performing// multiple retries if needed.func ( *Engine) ( context.Context, Request) error {// Register the request under the mutex so Close cannot observe a zero // WaitGroup counter and return from Wait while we are about to Add. .mux.Lock()if .closed { .mux.Unlock()returnErrEngineClosed } .wg.Add(1) .mux.Unlock()defer .wg.Done() , := context.WithCancel()defer () := .log.With(log.Int64("msg_id", .MsgID)) .Debug(, "Do called") := make(chanstruct{})var (// Handler result.error// Needed to prevent multiple handler calls.uint32 ) := func( *bin.Buffer, error) error { .Debug(, "Handler called")if := atomic.CompareAndSwapUint32(&, 0, 1); ! { .Warn(, "Handler already called")returnerrors.New("handler already called") }defer ()deferclose()if != nil { = returnnil } = .Output.Decode()return }// Setting callback that will be called if message is received. .mux.Lock() .rpc[.MsgID] = .mux.Unlock()deferfunc() {// Ensuring that callback can't be called after function return. .mux.Lock()delete(.rpc, .MsgID) .mux.Unlock() }()// Start retrying. , := .retryUntilAck(, )var *RetryLimitReachedErrswitch {caseerrors.As(, &):// The server did not acknowledge the request after all resends, but // the connection is alive (a dead one is closed by pings and closes // the engine). The server may still be processing it: a slow // updates.getChannelDifference was answered 30–60 s later in // production. Like TDLib and Telegram Desktop, stop resending and // keep waiting for the result, bounded by ctx and engine close, // instead of failing a request the server answers. .Warn(, "Request not acknowledged after retries, waiting for the result",log.Int("retries", .Retries), ) = truecase != nil && !errors.Is(, .Err()):// If the retryCtx was canceled, then one of two things happened: // 1. User canceled the parent context. // 2. The RPC result came and callback canceled retryCtx. // // If this is not a Context’s error, we could not write data to the // connection, so we return an error.returnerrors.Wrap(, "retryUntilAck") } .Debug(, "Acknowledged, waiting for result", log.Bool("sent", ))select {case<-.Done(): .Debug(, "Context done before result", log.Bool("sent", ), log.Error(.Err()))if ! {return .Err() }// Set nop callback because server will respond with 'RpcDropAnswer' instead of expected result. // // NOTE(ccln): We can decode 'RpcDropAnswer' here but I see no reason to do this // because it will also come as a response to 'RPCDropAnswerRequest'. // // https://core.telegram.org/mtproto/service_messages#cancellation-of-an-rpc-query .mux.Lock() .rpc[.MsgID] = func( *bin.Buffer, error) error { returnnil } .mux.Unlock()if := .drop(); != nil { .Info(, "Failed to drop request", log.Error())return .Err() } .Debug(, "Request dropped")return .Err()case<-.reqCtx.Done():select {case<-:// Result arrived concurrently with close, prefer it.returndefault: }// Request was acknowledged by the server, but the response was not // received before close: the server may have already processed it, // so resending is not safe. Report plain context error, callers // should not retry transparently. .Debug(, "Engine closed while waiting for result")returnerrors.Wrap(.reqCtx.Err(), "engine forcibly closed")case<-: .Debug(, "Result received", log.Error())return }}// retryUntilAck resends the request to the server until request is// acknowledged.//// Returns nil if acknowledge was received or error otherwise.func ( *Engine) ( context.Context, Request) ( bool, error) { , := context.WithCancel()defer ()var ( = .waitAck(.MsgID) = 0 = .log.Named("retry").With(log.Int64("msg_id", .MsgID)) )defer .removeAck(.MsgID)// Encoding request.if := .send(, .MsgID, .SeqNo, .Input); != nil {returnfalse, errors.Wrap(, "send") } := func() error { := .clock.Timer(.retryInterval)deferclock.StopTimer()for {select {case<-.Done():return .Err()case<-.reqCtx.Done():select {case<-:// Acknowledge arrived concurrently with close, prefer it. .Debug(, "Acknowledged")returnnildefault: }// Request was sent, but not yet acknowledged: per MTProto it is // safe to resend it on a new connection, so report ErrEngineClosed // (the cancellation cause) to let callers retry.returnerrors.Wrap(context.Cause(.reqCtx), "engine forcibly closed")case<-: .Debug(, "Acknowledged")returnnilcase<-.C(): .Reset(.retryInterval) .Debug(, "Acknowledge timed out, performing retry")if := .send(, .MsgID, .SeqNo, .Input); != nil {iferrors.Is(, context.Canceled) {returnnil } .Error(, "Retry failed", log.Error())return } ++if >= .maxRetries { .Debug(, "Retry limit reached")return &RetryLimitReachedErr{Retries: , } } } } }returntrue, ()}// NotifyResult notifies engine about received RPC response.func ( *Engine) ( int64, *bin.Buffer) error { .mux.Lock() , := .rpc[] := len(.rpc) .mux.Unlock()if ! {// Result arrived but no caller is waiting for it: the request likely // already timed out, was dropped, or the connection was replaced. .log.Warn(context.Background(), "rpc callback not set (result for unknown/expired request)",log.Int64("msg_id", ),log.Int("pending", ), )returnnil }return (, nil)}// NotifyErrorAll notifies every pending request about rpcErr, e.g. when the// session they were sent in is gone and they must be sent again in a new one.func ( *Engine) ( error) { .mux.Lock() := make([]func(*bin.Buffer, error) error, 0, len(.rpc))for , := range .rpc { = append(, ) } .mux.Unlock()for , := range {// Callback with rpcError always return nil. _ = (nil, ) }}// NotifyErrorBefore notifies pending requests with msg_id less than msgID// about rpcErr, e.g. requests sent in a server session that was replaced// (new_session_created with first_msg_id).func ( *Engine) ( int64, error) { .mux.Lock()var []func(*bin.Buffer, error) errorfor , := range .rpc {if < { = append(, ) } } .mux.Unlock()for , := range {// Callback with rpcError always return nil. _ = (nil, ) }}// NotifyError notifies engine about received RPC error.func ( *Engine) ( int64, error) { .mux.Lock() , := .rpc[] .mux.Unlock()if ! { .log.Warn(context.Background(), "rpc callback not set", log.Int64("msg_id", ))return }// Callback with rpcError always return nil. _ = (nil, )}// Close gracefully closes the engine.// All pending requests will be awaited.// All Do method calls of closed engine will return ErrEngineClosed error.func ( *Engine) () { .mux.Lock() .closed = true .mux.Unlock() .log.Info(context.Background(), "Close called") .wg.Wait()}// ForceClose forcibly closes the engine.// All pending requests will be canceled.// All Do method calls of closed engine will return ErrEngineClosed error.func ( *Engine) () { .reqCancel(ErrEngineClosed) .Close()}
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.