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

Convert timeouts and other time intervals to TimeSpan #688

Merged
merged 2 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions projects/client/RabbitMQ.Client/src/client/api/ConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ public sealed class ConnectionFactory : ConnectionFactoryBase, IAsyncConnectionF
public const ushort DefaultChannelMax = 2047;

/// <summary>
/// Default value for connection attempt timeout, in milliseconds.
/// Default value for connection attempt timeout.
/// </summary>
public const int DefaultConnectionTimeout = 30 * 1000;
public static readonly TimeSpan DefaultConnectionTimeout = TimeSpan.FromSeconds(30);

/// <summary>
/// Default value for the desired maximum frame size. Default is 0 ("no limit").
/// </summary>
public const uint DefaultFrameMax = 0;

/// <summary>
/// Default value for desired heartbeat interval, in seconds. Default is 60,
/// 0 means "heartbeats are disabled".
/// Default value for desired heartbeat interval. Default is 60 seconds,
/// TimeSpan.Zero means "heartbeats are disabled".
/// </summary>
public const ushort DefaultHeartbeat = 60; //
public static readonly TimeSpan DefaultHeartbeat = TimeSpan.FromSeconds(60); //

/// <summary>
/// Default password (value: "guest").
Expand Down Expand Up @@ -227,19 +227,19 @@ public TimeSpan ContinuationTimeout
public int Port { get; set; } = AmqpTcpEndpoint.UseDefaultPort;

/// <summary>
/// Timeout setting for connection attempts (in milliseconds).
/// Timeout setting for connection attempts.
/// </summary>
public int RequestedConnectionTimeout { get; set; } = DefaultConnectionTimeout;
public TimeSpan RequestedConnectionTimeout { get; set; } = DefaultConnectionTimeout;

/// <summary>
/// Timeout setting for socket read operations (in milliseconds).
/// Timeout setting for socket read operations.
/// </summary>
public int SocketReadTimeout { get; set; } = DefaultConnectionTimeout;
public TimeSpan SocketReadTimeout { get; set; } = DefaultConnectionTimeout;

/// <summary>
/// Timeout setting for socket write operations (in milliseconds).
/// Timeout setting for socket write operations.
/// </summary>
public int SocketWriteTimeout { get; set; } = DefaultConnectionTimeout;
public TimeSpan SocketWriteTimeout { get; set; } = DefaultConnectionTimeout;

/// <summary>
/// Ssl options setting.
Expand Down Expand Up @@ -295,9 +295,9 @@ public AmqpTcpEndpoint Endpoint
public uint RequestedFrameMax { get; set; } = DefaultFrameMax;

/// <summary>
/// Heartbeat timeout to use when negotiating with the server (in seconds).
/// Heartbeat timeout to use when negotiating with the server.
/// </summary>
public ushort RequestedHeartbeat { get; set; } = DefaultHeartbeat;
public TimeSpan RequestedHeartbeat { get; set; } = DefaultHeartbeat;

/// <summary>
/// When set to true, background thread will be used for the I/O loop.
Expand Down Expand Up @@ -525,13 +525,22 @@ private IFrameHandler CreateFrameHandlerForHostname(string hostname)
return CreateFrameHandler(this.Endpoint.CloneWithHostname(hostname));
}


private IFrameHandler ConfigureFrameHandler(IFrameHandler fh)
{
// make sure socket timeouts are higher than heartbeat
fh.ReadTimeout = Math.Max(SocketReadTimeout, RequestedHeartbeat * 1000);
fh.WriteTimeout = Math.Max(SocketWriteTimeout, RequestedHeartbeat * 1000);
// TODO: add user-provided configurator, like in the Java client
fh.ReadTimeout = RequestedHeartbeat;
fh.WriteTimeout = RequestedHeartbeat;

if (SocketReadTimeout > RequestedHeartbeat)
{
fh.ReadTimeout = SocketReadTimeout;
}

if (SocketWriteTimeout > RequestedHeartbeat)
{
fh.WriteTimeout = SocketWriteTimeout;
}

return fh;
}

Expand Down
50 changes: 7 additions & 43 deletions projects/client/RabbitMQ.Client/src/client/api/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public interface IConnection : NetworkConnection, IDisposable
uint FrameMax { get; }

/// <summary>
/// The current heartbeat setting for this connection (0 for disabled), in seconds.
/// The current heartbeat setting for this connection (System.TimeSpan.Zero for disabled).
/// </summary>
ushort Heartbeat { get; }
TimeSpan Heartbeat { get; }

/// <summary>
/// Returns true if the connection is still in a state where it can be used.
Expand Down Expand Up @@ -213,22 +213,6 @@ public interface IConnection : NetworkConnection, IDisposable
/// </remarks>
void Abort(ushort reasonCode, string reasonText);

/// <summary>
/// Abort this connection and all its channels and wait with a
/// timeout for all the in-progress close operations to complete.
/// </summary>
/// <remarks>
/// This method, behaves in a similar way as method <see cref="Abort()"/> with the
/// only difference that it explictly specifies a timeout given
/// for all the in-progress close operations to complete.
/// If timeout is reached and the close operations haven't finished, then socket is forced to close.
/// <para>
/// The timeout value is in milliseconds.
/// To wait infinitely for the close operations to complete use <see cref="Timeout.Infinite"/>.
/// </para>
/// </remarks>
void Abort(int timeout);

