Does RPC have a timeout mechanism?

前端 未结 4 614
情书的邮戳
情书的邮戳 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:09

    if you want to implement a timeout (to prevent a call from taking too long), then you'll want to change rpc.Dial for net.DialTimeout (notice they're separate packages: rpc vs net). Also be aware that the returned type isn't a client any more (as it is in the previous example); instead it is a 'connection'.

      conn, err := net.DialTimeout("tcp", "localhost:8080", time.Minute)
      if err != nil {
        log.Fatal("dialing:", err)
      }
    
      client := rpc.NewClient(conn)
    

提交回复
热议问题