Note:
Note:
kerrors.ErrRPCTimeout
is returned;Specified when initializing client:
import "github.com/cloudwego/kitex/client"
cli, err := xxx.NewClient(targetService,
client.WithConnectTimeout(100 * time.Millisecond),
client.WithRPCTimeout(2 * time.Second))
Note: The two configuration items can be specified independently as needed.
Specified when sending the request:
import "github.com/cloudwego/kitex/client/callopt"
rsp, err := cli.YourMethod(ctx, req,
callopt.WithConnectTimeout(100 * time.Millisecond))
callopt.WithRPCTimeout(2 * time.Second))
Note: The two configuration items can be specified independently as needed.
Applicable to scenarios that require dynamic configuration. Before each request, the Client will call the `TimeoutProvider`` specified to obtain RPCTimeout and ConnectionTimeout.
Specify a user-defined rpcinfo.TimeoutProvider
when initializing the client:
import (
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/rpcinfo"
)
type UserTimeoutProvider struct {}
func (UserTimeoutProvider) Timeouts(ri rpcinfo.RPCInfo) rpcinfo.Timeouts {
// Should return RPCTimeout、ConnectTimeout
// ReadWriteTimeout is not used; recommended to have the same value as RPCTimeout
}
opt := client.WithTimeoutProvider(&UserTimeoutProvider{})
cli, err := xxx.NewClient(targetService, opt)
Available extension(s):
When a request times out, the error
received by the Client will be:
&kerrors.DetailedError {
basic: kerrors.ErrRPCTimeout,
cause: errors.New("timeout=100ms, to=ServerName, method=MethodName, location=kitex.rpcTimeoutMW, remote=0.0.0.0:8888"),
}
kerrors.IsTimeoutError(err)
can be used to check whether it’s a timeout error.
Applicable to github.com/cloudwego/kitex >= v0.5.2
By default, reasons of ErrRPCTimeout may be:
In some scenarios, it’s necessary to distinguish these reasons, such as issuing multiple requests at the same time expecting only one request to be successful, and cancelling other requests. However, other requests cancelled are not regarded as RPC or business error, and should be filtered to avoid unwanted alarms.
Considering compatibility, this configuration is disabled by default and needs to be set by code:
import "github.com/cloudwego/kitex/pkg/rpctimeout"
rpctimeout.EnableGlobalNeedFineGrainedErrCode()
The difference before and after enabling is listed in the following table:
Scenarios | Disabled | Enabled |
---|---|---|
Timeout set by kitex | kerrors.ErrRPCTimeout | kerrors.ErrRPCTimeout |
Cancel called by business code | (same as above) | kerrors.ErrCanceledByBusiness |
Timeout set by business code (earlier than timeout set by kitex) | (same as above) | kerrors.ErrTimeoutByBusiness (*NOTE) |
*NOTE:Considering the fact that the timer of go language may not be accurate all the time, when a timeout occurs, it checks whether actualDDL + 50ms < kitex’s DDL, and only returns the error code if the condition is met, otherwise it still returns 103.
For example, Kitex sets a 1s timeout:
The threshold (50ms) can be modified under Kitex >= 0.7.1 by:
rpctimeout.SetBusinessTimeoutThreshold(10 * time.Millisecond)
NOTE:
For example, on the client side, request is sent in multiple segments. If the interval between two segments is too long, it will trigger a server-side read waiting timeout. Upon such a case, try increasing the ReadWriteTimeout.
NOTE:
github.com/cloudwego/kitex >= v0.9.0
(For non-streaming API) If timeout is available in the request’s TTHeader, set it into server request ctx.
For detailed usage, please refer to the example code below.
NOTE:
ctx.Deadline()
is sent via header grpc-timeout
to server which will be added to the server request’s ctxWithRPCTimeout
optionrpcinfo.GetRPCInfo(ctx).Config().RPCTimeout()
Specified when initializing a server:
import "github.com/cloudwego/kitex/server"
svr := xxx.NewServer(handler,
server.WithReadWriteTimeout(5 * time.Second),
)
Specified when initializing a server:
import "github.com/cloudwego/kitex/server"
svr := yourservice.NewServer(handler,
server.WithExitWaitTime(5 * time.Second),
)
This Option should be used with TTHeader; please refer to the example code below.
Client
cli := yourservice.MustNewClient(
serverName,
client.WithTransportProtocol(transport.TTHeader),
client.WithMetaHandler(transmeta.ClientTTHeaderHandler),
client.WithRPCTimeout(time.Second),
)
Server
svr := yourservice.NewServer(handler,
server.WithMetaHandler(transmeta.ServerTTHeaderHandler),
server.WithEnableContextTimeout(true),
)
This is the timeout time waiting for the packet during server-side decoding, and has nothing to do with the execution of the server handler (decoding has already been completed when the server handler is executed).
Currently, the server side does not support handler execution timeout.