/// <summary>
/// Abort this connection and all its channels and wait with a
/// timeout for all the in-progress close operations to complete.
Expand All @@ -244,25 +228,6 @@ public interface IConnection : NetworkConnection, IDisposable
/// </remarks>
void Abort(TimeSpan timeout);

/// <summary>
/// Abort this connection and all its channels and wait with a
/// timeout for all the in-progress close operations to complete.
/// </summary>
/// <remarks>
/// The method behaves in the same way as <see cref="Abort(int)"/>, with the only
/// difference that the connection is closed with the given connection close code and message.
/// <para>
/// The close code (See under "Reply Codes" in the AMQP 0-9-1 specification).
/// </para>
/// <para>
/// A message indicating the reason for closing the connection.
/// </para>
/// <para>
/// Operation timeout in milliseconds.
/// </para>
/// </remarks>
void Abort(ushort reasonCode, string reasonText, int timeout);

/// <summary>
/// Abort this connection and all its channels and wait with a
/// timeout for all the in-progress close operations to complete.
Expand Down Expand Up @@ -319,18 +284,17 @@ public interface IConnection : NetworkConnection, IDisposable
/// It can also throw <see cref="IOException"/> when socket was closed unexpectedly.
/// If timeout is reached and the close operations haven't finished, then socket is forced to close.
/// <para>
/// The timeout value is in milliseconds.
/// To wait infinitely for the close operations to complete use <see cref="Timeout.Infinite"/>.
/// To wait infinitely for the close operations to complete use <see cref="System.Threading.Timeout.InfiniteTimeSpan"/>.
/// </para>
/// </remarks>
void Close(int timeout);
void Close(TimeSpan timeout);

/// <summary>
/// Close this connection and all its channels
/// and wait with a timeout for all the in-progress close operations to complete.
/// </summary>
/// <remarks>
/// The method behaves in the same way as <see cref="Close(int)"/>, with the only
/// The method behaves in the same way as <see cref="Close(TimeSpan)"/>, with the only
/// difference that the connection is closed with the given connection close code and message.
/// <para>
/// The close code (See under "Reply Codes" in the AMQP 0-9-1 specification).
Expand All @@ -339,10 +303,10 @@ public interface IConnection : NetworkConnection, IDisposable
/// A message indicating the reason for closing the connection.
/// </para>
/// <para>
/// Operation timeout in milliseconds.
/// Operation timeout.
/// </para>
/// </remarks>
void Close(ushort reasonCode, string reasonText, int timeout);
void Close(ushort reasonCode, string reasonText, TimeSpan timeout);

/// <summary>
/// Create and return a fresh channel, session, and model.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public interface IConnectionFactory
uint RequestedFrameMax { get; set; }

/// <summary>
/// Heartbeat setting to request (in seconds).
/// Heartbeat setting to request.
/// </summary>
ushort RequestedHeartbeat { get; set; }
TimeSpan RequestedHeartbeat { get; set; }

/// <summary>
/// When set to true, background threads will be used for I/O and heartbeats.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ITcpClient : IDisposable
{
bool Connected { get; }

int ReceiveTimeout { get; set; }
TimeSpan ReceiveTimeout { get; set; }

Socket Client { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public uint FrameMax
get { return m_delegate.FrameMax; }
}

public ushort Heartbeat
public TimeSpan Heartbeat
{
get { return m_delegate.Heartbeat; }
}
Expand Down Expand Up @@ -599,28 +599,14 @@ public void Abort(ushort reasonCode, string reasonText)
m_delegate.Abort(reasonCode, reasonText);
}

///<summary>API-side invocation of connection abort with timeout.</summary>
public void Abort(int timeout)
{
StopRecoveryLoop();
if (m_delegate.IsOpen)
m_delegate.Abort(timeout);
}

///<summary>API-side invocation of connection abort with timeout.</summary>
public void Abort(TimeSpan timeout)
{
StopRecoveryLoop();
if (m_delegate.IsOpen)
{
m_delegate.Abort(timeout);
}

///<summary>API-side invocation of connection abort with timeout.</summary>
public void Abort(ushort reasonCode, string reasonText, int timeout)
{
StopRecoveryLoop();
if (m_delegate.IsOpen)
m_delegate.Abort(reasonCode, reasonText, timeout);
}
}

///<summary>API-side invocation of connection abort with timeout.</summary>
Expand Down Expand Up @@ -648,19 +634,23 @@ public void Close(ushort reasonCode, string reasonText)
}

///<summary>API-side invocation of connection.close with timeout.</summary>
public void Close(int timeout)
public void Close(TimeSpan timeout)
{
StopRecoveryLoop();
if (m_delegate.IsOpen)
{
m_delegate.Close(timeout);
}
}

///<summary>API-side invocation of connection.close with timeout.</summary>
public void Close(ushort reasonCode, string reasonText, int timeout)
public void Close(ushort reasonCode, string reasonText, TimeSpan timeout)
{
StopRecoveryLoop();
if (m_delegate.IsOpen)
{
m_delegate.Close(reasonCode, reasonText, timeout);
}
}

public IModel CreateModel()
Expand Down
Loading