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

Implement Keepalive #145

Merged
merged 2 commits into from
Dec 7, 2016
Merged

Implement Keepalive #145

merged 2 commits into from
Dec 7, 2016

Conversation

caleblloyd
Copy link
Contributor

  • Fixes Implement Keepalive Connection String Options #132
  • A value of 0 indicates that the OS Default keepalive settings are used
  • On Windows, a value greater than 0 is the idle connection time, measured in seconds, before the first keepalive packet is sent.
  • Unix-based Operating Systems will always use the OS Default keepalive settings.

@caleblloyd
Copy link
Contributor Author

I've included a basic test, but I couldn't think of a better way to unit test this since everything happens at the network driver layer. Here's a screenshot of a packet capture running this commit with Keepalive=3 in the connection string.

keepalive

As you can see, the keepalive runs every 3 seconds from time 792-810. At time 812, I shutdown the MySql server, and the server sends FIN.

The client does not send FIN, so the client believes the connection is still open. The next keepalive packet is not ACK'd, so the client retries keepalives every 1s, which is the proper interval. Eventually 10 keepalives are missed, so the client RST the connection.

I think that this demonstrates correct behavior.

var stopwatch = Stopwatch.StartNew();
await Task.Delay(3000);
stopwatch.Stop();
Assert.InRange(stopwatch.ElapsedMilliseconds, 2500, 3500);
Copy link
Member

Choose a reason for hiding this comment

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

What is this testing specifically? That no exception is thrown?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. I guess the stopwatch is not necessary, I'll just remove that

@@ -148,6 +148,13 @@ These are the other options that MySqlConnector supports. They are set to sensi
<td>True to have MySqlDataReader.GetValue() and MySqlDataReader.GetDateTime() return DateTime.MinValue for date or datetime columns that have disallowed values.</td>
</tr>
<tr>
<td>Keep Alive, Keepalive</td>
<td>0</td>
<td>TCP Keepalive idle time. A value of 0 indicates that the OS Default keepalive settings are used.
Copy link
Member

Choose a reason for hiding this comment

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

Connector/Net documents "A value of 0 indicates that keepalive is not used."; do we want to be different by default?

public static void SetKeepalive(Socket socket, uint keepAliveTimeSeconds)
{
// Always use the OS Default Keepalive settings
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
Copy link
Member

Choose a reason for hiding this comment

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

Following up on question above, should this option only be set if user has set keepAlive != 0?

If you think that the benefits of turning it on by default are worthwhile, then I have no objection. (I'm in favour of changing to use sensible defaults.)

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 think that always enabling it to use the OS Default would be sensible. It allows the OS to clean up old sockets should the connection go away. If data is consistently being sent across the network, Keepalive packets will never need to be sent.

Windows and Linux both default to send the first Keepalive packet after 2 hours of inactivity on the socket.

@bgrainger bgrainger merged commit 14dc9d4 into mysql-net:master Dec 7, 2016
@caleblloyd caleblloyd deleted the f_keepalive branch February 7, 2017 02:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants