forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketUtils.cpp
161 lines (151 loc) · 5.21 KB
/
SocketUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "SocketUtils.h"
#include "WdtOptions.h"
#include "Reporting.h"
#include "ErrorCodes.h"
#include <glog/logging.h>
#include <folly/Conv.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <algorithm>
namespace facebook {
namespace wdt {
/* static */
int SocketUtils::getReceiveBufferSize(int fd) {
int size;
socklen_t sizeSize = sizeof(size);
getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&size, &sizeSize);
return size;
}
/* static */
bool SocketUtils::getNameInfo(const struct sockaddr *sa, socklen_t salen,
std::string &host, std::string &port) {
char hostBuf[NI_MAXHOST], portBuf[NI_MAXSERV];
int res = getnameinfo(sa, salen, hostBuf, sizeof(hostBuf), portBuf,
sizeof(portBuf), NI_NUMERICHOST | NI_NUMERICSERV);
if (res) {
LOG(ERROR) << "getnameinfo failed " << gai_strerror(res);
return false;
}
host = std::string(hostBuf);
port = std::string(portBuf);
return true;
}
/* static */
void SocketUtils::setReadTimeout(int fd) {
const auto &options = WdtOptions::get();
int timeout = getTimeout(options.read_timeout_millis);
if (timeout > 0) {
struct timeval tv;
tv.tv_sec = timeout / 1000; // milli to sec
tv.tv_usec = (timeout % 1000) * 1000; // milli to micro
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
sizeof(struct timeval));
}
}
/* static */
void SocketUtils::setWriteTimeout(int fd) {
const auto &options = WdtOptions::get();
int timeout = getTimeout(options.write_timeout_millis);
if (timeout > 0) {
struct timeval tv;
tv.tv_sec = timeout / 1000; // milli to sec
tv.tv_usec = (timeout % 1000) * 1000; // milli to micro
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
sizeof(struct timeval));
}
}
/* static */
int SocketUtils::getTimeout(int networkTimeout) {
const auto &options = WdtOptions::get();
int abortInterval = options.abort_check_interval_millis;
if (abortInterval <= 0) {
return networkTimeout;
}
if (networkTimeout <= 0) {
return abortInterval;
}
return std::min(networkTimeout, abortInterval);
}
int64_t SocketUtils::readWithAbortCheck(
int fd, char *buf, int64_t nbyte,
WdtBase::IAbortChecker const *abortChecker, bool tryFull) {
const auto &options = WdtOptions::get();
START_PERF_TIMER
int64_t numRead = ioWithAbortCheck(read, fd, buf, nbyte, abortChecker,
options.read_timeout_millis, tryFull);
RECORD_PERF_RESULT(PerfStatReport::SOCKET_READ);
return numRead;
}
int64_t SocketUtils::writeWithAbortCheck(
int fd, const char *buf, int64_t nbyte,
WdtBase::IAbortChecker const *abortChecker, bool tryFull) {
const auto &options = WdtOptions::get();
START_PERF_TIMER
int64_t written = ioWithAbortCheck(write, fd, buf, nbyte, abortChecker,
options.write_timeout_millis, tryFull);
RECORD_PERF_RESULT(PerfStatReport::SOCKET_WRITE)
return written;
}
template <typename F, typename T>
int64_t SocketUtils::ioWithAbortCheck(
F readOrWrite, int fd, T tbuf, int64_t numBytes,
WdtBase::IAbortChecker const *abortChecker, int timeoutMs, bool tryFull) {
WDT_CHECK(abortChecker != nullptr) << "abort checker can not be null";
const auto &options = WdtOptions::get();
bool checkAbort = (options.abort_check_interval_millis > 0);
auto startTime = Clock::now();
int64_t doneBytes = 0;
int retries = 0;
while (doneBytes < numBytes) {
const int64_t ret = readOrWrite(fd, tbuf + doneBytes, numBytes - doneBytes);
if (ret < 0) {
// error
if (errno != EINTR && errno != EAGAIN) {
PLOG(ERROR) << "non-retryable error encountered during socket io " << fd
<< " " << doneBytes << " " << retries;
return (doneBytes > 0 ? doneBytes : ret);
}
} else if (ret == 0) {
// eof
VLOG(1) << "EOF received during socket io. fd : " << fd
<< ", finished bytes : " << doneBytes
<< ", retries : " << retries;
return doneBytes;
} else {
// success
doneBytes += ret;
if (!tryFull) {
// do not have to read/write entire data
return doneBytes;
}
}
if (checkAbort && abortChecker->shouldAbort()) {
LOG(ERROR) << "transfer aborted during socket io " << fd << " "
<< doneBytes << " " << retries;
return (doneBytes > 0 ? doneBytes : -1);
}
if (timeoutMs > 0) {
int duration = durationMillis(Clock::now() - startTime);
if (duration >= timeoutMs) {
LOG(INFO) << "socket io timed out after " << duration << " ms, retries "
<< retries << " fd " << fd << " doneBytes " << doneBytes;
return (doneBytes > 0 ? doneBytes : -1);
}
}
retries++;
}
VLOG_IF(1, retries > 1) << "socket io for " << doneBytes << " bytes took "
<< retries << " retries";
return doneBytes;
}
}
}