Does RPC have a timeout mechanism?

前端 未结 4 617
情书的邮戳
情书的邮戳 2021-02-09 14:30

If RPC does not have a timeout mechanism, how do I \"kill\" an RPC call if it is trying to call an RPC method of a server that is closed?

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-09 15:21

    Or, anno now, someone might prefer to use context instead. This also takes care of returning a proper error when timed out. (context.DeadlineExceeded)

    import (
        "context"
        "log"
        "net/rpc"
    )
    
    type Client struct {
        *rpc.Client
    }
    
    // CallEx is a context aware wrapper around rpc's Client.Call()
    func (c *client) CallEx(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error {
        ec := make(chan error, 1)
        go func() {
            ec <- c.Call(serviceMethod, args, reply)
        }()
        select {
        case err := <-ec:
            return err
        case <-ctx.Done():
            return ctx.Err()
        }
    }
    

    Invoke this with a Deadlined context:

    type Args struct {
       A, B int
    }
    
    func main(){
        rpc, err := rpc.DialHTTP("tcp", "host")
        if err != nil {
            t.Fatal(err)
        }
        c := client{rpc}
    
        ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
        defer cancel()
    
        var i int
        if err := c.CallEx(ctx, "Calc.Multiply", Args{2, 2}, &i); err != nil {
            log.Fatal(err)
        }
    }
    

提交回复
热议问题