Skip to content

Commit

Permalink
Add TimeSpan overloads for C# timeout (zeroc-ice#2809)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Sep 27, 2024
1 parent 9353278 commit 9097a09
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 173 deletions.
2 changes: 1 addition & 1 deletion csharp/src/Ice/Internal/CollocatedRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal int invokeAsyncRequest(OutgoingAsyncBase outAsync, int batchRequestCoun
}

outAsync.attachCollocatedObserver(_adapter, requestId);
if (!synchronous || !_response || _reference.getInvocationTimeout() > 0)
if (!synchronous || !_response || _reference.getInvocationTimeout() > TimeSpan.Zero)
{
// Don't invoke from the user thread if async or invocation timeout is set
_adapter.getThreadPool().execute(
Expand Down
27 changes: 6 additions & 21 deletions csharp/src/Ice/Internal/DefaultsAndOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,10 @@ internal DefaultsAndOverrides(Ice.Properties properties, Ice.Logger logger)
throw new ParseException($"illegal value '{val}' in property Ice.Default.EndpointSelection; expected 'Random' or 'Ordered'");
}

defaultLocatorCacheTimeout = properties.getIcePropertyAsInt("Ice.Default.LocatorCacheTimeout");
if (defaultLocatorCacheTimeout < -1)
{
defaultLocatorCacheTimeout = -1;
StringBuilder msg = new StringBuilder("invalid value for Ice.Default.LocatorCacheTimeout `");
msg.Append(properties.getIceProperty("Ice.Default.LocatorCacheTimeout"));
msg.Append("': defaulting to -1");
logger.warning(msg.ToString());
}

defaultInvocationTimeout = properties.getIcePropertyAsInt("Ice.Default.InvocationTimeout");
if (defaultInvocationTimeout < 1 && defaultInvocationTimeout != -1)
{
defaultInvocationTimeout = -1;
StringBuilder msg = new StringBuilder("invalid value for Ice.Default.InvocationTimeout `");
msg.Append(properties.getIceProperty("Ice.Default.InvocationTimeout"));
msg.Append("': defaulting to -1");
logger.warning(msg.ToString());
}
defaultLocatorCacheTimeout = TimeSpan.FromSeconds(
properties.getIcePropertyAsInt("Ice.Default.LocatorCacheTimeout"));
defaultInvocationTimeout = TimeSpan.FromMilliseconds(
properties.getIcePropertyAsInt("Ice.Default.InvocationTimeout"));

defaultPreferSecure = properties.getIcePropertyAsInt("Ice.Default.PreferSecure") > 0;

Expand All @@ -117,8 +102,8 @@ internal DefaultsAndOverrides(Ice.Properties properties, Ice.Logger logger)
public string defaultProtocol;
public bool defaultCollocationOptimization;
public Ice.EndpointSelectionType defaultEndpointSelection;
public int defaultLocatorCacheTimeout;
public int defaultInvocationTimeout;
public TimeSpan defaultLocatorCacheTimeout;
public TimeSpan defaultInvocationTimeout;
public bool defaultPreferSecure;
public Ice.EncodingVersion defaultEncoding;
public Ice.FormatType defaultFormat;
Expand Down
26 changes: 13 additions & 13 deletions csharp/src/Ice/Internal/LocatorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,22 @@ public void
}

public
RequestCallback(Reference @ref, int ttl, GetEndpointsCallback cb)
RequestCallback(Reference @ref, TimeSpan ttl, GetEndpointsCallback cb)
{
_ref = @ref;
_ttl = ttl;
_callback = cb;
}

private readonly Reference _ref;
private readonly int _ttl;
private readonly TimeSpan _ttl;
private readonly GetEndpointsCallback _callback;
}

private abstract class Request
{
public void
addCallback(Reference @ref, Reference wellKnownRef, int ttl, GetEndpointsCallback cb)
addCallback(Reference @ref, Reference wellKnownRef, TimeSpan ttl, GetEndpointsCallback cb)
{
RequestCallback callback = new RequestCallback(@ref, ttl, cb);
lock (_mutex)
Expand Down Expand Up @@ -304,13 +304,13 @@ public Ice.LocatorRegistryPrx getLocatorRegistry()
}

public void
getEndpoints(Reference @ref, int ttl, GetEndpointsCallback callback)
getEndpoints(Reference @ref, TimeSpan ttl, GetEndpointsCallback callback)
{
getEndpoints(@ref, null, ttl, callback);
}

public void
getEndpoints(Reference @ref, Reference wellKnownRef, int ttl, GetEndpointsCallback callback)
getEndpoints(Reference @ref, Reference wellKnownRef, TimeSpan ttl, GetEndpointsCallback callback)
{
Debug.Assert(@ref.isIndirect());
EndpointI[] endpoints = null;
Expand Down Expand Up @@ -803,9 +803,9 @@ internal void clear()
}
}

internal EndpointI[] getAdapterEndpoints(string adapter, int ttl, out bool cached)
internal EndpointI[] getAdapterEndpoints(string adapter, TimeSpan ttl, out bool cached)
{
if (ttl == 0) // Locator cache disabled.
if (ttl == TimeSpan.Zero) // Locator cache disabled.
{
cached = false;
return null;
Expand Down Expand Up @@ -847,9 +847,9 @@ internal EndpointI[] removeAdapterEndpoints(string adapter)
}
}

internal Reference getObjectReference(Ice.Identity id, int ttl, out bool cached)
internal Reference getObjectReference(Ice.Identity id, TimeSpan ttl, out bool cached)
{
if (ttl == 0) // Locator cache disabled.
if (ttl == TimeSpan.Zero) // Locator cache disabled.
{
cached = false;
return null;
Expand Down Expand Up @@ -890,16 +890,16 @@ internal Reference removeObjectReference(Ice.Identity id)
}
}

private bool checkTTL(long time, int ttl)
private bool checkTTL(long time, TimeSpan ttl)
{
Debug.Assert(ttl != 0);
if (ttl < 0) // TTL = infinite
Debug.Assert(ttl != TimeSpan.Zero);
if (ttl < TimeSpan.Zero) // TTL = infinite
{
return true;
}
else
{
return Time.currentMonotonicTimeMillis() - time <= ((long)ttl * 1000);
return Time.currentMonotonicTimeMillis() - time <= ttl.TotalMilliseconds;
}
}

