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

Keepalive support #1992

Merged
merged 1 commit into from
Jul 9, 2016
Merged

Keepalive support #1992

merged 1 commit into from
Jul 9, 2016

Conversation

zsurocking
Copy link
Contributor

Creates a KeepAliveManager which should be used by each transport. It does keepalive pings and shuts down the transport if does not receive an response in time.
It prevents the connection being shut down for long lived streams. It could also detect broken socket in certain platforms.

@zsurocking
Copy link
Contributor Author

The integration part with OkhttpClientTransport is not done yet. I will do it in another commit or pull request.
But this one should be already complete for review.

}

public KeepAliveManager(ManagedClientTransport transport, ScheduledExecutorService scheduler) {
this(transport, scheduler, SYSTEM_TICKER, KEEPALIVE_DELAY, KEEPALIVE_TIMEOUT);
Copy link
Member

Choose a reason for hiding this comment

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

The keepalive delay must be configurable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added another constructor.

zhangkun83 added a commit to zhangkun83/grpc-java that referenced this pull request Jun 30, 2016
A timer that is optimized for being reset frequently, useful for grpc#1986
and grpc#1992.
@zsurocking
Copy link
Contributor Author

#1972

@zsurocking
Copy link
Contributor Author

Updated okhttp transport to use keepalive.

zhangkun83 added a commit to zhangkun83/grpc-java that referenced this pull request Jul 1, 2016
A timer that is optimized for being reset frequently, useful for grpc#1986
and grpc#1992.
// We don't have any active streams. No need to do keepalives any more.
// Again, we have to call this inside the lock to avoid the race between onTransportIdle
// and onTransportActive.
keepAliveManager.onTransportIdle();
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be done in maybeClearInUse() instead, before calling listener.transportInUse(false).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see both maybeClearInUse and setInUse actually consider pending streams. IIUC, when there's no connection, all streams will be pending streams. We probably don't want to do keepalives when there're only pending streams.

Copy link
Member

@ejona86 ejona86 Jul 8, 2016

Choose a reason for hiding this comment

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

Pending streams are a bit of an edge case. Pending streams used to happen before transportReady() (but that's no longer the case due to changes in TransportSet). Otherwise pendingStreams are only caused by MAX_CONCURRENT_STREAMS. The only case we could have pendingStreams but no active streams is when MAX_CONCURRENT_STREAMS is 0. It may be a good idea to do pings in that case, since if the connection goes down we'll be permanently hosed as we'll never do writes (which means we may never detect the closure).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Changed it to what Kun suggested.

@zhangkun83
Copy link
Contributor

I find ResettableTimer could significantly simplify the implementation, removing the explicit state-keeping. I wrote a POC.

@zhangkun83
Copy link
Contributor

@ejona86 is concerned about the complexity of ResetttableTimer. @carl-mastrangelo suggests that we periodically check the time of last activity, and schedule a ping if it was older than keepAliveDelay. Then we don't need to worry about the potential performance issue of frequently cancelling the timer.

/**
* Enable keepalive with default delay and timeout.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785")
Copy link
Member

Choose a reason for hiding this comment

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

No @ExperimentalApi for package-private classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@ejona86
Copy link
Member

ejona86 commented Jul 8, 2016

@zsurocking LGTM

Creates a KeepAliveManager which should be used by each transport. It does keepalive pings and shuts down the transport if does not receive an response in time.
It prevents the connection being shut down for long lived streams. It could also detect broken socket in certain platforms.
@zsurocking zsurocking merged commit d9001ca into grpc:master Jul 9, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants