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

Manage HTTP/2 connections and keep them alive with PING frames #673

Merged
merged 14 commits into from
Jun 13, 2023

Conversation

timostamm
Copy link
Member

@timostamm timostamm commented Jun 9, 2023

This PR adds support Basic Keepalive for HTTP/2 for clients that use one of the transports from @bufbuild/connect-node.

It replaces the option keepSessionAlive, which is deprecated with this PR.

In it's most simple form, the following example enables regular PINGs every 5 minutes:

import { createConnectTransport } from "@bufbuild/connect-node";

const transport = createConnectTransport({
  httpVersion: "2",
  baseUrl: "https://demo.connect.build",
  pingIntervalMs: 1000 * 60 * 5,
});

With the example above, PING frames are only sent for connections that have open streams (running RPCs). The following options are available to tweak the behavior depending on the requirements.

  • pingIntervalMs?: number
    The interval to send PING frames to keep a connection alive. The interval is reset whenever a stream receives data. If a PING frame is not responded to within pingTimeoutMs, the connection and all open streams close.

    By default, no PING frames are sent. If a value is provided, PING frames are sent only for connections that have open streams, unless pingIdleConnections is enabled.

    Sensible values can be between 10 seconds and 2 hours, depending on your infrastructure.

    This option is equivalent to GRPC_ARG_KEEPALIVE_TIME_MS in gRPC Core.

  • pingIdleConnection?: boolean
    Enable PING frames for connections that are have no open streams. This option is only effective if a value for pingIntervalMs is provided.

    Note that it may not be necessary to enable this option. If a request is made on a connection that has not been used for longer than pingIntervalMs, a PING frame is sent to verify that the connection is still alive, and a new connection is opened transparently if necessary.

    Defaults to false.

    This option is equivalent to GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS in gRPC Core.

  • pingTimeoutMs?: number
    Timeout for PING frames. If a PING is not answered within this time, the connection is considered dead.

    Defaults to 15 seconds. This option is only used if a value for pingIntervalMs is provided.

    This option is equivalent to GRPC_ARG_KEEPALIVE_TIME_MS in gRPC Core.

  • idleConnectionTimeoutMs?: number
    Automatically close a connection if the time since the last request stream exceeds this value.

    Defaults to 15 minutes.
    This option is equivalent to GRPC_ARG_CLIENT_IDLE_TIMEOUT_MS of gRPC core.

Sessions and keep-alive are managed by the class Http2SessionManager from @bufbuild/connect-node. This class can be used to gain control over the session used by the transport by creating it ahead of time, and passing the instance to the transport constructor function. The following example shows how to explicitly close a HTTP/2 connection:

import { createPromiseClient } from "@bufbuild/connect";
import { createConnectTransport, Http2SessionManager } from "@bufbuild/connect-node";

// create a client, 
const sessionManager = new Http2SessionManager("https://demo.connect.build");
const transport = createConnectTransport({
  httpVersion: "2",
  baseUrl: "https://demo.connect.build",
  sessionManager,
});
const client = createPromiseClient(..., transport);

// make calls with the client

// close the connection
sessionManager.abort();

@timostamm timostamm linked an issue Jun 9, 2023 that may be closed by this pull request
@timostamm timostamm requested a review from smaye81 June 12, 2023 23:23
@timostamm timostamm marked this pull request as ready for review June 12, 2023 23:23
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.

Keep HTTP/2 connections alive with ping frames
2 participants