Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kwild,rpc: db and rpc timeout settings #736

Merged
merged 3 commits into from
May 16, 2024

Conversation

jchappelow
Copy link
Member

@jchappelow jchappelow commented May 14, 2024

WIP: looks fine, but need to test e2e.

This adds two timeouts with settings to kwild:

  • app.rpc_timeout -- timeout for the duration of request handling for both gRPC (and thus http gateway) and JSON-RPC servers enforce the timeout. This is a server timeout.
  • app.db_read_timeout -- timeout on read-only DB actions (actually engine, but using the read-only DB tx). This is a service timeout applied only in Call and Query methods of the user service.

For gRCP, rpc_timeout uses a unary interceptor that cancels the context given to the service method. If I missed an existing timeout feature in the grpc packages, please let me know. I feel like this should have already existed.

For JSON-RPC, I thought this was handled by http.Server.WriteTimeout, but shockingly that timeout only closes the connection without cancelling the http.Request's context. This change employs the http.TimeoutHandler in the standard library to do this safely without concurrent writes to the http.ResponseWriter.

@jchappelow
Copy link
Member Author

jchappelow commented May 14, 2024

Resolves #717 , although this resolution is not DB specific. The timeout is applied at the level of the RPC service request. The linked issue suggests that solution is preferred, and I think so too.

@jchappelow jchappelow linked an issue May 14, 2024 that may be closed by this pull request
jsonrpc: timeout

kwild,jsonrpc: use RPC timeout setting
@jchappelow
Copy link
Member Author

jchappelow commented May 15, 2024

Added the service-level DB read timeout (db_read_timeout) that is just like to the previous read_timeout setting.

The RPC servers themselves also have a rpc_timeout setting, which is longer, applies to all methods handled by the server, and is necessary to deal with slow clients that either cannot send or receive fast enough and may be maliciously trying to consumer resources. The JSON server already had quite long defaults for these; I have just exposed a setting and ensured that the timeout also cancels the handler's context.

@@ -555,6 +557,8 @@ func DefaultConfig() *KwildConfig {
DBPort: "5432", // ignored with unix socket, but applies if IP used for DBHost
DBUser: "kwild",
DBName: "kwild",
RPCTimeout: Duration(45 * time.Second),
ReadTxTimeout: Duration(5 * time.Second),
Copy link
Member Author

@jchappelow jchappelow May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was very surprised to find that the default for the db read timeout was 100ms on v0.6.x. Isn't that extremely short? Shortest I would consider is probably 500ms-2s

Also note that the server timeout is separate, much longer, applies to all requests, and is primarily a defensive mechanism for the server.

@jchappelow jchappelow changed the title kwild,rpc: timeout setting kwild,rpc: db and rpc timeout settings May 15, 2024
@brennanjl brennanjl merged commit 4e0962a into kwilteam:main May 16, 2024
2 checks passed
@jchappelow jchappelow added this to the v0.7.8 milestone May 16, 2024
@jchappelow
Copy link
Member Author

jchappelow commented May 16, 2024

Regarding the gRPC request timeout, it really does look like you have to make a unary interceptor like this for per-request timeout: grpc/grpc-go#5059
@Yaiba ^

@Yaiba
Copy link
Contributor

Yaiba commented May 16, 2024

Regarding the gRPC request timeout, it really does look like you have to make a unary interceptor like this for per-request timeout: grpc/grpc-go#5059 @Yaiba ^

yeah, now I see what you mean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add read-timeout to v0.7+
3 participants