Skip to content

Add automatic stale connection cleanup#40

Merged
pushkarnk merged 3 commits intomasterfrom
timeout
Jul 30, 2019
Merged

Add automatic stale connection cleanup#40
pushkarnk merged 3 commits intomasterfrom
timeout

Conversation

@pushkarnk
Copy link
Copy Markdown
Contributor

@pushkarnk pushkarnk commented Jul 17, 2019

Motivation:

On Kitura-WebSocket Kitura/Kitura-WebSocket#48
introduces an automatic connection cleanup feature. Using this,
WebSocketServices can configure an optional timeout value. If a connection
remains idle for half of the configured timeout value, the server sends a
heartbeat ping to the client. In a following time period, again equal to
half of the configured timeout value, if a pong is received on the connection,
the latter is kept alive. If not, the connection is closed.

In Kitura-WebSocket-NIO, we implement this using the IdleStateHandler which
is configured with half of the configured timeout value. When we receive the
first IdleStateEvent, we send a heartbeat ping to the client. If a pong is
received before the next IdleStateEvent, the connection is kept alive. If the
next IdleStateEvent is received before the pong, the connection is closed.

This commit also includes a new client framework for testing. It is difficult
to write the connection cleanup tests using the existing adhoc WebSocket client
framework implemented in KituraTest.swift and KituraTest+Frames.swift. The
new client framework is called WebSocketClient. It uses a callback model and
is largely work-in-progress. We must move all of the existing tests to the new
framework in due time.

Note: the IdleStateHandler is inserted into the pipeline just before the WebSocketConnection handler only if we have a non-nil connection timeout value configured.

@pushkarnk
Copy link
Copy Markdown
Contributor Author

The new testing framework WebSocketClient uses the newly added WebSocket client upgrader.

}

public func userInboundEventTriggered(context: ChannelHandlerContext, event: Any) {
guard event is IdleStateHandler.IdleStateEvent else { return }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

usually the rules are that if you don't handle a user event, you should forward it (as opposed to dropping it). This might be the last handler in the pipeline so it might be fine...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is the last inbound handler in the pipeline.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added a change to propagate events that aren't IdleStateEvents

}
}

public func errorCaught(context: ChannelHandlerContext, error: Error) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

here I would definitely recommend that on unknown error you either:

  • forward (if this is not the "application handler", ie. the last and the most high-level handler)
  • log & close the Channel (in case this is the last handler which I often call the "application handler")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @weissi I've addressed this comment.

@pushkarnk pushkarnk changed the title Add automatic connection cleanup Add automatic stale connection cleanup Jul 17, 2019
@pushkarnk
Copy link
Copy Markdown
Contributor Author

@bridger Can you test your app with these changes please? Thanks!

@bridger
Copy link
Copy Markdown
Contributor

bridger commented Jul 23, 2019

I tried my app with this branch and with Kitura/Kitura-NIO#217. Unfortunately, it looks like I still have stale connections left. Maybe it is related to #42 ?

Pushkar Kulkarni added 3 commits July 30, 2019 09:09
Motivation:

On Kitura-WebSocket Kitura/Kitura-WebSocket#48
introduces an automatic connection cleanup feature. Using this,
`WebSocketService`s can configure an optional timeout value. If a connection
remains idle for half of the configured timeout value, the server sends a
heartbeat ping to the client. In a following time period equal to
half of the configured timeout value, if a pong is received on the connection,
the latter is kept alive. If not, the connection is closed.

In `Kitura-WebSocket-NIO`, we implement this using the `IdleStateHandler` which
is configured with half of the configured timeout value. When we receive the
first `IdleStateEvent`, we send a heartbeat ping to the client. If a pong is
received before the next `IdleStateEvent`, the connection is kept alive. If the
next `IdleStateEvent` is received before the pong, the connection is closed.

This commit also includes a new client framework for testing. It is difficult
to write the connection cleanup tests using the existing adhoc WebSocket client
framework implemented in `KituraTest.swift` and `KituraTest+Frames.swift`. The
new client framework is called `WebSocketClient`. It uses a callback model and
is largely work-in-progress. We must move all of the existing tests to the new
framework in due time.
@pushkarnk pushkarnk merged commit 7db750d into master Jul 30, 2019
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.

3 participants