Expand Down
14 changes: 7 additions & 7 deletions csharp/src/Ice/Internal/OutgoingAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@ protected void invokeImpl(bool userThread)
{
if (userThread)
{
int invocationTimeout = proxy_.iceReference().getInvocationTimeout();
if (invocationTimeout > 0)
TimeSpan invocationTimeout = proxy_.iceReference().getInvocationTimeout();
if (invocationTimeout > TimeSpan.Zero)
{
instance_.timer().schedule(this, invocationTimeout);
instance_.timer().schedule(this, (long)invocationTimeout.TotalMilliseconds);
}
}
else if (observer_ != null)
Expand Down Expand Up @@ -606,7 +606,7 @@ protected override bool sentImpl(bool done)
_sent = true;
if (done)
{
if (proxy_.iceReference().getInvocationTimeout() != -1)
if (proxy_.iceReference().getInvocationTimeout() > TimeSpan.Zero)
{
instance_.timer().cancel(this);
}
Expand All @@ -616,7 +616,7 @@ protected override bool sentImpl(bool done)

protected override bool exceptionImpl(Ice.Exception ex)
{
if (proxy_.iceReference().getInvocationTimeout() != -1)
if (proxy_.iceReference().getInvocationTimeout() > TimeSpan.Zero)
{
instance_.timer().cancel(this);
}
Expand All @@ -625,7 +625,7 @@ protected override bool exceptionImpl(Ice.Exception ex)

protected override bool responseImpl(bool userThread, bool ok, bool invoke)
{
if (proxy_.iceReference().getInvocationTimeout() != -1)
if (proxy_.iceReference().getInvocationTimeout() > TimeSpan.Zero)
{
instance_.timer().cancel(this);
}
Expand Down Expand Up @@ -1040,7 +1040,7 @@ public override int invokeRemote(Ice.ConnectionI connection, bool compress, bool
public override int invokeCollocated(CollocatedRequestHandler handler)
{
// The stream cannot be cached if the proxy is not a twoway or there is an invocation timeout set.
if (!proxy_.ice_isTwoway() || proxy_.iceReference().getInvocationTimeout() != -1)
if (!proxy_.ice_isTwoway() || proxy_.iceReference().getInvocationTimeout() > TimeSpan.Zero)
{
// Disable caching by marking the streams as cached!
state_ |= StateCachedBuffers;
Expand Down
34 changes: 17 additions & 17 deletions csharp/src/Ice/Internal/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Dictionary<string, string> getContext()
return _context;
}

public int
public TimeSpan
getInvocationTimeout()
{
return _invocationTimeout;
Expand Down Expand Up @@ -125,7 +125,7 @@ public Ice.Communicator getCommunicator()

public abstract Ice.EndpointSelectionType getEndpointSelection();

public abstract int getLocatorCacheTimeout();
public abstract TimeSpan getLocatorCacheTimeout();

public abstract string getConnectionId();

Expand Down Expand Up @@ -185,7 +185,7 @@ public Reference changeFacet(string newFacet)
return r;
}

public Reference changeInvocationTimeout(int newTimeout)
public Reference changeInvocationTimeout(TimeSpan newTimeout)
{
Reference r = _instance.referenceFactory().copy(this);
r._invocationTimeout = newTimeout;
Expand Down Expand Up @@ -222,7 +222,7 @@ public virtual Reference changeCompress(bool newCompress)

public abstract Reference changeEndpointSelection(Ice.EndpointSelectionType newType);

public abstract Reference changeLocatorCacheTimeout(int newTimeout);
public abstract Reference changeLocatorCacheTimeout(TimeSpan newTimeout);

public abstract Reference changeConnectionId(string connectionId);

Expand Down Expand Up @@ -446,7 +446,7 @@ public virtual bool Equals(Reference other)
protected bool secure_;
private Ice.ProtocolVersion _protocol;
private Ice.EncodingVersion _encoding;
private int _invocationTimeout;
private TimeSpan _invocationTimeout;
private bool? _compress;

protected Reference(
Expand All @@ -459,7 +459,7 @@ protected Reference(
bool? compress,
Ice.ProtocolVersion protocol,
Ice.EncodingVersion encoding,
int invocationTimeout,
TimeSpan invocationTimeout,
Dictionary<string, string> context)
{
// Validate string arguments.
Expand Down Expand Up @@ -496,7 +496,7 @@ public FixedReference(
Ice.ProtocolVersion protocol,
Ice.EncodingVersion encoding,
Ice.ConnectionI connection,
int invocationTimeout,
TimeSpan invocationTimeout,
Dictionary<string, string> context)
: base(instance, communicator, identity, facet, mode, secure, compress, protocol, encoding, invocationTimeout, context)
{
Expand Down Expand Up @@ -543,9 +543,9 @@ public override Ice.EndpointSelectionType getEndpointSelection()
return Ice.EndpointSelectionType.Random;
}

public override int getLocatorCacheTimeout()
public override TimeSpan getLocatorCacheTimeout()
{
return 0;
return TimeSpan.Zero;
}

public override string getConnectionId()
Expand Down Expand Up @@ -603,7 +603,7 @@ public override Reference changeEndpointSelection(Ice.EndpointSelectionType newT
throw new Ice.FixedProxyException();
}

public override Reference changeLocatorCacheTimeout(int newTimeout)
public override Reference changeLocatorCacheTimeout(TimeSpan newTimeout)
{
throw new Ice.FixedProxyException();
}
Expand Down Expand Up @@ -756,7 +756,7 @@ public override Ice.EndpointSelectionType getEndpointSelection()
return _endpointSelection;
}

public override int getLocatorCacheTimeout()
public override TimeSpan getLocatorCacheTimeout()
{
return _locatorCacheTimeout;
}
Expand Down Expand Up @@ -872,7 +872,7 @@ public override Reference changeEndpointSelection(Ice.EndpointSelectionType newT
return r;
}

public override Reference changeLocatorCacheTimeout(int newTimeout)
public override Reference changeLocatorCacheTimeout(TimeSpan newTimeout)
{
RoutableReference r = (RoutableReference)getInstance().referenceFactory().copy(this);
r._locatorCacheTimeout = newTimeout;
Expand Down Expand Up @@ -1000,8 +1000,8 @@ public override Dictionary<string, string> toProperty(string prefix)
properties[prefix + ".PreferSecure"] = _preferSecure ? "1" : "0";
properties[prefix + ".EndpointSelection"] =
_endpointSelection == Ice.EndpointSelectionType.Random ? "Random" : "Ordered";
properties[prefix + ".LocatorCacheTimeout"] = _locatorCacheTimeout.ToString(CultureInfo.InvariantCulture);
properties[prefix + ".InvocationTimeout"] = getInvocationTimeout().ToString(CultureInfo.InvariantCulture);
properties[prefix + ".LocatorCacheTimeout"] = _locatorCacheTimeout.TotalSeconds.ToString(CultureInfo.InvariantCulture);
properties[prefix + ".InvocationTimeout"] = getInvocationTimeout().TotalMilliseconds.ToString(CultureInfo.InvariantCulture);

if (_routerInfo != null)
{
Expand Down Expand Up @@ -1243,8 +1243,8 @@ public RoutableReference(
bool cacheConnection,
bool preferSecure,
Ice.EndpointSelectionType endpointSelection,
int locatorCacheTimeout,
int invocationTimeout,
TimeSpan locatorCacheTimeout,
TimeSpan invocationTimeout,
Dictionary<string, string> context)
: base(
instance,
Expand Down Expand Up @@ -1577,7 +1577,7 @@ public int Compare(EndpointI le, EndpointI re)
private bool _cacheConnection;
private bool _preferSecure;
private Ice.EndpointSelectionType _endpointSelection;
private int _locatorCacheTimeout;
private TimeSpan _locatorCacheTimeout;

private string _connectionId = "";
}
42 changes: 7 additions & 35 deletions csharp/src/Ice/Internal/ReferenceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal Reference create(Ice.Identity ident, Ice.ConnectionI connection)
Ice.Util.Protocol_1_0,
_instance.defaultsAndOverrides().defaultEncoding,
connection,
-1,
TimeSpan.FromMilliseconds(-1),
null);
}

Expand Down Expand Up @@ -725,8 +725,8 @@ private Reference create(
bool cacheConnection = true;
bool preferSecure = defaultsAndOverrides.defaultPreferSecure;
Ice.EndpointSelectionType endpointSelection = defaultsAndOverrides.defaultEndpointSelection;
int locatorCacheTimeout = defaultsAndOverrides.defaultLocatorCacheTimeout;
int invocationTimeout = defaultsAndOverrides.defaultInvocationTimeout;
TimeSpan locatorCacheTimeout = defaultsAndOverrides.defaultLocatorCacheTimeout;
TimeSpan invocationTimeout = defaultsAndOverrides.defaultInvocationTimeout;
Dictionary<string, string> context = null;

//
Expand Down Expand Up @@ -805,40 +805,12 @@ private Reference create(
}

property = propertyPrefix + ".LocatorCacheTimeout";
string val = properties.getProperty(property);
if (val.Length > 0)
{
locatorCacheTimeout = properties.getPropertyAsIntWithDefault(property, locatorCacheTimeout);
if (locatorCacheTimeout < -1)
{
locatorCacheTimeout = -1;

StringBuilder msg = new StringBuilder("invalid value for ");
msg.Append(property);
msg.Append(" `");
msg.Append(properties.getProperty(property));
msg.Append("': defaulting to -1");
_instance.initializationData().logger.warning(msg.ToString());
}
}
locatorCacheTimeout = TimeSpan.FromSeconds(
properties.getPropertyAsIntWithDefault(property, (int)locatorCacheTimeout.TotalSeconds));

property = propertyPrefix + ".InvocationTimeout";
val = properties.getProperty(property);
if (val.Length > 0)
{
invocationTimeout = properties.getPropertyAsIntWithDefault(property, invocationTimeout);
if (invocationTimeout < 1 && invocationTimeout != -1)
{
invocationTimeout = -1;

StringBuilder msg = new StringBuilder("invalid value for ");
msg.Append(property);
msg.Append(" `");
msg.Append(properties.getProperty(property));
msg.Append("': defaulting to -1");
_instance.initializationData().logger.warning(msg.ToString());
}
}
invocationTimeout = TimeSpan.FromMilliseconds(
properties.getPropertyAsIntWithDefault(property, (int)invocationTimeout.TotalMilliseconds));

property = propertyPrefix + ".Context.";
Dictionary<string, string> contexts = properties.getPropertiesForPrefix(property);
Expand Down
Loading

0 comments on commit 9097a09

Please sign in to comment.