Commit 4f1bee94 authored by legoc's avatar legoc
Browse files

(split2) Refined timeout management

- When RequestSocketImpl has timeout, linger is set to 100ms.
- Request::setTimeout() propagates the timeout to the associated Server.
parent 2525b652
......@@ -4,8 +4,8 @@
#
# -----------------------------------------------------------------------------
AC_INIT(cameo-api-cpp, 0.3.0)
LIBRARY_VERSION=0:3:0
AC_INIT(cameo-api-cpp, 0.3.1)
LIBRARY_VERSION=0:3:1
AC_CONFIG_AUX_DIR(config)
AC_CONFIG_SRCDIR(src/cameo/Application.cpp)
......
......@@ -1012,6 +1012,9 @@ std::unique_ptr<Instance> Request::connectToRequester() {
// Instantiate the requester server if it does not exist.
if (m_requesterServer.get() == nullptr) {
m_requesterServer.reset(new Server(m_impl->m_requesterServerEndpoint));
// The server inherits the timeout.
m_requesterServer->setTimeout(m_impl->m_timeout);
}
// Connect and find the instance.
......
......@@ -56,7 +56,7 @@ void Services::init() {
void Services::initRequestSocket() {
// Create the request socket. The server endpoint must have been initialized.
m_requestSocket = std::move(createRequestSocket(m_serverEndpoint));
m_requestSocket = std::move(createRequestSocket(m_serverEndpoint, m_impl->m_timeout));
}
std::vector<std::string> Services::split(const std::string& info) {
......@@ -77,6 +77,10 @@ std::vector<std::string> Services::split(const std::string& info) {
void Services::setTimeout(int timeout) {
m_impl->setTimeout(timeout);
if (m_requestSocket.get() != nullptr) {
m_requestSocket->setTimeout(timeout);
}
}
int Services::getTimeout() const {
......
......@@ -26,17 +26,24 @@ using namespace std;
namespace cameo {
RequestSocketImpl::RequestSocketImpl(zmq::socket_t * socket, int timeout) :
m_socket(socket), m_timeout(timeout) {
m_socket(socket) {
setTimeout(timeout);
}
RequestSocketImpl::~RequestSocketImpl() {
}
void RequestSocketImpl::setLinger(bool linger) {
// Set the value only in the case linger=false.
// This value has effect when terminating the context.
if (!linger) {
int lingerValue = 0;
void RequestSocketImpl::setTimeout(int timeout) {
m_timeout = timeout;
// Set the linger in case of timeout.
// If not, the context can block indefinitely.
// Does the value 100 can lead to a side-effect? A too small value like 1 has some side-effect.
// After some tests, the value seems reasonable.
// If a Server instance is not reachable, the context that contains the message in timeout will block during this linger period.
if (m_timeout > 0) {
int lingerValue = 100;
m_socket->setsockopt(ZMQ_LINGER, &lingerValue, sizeof(int));
}
}
......
......@@ -29,7 +29,7 @@ public:
RequestSocketImpl(zmq::socket_t * socket, int timeout = 0);
virtual ~RequestSocketImpl();
void setLinger(bool linger);
void setTimeout(int timeout);
std::unique_ptr<zmq::message_t> request(const std::string& requestTypePart, const std::string& requestDataPart, int overrideTimeout = -1);
......
......@@ -256,10 +256,6 @@ zmq::socket_t * ServicesImpl::createRequestSocket(const std::string& endpoint) {
zmq::socket_t* socket = new zmq::socket_t(m_context, ZMQ_REQ);
try {
// Set the linger value to 0 to ensure that pending requests are destroyed in case of timeout.
//int value = 0;
//socket->setsockopt(ZMQ_LINGER, &value, sizeof(int));
// Connect to the endpoint.
socket->connect(endpoint.c_str());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment