for (const auto& carbonServer : carbonServers) {
ComboAddress remote(carbonServer, 2003);
- Socket s(remote.sin4.sin_family, SOCK_STREAM);
- s.setNonBlocking();
- s.connect(remote); // we do the connect so the attempt happens while we gather stats
try {
+ Socket s(remote.sin4.sin_family, SOCK_STREAM);
+ s.setNonBlocking();
+ s.connect(remote, 2);
+
writen2WithTimeout(s.getHandle(), msg.c_str(), msg.length(), 2);
} catch (runtime_error &e){
L<<Logger::Warning<<"Unable to write data to carbon server at "<<remote.toStringWithPort()<<": "<<e.what()<<endl;
}
#endif
//! Connect the socket to a specified endpoint
- void connect(const ComboAddress &ep)
+ void connect(const ComboAddress &ep, int timeout=0)
{
- if(::connect(d_socket,(struct sockaddr *)&ep, ep.getSocklen()) < 0 && errno != EINPROGRESS)
- throw NetworkError("While connecting to "+ep.toStringWithPort()+": "+string(strerror(errno)));
+ if(::connect(d_socket,(struct sockaddr *)&ep, ep.getSocklen()) < 0) {
+ if(errno == EINPROGRESS) {
+ if (timeout > 0) {
+ /* if a timeout is provided, we wait until the connection has been established */
+ int res = waitForRWData(d_socket, false, timeout, 0);
+ if (res == 0) {
+ throw NetworkError("timeout while connecting to "+ep.toStringWithPort());
+ } else if (res < 0) {
+ throw NetworkError("while waiting to connect to "+ep.toStringWithPort()+": "+string(strerror(errno)));
+ }
+ }
+ }
+ else {
+ throw NetworkError("While connecting to "+ep.toStringWithPort()+": "+string(strerror(errno)));
+ }
+ }
}