Timeouts

Kitex supports RPC timeout and connection timeout, both of which support client-level and invocation-level configurations.

RPC Timeout

  1. You can specify RPC timeout in client initialization, it will works for all RPC started by this client by default.
import "github.com/cloudwego/kitex/client"
...
rpcTimeout := client.WithRPCTimeout(3*time.Second)
client, err := echo.NewClient("echo", rpcTimeout)
if err != nil {
	log.Fatal(err)
}
  1. And you can also specify timeout for a specific RPC call.
import "github.com/cloudwego/kitex/client/callopt"
...
rpcTimeout := callopt.WithRPCTimeout(3*time.Second)
resp, err := client.Echo(context.Background(), req, rpcTimeout)
if err != nil {
	log.Fatal(err)
}

Connection Timeout

  1. You can specify connection timeout in client initialization, it will works for all RPC started by this client by default.
import "github.com/cloudwego/kitex/client"
...
connTimeout := client.WithConnectTimeout(50*time.Millisecond)
client, err := echo.NewClient("echo", connTimeout)
if err != nil {
	log.Fatal(err)
}
  1. And you can also specify timeout for a specific RPC call.
import "github.com/cloudwego/kitex/client/callopt"
...
connTimeout := callopt.WithConnectTimeout(50*time.Millisecond)
resp, err := client.Echo(context.Background(), req, connTimeout)
if err != nil {
	log.Fatal(err)
}

Last modified April 4, 2023 : optimzie: custom_tpl docs (#597) (b3e25f4)