Skip to content

Commit

Permalink
Add std::chrono::duration overloads to set timeouts (#2794)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Sep 27, 2024
1 parent 2f1575c commit 041c553
Show file tree
Hide file tree
Showing 26 changed files with 187 additions and 226 deletions.
48 changes: 39 additions & 9 deletions cpp/include/Ice/Proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ReferenceF.h"
#include "RequestHandlerF.h"

#include <chrono>
#include <future>
#include <iosfwd>
#include <optional>
Expand Down Expand Up @@ -165,7 +166,22 @@ namespace Ice
* @param timeout The new invocation timeout (in milliseconds).
* @return A proxy with the new timeout.
*/
Prx ice_invocationTimeout(int timeout) const { return fromReference(asPrx()._invocationTimeout(timeout)); }
Prx ice_invocationTimeout(int timeout) const
{
return ice_invocationTimeout(std::chrono::milliseconds(timeout));
}

/**
* Obtains a proxy that is identical to this proxy, except for the invocation timeout.
* @param timeout The new invocation timeout.
* @return A proxy with the new timeout.
*/
template<class Rep, class Period>
Prx ice_invocationTimeout(const std::chrono::duration<Rep, Period>& timeout) const
{
return fromReference(
asPrx()._invocationTimeout(std::chrono::duration_cast<std::chrono::milliseconds>(timeout)));
}

/**
* Obtains a proxy that is identical to this proxy, except for the locator.
Expand All @@ -182,7 +198,22 @@ namespace Ice
* @param timeout The new locator cache timeout (in seconds).
* @return A proxy with the new timeout.
*/
Prx ice_locatorCacheTimeout(int timeout) const { return fromReference(asPrx()._locatorCacheTimeout(timeout)); }
Prx ice_locatorCacheTimeout(int timeout) const
{
return ice_locatorCacheTimeout(std::chrono::seconds(timeout));
}

/**
* Obtains a proxy that is identical to this proxy, except for the locator cache timeout.
* @param timeout The new locator cache timeout.
* @return A proxy with the new timeout.
*/
template<class Rep, class Period>
Prx ice_locatorCacheTimeout(const std::chrono::duration<Rep, Period>& timeout) const
{
return fromReference(
asPrx()._locatorCacheTimeout(std::chrono::duration_cast<std::chrono::seconds>(timeout)));
}

/**
* Obtains a proxy that is identical to this proxy, but uses oneway invocations.
Expand Down Expand Up @@ -614,9 +645,9 @@ namespace Ice

/**
* Obtains the locator cache timeout of this proxy.
* @return The locator cache timeout value (in seconds).
* @return The locator cache timeout value.
*/
std::int32_t ice_getLocatorCacheTimeout() const noexcept;
std::chrono::milliseconds ice_getLocatorCacheTimeout() const noexcept;

/**
* Determines whether this proxy caches connections.
Expand Down Expand Up @@ -670,9 +701,9 @@ namespace Ice

/**
* Obtains the invocation timeout of this proxy.
* @return The invocation timeout value (in milliseconds).
* @return The invocation timeout value.
*/
std::int32_t ice_getInvocationTimeout() const noexcept;
std::chrono::milliseconds ice_getInvocationTimeout() const noexcept;

/**
* Determines whether this proxy uses twoway invocations.
Expand Down Expand Up @@ -787,14 +818,13 @@ namespace Ice
IceInternal::ReferencePtr _identity(Identity) const;
IceInternal::ReferencePtr _facet(std::string) const;
IceInternal::ReferencePtr _fixed(ConnectionPtr) const;
IceInternal::ReferencePtr _invocationTimeout(int) const;
IceInternal::ReferencePtr _invocationTimeout(std::chrono::milliseconds) const;
IceInternal::ReferencePtr _locator(const std::optional<LocatorPrx>&) const;
IceInternal::ReferencePtr _locatorCacheTimeout(int) const;
IceInternal::ReferencePtr _locatorCacheTimeout(std::chrono::milliseconds) const;
IceInternal::ReferencePtr _oneway() const;
IceInternal::ReferencePtr _preferSecure(bool) const;
IceInternal::ReferencePtr _router(const std::optional<RouterPrx>&) const;
IceInternal::ReferencePtr _secure(bool) const;
IceInternal::ReferencePtr _timeout(int) const;
IceInternal::ReferencePtr _twoway() const;

// Only the assignment operators can change these fields. All other member functions must be const.
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Ice/CollocatedRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ CollocatedRequestHandler::invokeAsyncRequest(OutgoingAsyncBase* outAsync, int ba
//
auto self = shared_from_this();

if (!synchronous || !_response || _reference->getInvocationTimeout() > 0)
if (!synchronous || !_response || _reference->getInvocationTimeout() > 0ms)
{
auto stream = make_shared<InputStream>();
is.swap(*stream);
Expand Down
23 changes: 5 additions & 18 deletions cpp/src/Ice/DefaultsAndOverrides.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace std;
using namespace Ice;
using namespace IceInternal;

IceInternal::DefaultsAndOverrides::DefaultsAndOverrides(const PropertiesPtr& properties, const LoggerPtr& logger)
IceInternal::DefaultsAndOverrides::DefaultsAndOverrides(const PropertiesPtr& properties)
: overrideCompress(nullopt),
overrideSecure(nullopt)
{
Expand Down Expand Up @@ -63,23 +63,10 @@ IceInternal::DefaultsAndOverrides::DefaultsAndOverrides(const PropertiesPtr& pro
throw ParseException(__FILE__, __LINE__, "illegal value '" + value + "'; expected 'Random' or 'Ordered'");
}

const_cast<int&>(defaultInvocationTimeout) = properties->getIcePropertyAsInt("Ice.Default.InvocationTimeout");
if (defaultInvocationTimeout < 1 && defaultInvocationTimeout != -1)
{
const_cast<int32_t&>(defaultInvocationTimeout) = -1;
Warning out(logger);
out << "invalid value for Ice.Default.InvocationTimeout `"
<< properties->getIceProperty("Ice.Default.InvocationTimeout") << "': defaulting to -1";
}

const_cast<int&>(defaultLocatorCacheTimeout) = properties->getIcePropertyAsInt("Ice.Default.LocatorCacheTimeout");
if (defaultLocatorCacheTimeout < -1)
{
const_cast<int32_t&>(defaultLocatorCacheTimeout) = -1;
Warning out(logger);
out << "invalid value for Ice.Default.LocatorCacheTimeout `"
<< properties->getIceProperty("Ice.Default.LocatorCacheTimeout") << "': defaulting to -1";
}
const_cast<chrono::milliseconds&>(defaultInvocationTimeout) =
chrono::milliseconds(properties->getIcePropertyAsInt("Ice.Default.InvocationTimeout"));
const_cast<chrono::seconds&>(defaultLocatorCacheTimeout) =
chrono::seconds(properties->getIcePropertyAsInt("Ice.Default.LocatorCacheTimeout"));

const_cast<bool&>(defaultPreferSecure) = properties->getIcePropertyAsInt("Ice.Default.PreferSecure") > 0;

Expand Down
8 changes: 5 additions & 3 deletions cpp/src/Ice/DefaultsAndOverrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
#include "Ice/PropertiesF.h"
#include "Network.h"

#include <chrono>

namespace IceInternal
{
class DefaultsAndOverrides
{
public:
DefaultsAndOverrides(const Ice::PropertiesPtr&, const Ice::LoggerPtr&);
DefaultsAndOverrides(const Ice::PropertiesPtr&);

std::string defaultHost;
Address defaultSourceAddress;
std::string defaultProtocol;
bool defaultCollocationOptimization;
Ice::EndpointSelectionType defaultEndpointSelection;
int defaultInvocationTimeout;
int defaultLocatorCacheTimeout;
std::chrono::milliseconds defaultInvocationTimeout;
std::chrono::seconds defaultLocatorCacheTimeout;
bool defaultPreferSecure;
Ice::EncodingVersion defaultEncoding;
Ice::FormatType defaultFormat;
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Ice/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ IceInternal::Instance::initialize(const Ice::CommunicatorPtr& communicator)
const_cast<TraceLevelsPtr&>(_traceLevels) = make_shared<TraceLevels>(_initData.properties);

const_cast<DefaultsAndOverridesPtr&>(_defaultsAndOverrides) =
make_shared<DefaultsAndOverrides>(_initData.properties, _initData.logger);
make_shared<DefaultsAndOverrides>(_initData.properties);

const_cast<ConnectionOptions&>(_clientConnectionOptions) = readConnectionOptions("Ice.Connection.Client");
const_cast<ConnectionOptions&>(_serverConnectionOptions) = readConnectionOptions("Ice.Connection.Server");
Expand Down
25 changes: 14 additions & 11 deletions cpp/src/Ice/LocatorInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,12 @@ IceInternal::LocatorTable::clear()
}

bool
IceInternal::LocatorTable::getAdapterEndpoints(const string& adapter, int ttl, vector<EndpointIPtr>& endpoints)
IceInternal::LocatorTable::getAdapterEndpoints(
const string& adapter,
chrono::milliseconds ttl,
vector<EndpointIPtr>& endpoints)
{
if (ttl == 0) // No locator cache.
if (ttl == 0ms) // No locator cache.
{
return false;
}
Expand Down Expand Up @@ -214,9 +217,9 @@ IceInternal::LocatorTable::removeAdapterEndpoints(const string& adapter)
}

bool
IceInternal::LocatorTable::getObjectReference(const Identity& id, int ttl, ReferencePtr& ref)
IceInternal::LocatorTable::getObjectReference(const Identity& id, chrono::milliseconds ttl, ReferencePtr& ref)
{
if (ttl == 0) // No locator cache
if (ttl == 0ms) // No locator cache
{
return false;
}
Expand Down Expand Up @@ -266,16 +269,16 @@ IceInternal::LocatorTable::removeObjectReference(const Identity& id)
}

bool
IceInternal::LocatorTable::checkTTL(const chrono::steady_clock::time_point& time, int ttl) const
IceInternal::LocatorTable::checkTTL(const chrono::steady_clock::time_point& time, chrono::milliseconds ttl) const
{
assert(ttl != 0);
if (ttl < 0) // TTL = infinite
assert(ttl != 0ms);
if (ttl < 0ms) // TTL = infinite
{
return true;
}
else
{
return chrono::steady_clock::now() - time <= chrono::seconds(ttl);
return chrono::steady_clock::now() - time <= ttl;
}
}

Expand Down Expand Up @@ -345,7 +348,7 @@ IceInternal::LocatorInfo::RequestCallback::exception(const LocatorInfoPtr& locat

IceInternal::LocatorInfo::RequestCallback::RequestCallback(
const ReferencePtr& ref,
int ttl,
chrono::milliseconds ttl,
const GetEndpointsCallbackPtr& cb)
: _reference(ref),
_ttl(ttl),
Expand All @@ -357,7 +360,7 @@ void
IceInternal::LocatorInfo::Request::addCallback(
const ReferencePtr& ref,
const ReferencePtr& wellKnownRef,
int ttl,
chrono::milliseconds ttl,
const GetEndpointsCallbackPtr& cb)
{
RequestCallbackPtr callback = make_shared<RequestCallback>(ref, ttl, cb);
Expand Down Expand Up @@ -508,7 +511,7 @@ void
IceInternal::LocatorInfo::getEndpoints(
const ReferencePtr& ref,
const ReferencePtr& wellKnownRef,
int ttl,
chrono::milliseconds ttl,
const GetEndpointsCallbackPtr& callback)
{
assert(ref->isIndirect());
Expand Down
24 changes: 16 additions & 8 deletions cpp/src/Ice/LocatorInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ namespace IceInternal

void clear();

bool getAdapterEndpoints(const std::string&, int, std::vector<EndpointIPtr>&);
bool getAdapterEndpoints(const std::string&, std::chrono::milliseconds, std::vector<EndpointIPtr>&);
void addAdapterEndpoints(const std::string&, const std::vector<EndpointIPtr>&);
std::vector<EndpointIPtr> removeAdapterEndpoints(const std::string&);

bool getObjectReference(const Ice::Identity&, int, ReferencePtr&);
bool getObjectReference(const Ice::Identity&, std::chrono::milliseconds, ReferencePtr&);
void addObjectReference(const Ice::Identity&, const ReferencePtr&);
ReferencePtr removeObjectReference(const Ice::Identity&);

private:
bool checkTTL(const std::chrono::steady_clock::time_point&, int) const;
bool checkTTL(const std::chrono::steady_clock::time_point&, std::chrono::milliseconds) const;

std::map<std::string, std::pair<std::chrono::steady_clock::time_point, std::vector<EndpointIPtr>>>
_adapterEndpointsMap;
Expand All @@ -79,22 +79,26 @@ namespace IceInternal
class RequestCallback final
{
public:
RequestCallback(const ReferencePtr&, int, const GetEndpointsCallbackPtr&);
RequestCallback(const ReferencePtr&, std::chrono::milliseconds, const GetEndpointsCallbackPtr&);

void response(const LocatorInfoPtr&, const std::optional<Ice::ObjectPrx>&);
void exception(const LocatorInfoPtr&, std::exception_ptr);

private:
const ReferencePtr _reference;
const int _ttl;
const std::chrono::milliseconds _ttl;
const GetEndpointsCallbackPtr _callback;
};
using RequestCallbackPtr = std::shared_ptr<RequestCallback>;

class Request
{
public:
void addCallback(const ReferencePtr&, const ReferencePtr&, int, const GetEndpointsCallbackPtr&);
void addCallback(
const ReferencePtr&,
const ReferencePtr&,
std::chrono::milliseconds,
const GetEndpointsCallbackPtr&);

void response(const std::optional<Ice::ObjectPrx>&);
void exception(std::exception_ptr);
Expand Down Expand Up @@ -129,11 +133,15 @@ namespace IceInternal

std::optional<Ice::LocatorRegistryPrx> getLocatorRegistry();

void getEndpoints(const ReferencePtr& ref, int ttl, const GetEndpointsCallbackPtr& cb)
void getEndpoints(const ReferencePtr& ref, std::chrono::milliseconds ttl, const GetEndpointsCallbackPtr& cb)
{
getEndpoints(ref, 0, ttl, cb);
}
void getEndpoints(const ReferencePtr&, const ReferencePtr&, int, const GetEndpointsCallbackPtr&);
void getEndpoints(
const ReferencePtr&,
const ReferencePtr&,
std::chrono::milliseconds,
const GetEndpointsCallbackPtr&);

void clearCache(const ReferencePtr&);

Expand Down
12 changes: 6 additions & 6 deletions cpp/src/Ice/OutgoingAsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,10 @@ ProxyOutgoingAsyncBase::invokeImpl(bool userThread)
{
if (userThread)
{
int invocationTimeout = _proxy._getReference()->getInvocationTimeout();
if (invocationTimeout > 0)
chrono::milliseconds invocationTimeout = _proxy._getReference()->getInvocationTimeout();
if (invocationTimeout > 0ms)
{
_instance->timer()->schedule(shared_from_this(), chrono::milliseconds(invocationTimeout));
_instance->timer()->schedule(shared_from_this(), invocationTimeout);
}
}
else
Expand Down Expand Up @@ -515,7 +515,7 @@ ProxyOutgoingAsyncBase::sentImpl(bool done)
_sent = true;
if (done)
{
if (_proxy._getReference()->getInvocationTimeout() != -1)
if (_proxy._getReference()->getInvocationTimeout() > 0ms)
{
_instance->timer()->cancel(shared_from_this());
}
Expand All @@ -526,7 +526,7 @@ ProxyOutgoingAsyncBase::sentImpl(bool done)
bool
ProxyOutgoingAsyncBase::exceptionImpl(std::exception_ptr ex)
{
if (_proxy._getReference()->getInvocationTimeout() != -1)
if (_proxy._getReference()->getInvocationTimeout() > 0ms)
{
_instance->timer()->cancel(shared_from_this());
}
Expand All @@ -536,7 +536,7 @@ ProxyOutgoingAsyncBase::exceptionImpl(std::exception_ptr ex)
bool
ProxyOutgoingAsyncBase::responseImpl(bool ok, bool invoke)
{
if (_proxy._getReference()->getInvocationTimeout() != -1)
if (_proxy._getReference()->getInvocationTimeout() > 0ms)
{
_instance->timer()->cancel(shared_from_this());
}
Expand Down
Loading

0 comments on commit 041c553

Please sign in to comment.