Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ff3e95b

Browse files
committedMay 17, 2016
fix(isolate): backoff for reconnection to isolate daemon (#38)
1 parent 14286f5 commit ff3e95b

File tree

13 files changed

+46
-16
lines changed

13 files changed

+46
-16
lines changed
 

‎cache/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ADD_LIBRARY(cache MODULE
77

88
TARGET_LINK_LIBRARIES(cache
99
cocaine-core
10+
cocaine-io-util
1011
blackhole
1112
${Boost_LIBRARIES})
1213

‎chrono/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ADD_LIBRARY(chrono MODULE
77

88
TARGET_LINK_LIBRARIES(chrono
99
cocaine-core
10+
cocaine-io-util
1011
blackhole
1112
${Boost_LIBRARIES})
1213

‎docker/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ADD_LIBRARY(docker MODULE
1616

1717
TARGET_LINK_LIBRARIES(docker
1818
cocaine-core
19+
cocaine-io-util
1920
blackhole
2021
${Boost_LIBRARIES}
2122
curl)

‎echo/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ ADD_LIBRARY(echo MODULE
77

88
TARGET_LINK_LIBRARIES(echo
99
cocaine-core
10-
${Boost_LIBRARIES})
10+
cocaine-io-util
11+
${Boost_LIBRARIES})
1112

1213
SET_TARGET_PROPERTIES(echo PROPERTIES
1314
PREFIX ""

‎elasticsearch/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ TARGET_LINK_LIBRARIES(elasticsearch
1818
blackhole
1919
swarm
2020
swarm_urlfetcher
21-
cocaine-core)
21+
cocaine-core
22+
cocaine-io-util)
2223

2324
SET_TARGET_PROPERTIES(elasticsearch PROPERTIES
2425
PREFIX ""

‎graphite/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ADD_LIBRARY(graphite MODULE
1212
TARGET_LINK_LIBRARIES(graphite
1313
blackhole
1414
cocaine-core
15+
cocaine-io-util
1516
${Boost_LIBRARIES})
1617

1718
SET_TARGET_PROPERTIES(graphite PROPERTIES

‎ipvs/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ELSE()
99

1010
TARGET_LINK_LIBRARIES(ipvs-gateway
1111
cocaine-core
12+
cocaine-io-util
1213
ipvs)
1314

1415
SET_TARGET_PROPERTIES(ipvs-gateway PROPERTIES

‎logging/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ TARGET_LINK_LIBRARIES(logging
1414
${Boost_LIBRARIES}
1515
blackhole
1616
cocaine-core
17+
cocaine-io-util
1718
)
1819

1920
SET_TARGET_PROPERTIES(logging PROPERTIES

‎mongo/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ IF(MongoDB_FOUND)
1818

1919
TARGET_LINK_LIBRARIES(mongodb
2020
cocaine-core
21+
cocaine-io-util
2122
${Boost_LIBRARIES}
2223
mongoclient)
2324

‎node/src/isolate/external.cpp

+32-14
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ struct spawn_dispatch_t :
8282
spawn_handle->on_terminate(ec, msg);
8383
}
8484

85-
virtual void discard(const std::error_code& ec) const {
85+
virtual void discard(const std::error_code&) const {
8686
// As a personal Boris request we do not shutdown worker on external daemon disconnection
8787
// spawn_handle->on_terminate(ec, "external isolation session was discarded");
8888
}
@@ -169,6 +169,7 @@ struct external_t::inner_t :
169169
std::atomic<id_t> cur_id;
170170
std::shared_ptr<dispatch<io::context_tag>> signal_dispatch;
171171
bool prepared;
172+
bool connecting;
172173

173174

174175
std::vector<std::shared_ptr<spool_load_t>> spool_queue;
@@ -183,7 +184,8 @@ struct external_t::inner_t :
183184
args(_args),
184185
log(context.log("universal_isolate/"+_name)),
185186
signal_dispatch(std::make_shared<dispatch<io::context_tag>>("universal_isolate_signal")),
186-
prepared(false)
187+
prepared(false),
188+
connecting(false)
187189
{
188190
signal_dispatch->on<io::context::prepared>([&](){
189191
prepared = true;
@@ -197,25 +199,27 @@ struct external_t::inner_t :
197199

198200
void connect() {
199201
socket.reset(new asio::ip::tcp::socket(io_context));
202+
connecting = true;
200203
auto ep = args.as_object().at("external_isolation_endpoint", dynamic_t::empty_object).as_object();
201204
tcp::endpoint endpoint(
202205
asio::ip::address::from_string(ep.at("host", "127.0.0.1").as_string()),
203206
static_cast<unsigned short>(ep.at("port", 29042u).as_uint())
204207
);
205208

206-
auto connect_timeout = args.as_object().at("connect_timeout", 5u).as_uint();
209+
auto connect_timeout_ms = args.as_object().at("connect_timeout_ms", 5000u).as_uint();
207210

208211
COCAINE_LOG_INFO(log, "connecting to external isolation daemon to {}", boost::lexical_cast<std::string>(endpoint));
209212
auto self_shared = shared_from_this();
210213

211-
connect_timer.expires_from_now(boost::posix_time::seconds(connect_timeout));
214+
connect_timer.expires_from_now(boost::posix_time::milliseconds(connect_timeout_ms));
212215
connect_timer.async_wait([=](const std::error_code& ec){
213216
if(!ec) {
214217
self_shared->socket->cancel();
215218
}
216219
});
217220

218221
socket->async_connect(endpoint, [=](const std::error_code& ec) {
222+
connecting = false;
219223
if (connect_timer.cancel() && !ec) {
220224
COCAINE_LOG_INFO(log, "connected to isolation daemon");
221225
session = context.engine().attach(std::move(socket), nullptr);
@@ -229,11 +233,25 @@ struct external_t::inner_t :
229233
}
230234

231235
void reconnect(const std::error_code& e) {
232-
if(session) {
233-
COCAINE_LOG_INFO(log, "reconnecting to external isolation daemon");
234-
session->detach(e);
235-
session.reset();
236-
connect();
236+
if(!connecting) {
237+
connecting = true;
238+
if(session) {
239+
session->detach(e);
240+
session.reset();
241+
}
242+
243+
auto reconnect_timeout_ms = args.as_object().at("reconnect_timeout_ms", 3000u).as_uint();
244+
COCAINE_LOG_INFO(log, "queueing reconnect to external isolation daemon after {} ms", reconnect_timeout_ms);
245+
246+
connect_timer.expires_from_now(boost::posix_time::milliseconds(reconnect_timeout_ms));
247+
std::weak_ptr<inner_t> weak_self(shared_from_this());
248+
connect_timer.async_wait([=](const std::error_code&) {
249+
// We don't perform error code check as nobody should cancel this timer
250+
auto self = weak_self.lock();
251+
if (self) {
252+
self->connect();
253+
} // else application was already stopped and isolation was destroyed
254+
});
237255
}
238256
}
239257

@@ -379,8 +397,8 @@ external_t::spool(std::shared_ptr<api::spool_handle_base_t> handler) {
379397
} else {
380398
COCAINE_LOG_DEBUG(inner->log, "queuing spool request");
381399
_inner->spool_queue.push_back(load);
382-
if(!_inner->session && !_inner->socket) {
383-
inner->connect();
400+
if(!inner->connecting && _inner->prepared) {
401+
inner->reconnect(std::error_code());
384402
}
385403
}
386404
});
@@ -402,10 +420,10 @@ external_t::spawn(const std::string& path,
402420
load->apply();
403421
} else {
404422
COCAINE_LOG_DEBUG(_inner->log, "queuing spawn request");
405-
if(!_inner->session && !_inner->socket) {
406-
inner->connect();
407-
}
408423
_inner->spawn_queue.push_back(load);
424+
if(!inner->connecting && _inner->prepared) {
425+
inner->reconnect(std::error_code());
426+
}
409427
}
410428
});
411429
return std::unique_ptr<api::cancellation_t>(new api::cancellation_wrapper(load));

‎python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ADD_LIBRARY(python MODULE
2424

2525
TARGET_LINK_LIBRARIES(python
2626
cocaine-core
27+
cocaine-io-util
2728
${PYTHON_LIBS})
2829

2930
SET_TARGET_PROPERTIES(python PROPERTIES

‎unicorn/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ TARGET_LINK_LIBRARIES(unicorn
2727
msgpack
2828
blackhole
2929
cocaine-core
30+
cocaine-io-util
3031
zookeeper_mt
3132
${Boost_LIBRARIES})
3233

‎urlfetch/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ADD_LIBRARY(urlfetch MODULE
1717

1818
TARGET_LINK_LIBRARIES(urlfetch
1919
cocaine-core
20+
cocaine-io-util
2021
blackhole
2122
swarm
2223
swarm_urlfetcher

0 commit comments

Comments
 (0)
Please sign in to comment.