Skip to content

Commit

Permalink
traditionTcpRelay
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyoko-Jeremie committed Aug 28, 2023
1 parent 8217a57 commit 9425a54
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
17 changes: 16 additions & 1 deletion src/DelayCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace DelayCollection {
return std::strong_ordering::equal;
}

DelayInfo(TimeMs delay) : delay(delay), timeClock(nowTimePointClock()) {}
explicit DelayInfo(TimeMs delay) : delay(delay), timeClock(nowTimePointClock()) {}

DelayInfo &operator=(const DelayInfo &o) = default;

Expand Down Expand Up @@ -141,6 +141,12 @@ namespace DelayCollection {
TimeHistory historyHttpPing;
TimeHistory historyRelayFirstDelay;

bool traditionTcpRelay;

public:

explicit DelayCollect(bool traditionTcpRelay) : traditionTcpRelay(traditionTcpRelay) {}

public:

std::deque<TimeHistory::DelayInfo> getHistoryTcpPing() {
Expand Down Expand Up @@ -182,16 +188,25 @@ namespace DelayCollection {
public:
void pushTcpPing(TimeMs t) {
lastTcpPing = t;
if (traditionTcpRelay) {
return;
}
historyTcpPing.addDelayInfo(t);
}

void pushHttpPing(TimeMs t) {
lastHttpPing = t;
if (traditionTcpRelay) {
return;
}
historyHttpPing.addDelayInfo(t);
}

void pushRelayFirstDelay(TimeMs t) {
lastRelayFirstDelay = t;
if (traditionTcpRelay) {
return;
}
historyRelayFirstDelay.addDelayInfo(t);
}

Expand Down
64 changes: 34 additions & 30 deletions src/TcpRelaySession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,21 +296,23 @@ void TcpRelaySession::do_upstream_read() {
if (ct) {
ct->relayGotoDown(upstream_data_, bytes_transferred);
}
if (!firstDelayUpEnd) {
firstDelayUpEnd = true;
int64_t tt = -1;
int64_t ttt = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
if (firstDelayTimestamp.compare_exchange_strong(tt, ttt)) {
// firstDelayTimestamp is first write
// the first package timestamp write ok
/*empty*/
} else {
// firstDelayTimestamp have first package timestamp now
int64_t d = ttt - tt;
nowServer->delayCollect->pushRelayFirstDelay(
std::chrono::milliseconds{d}
);
if (!traditionTcpRelay) {
if (!firstDelayUpEnd) {
firstDelayUpEnd = true;
int64_t tt = -1;
int64_t ttt = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
if (firstDelayTimestamp.compare_exchange_strong(tt, ttt)) {
// firstDelayTimestamp is first write
// the first package timestamp write ok
/*empty*/
} else {
// firstDelayTimestamp have first package timestamp now
int64_t d = ttt - tt;
nowServer->delayCollect->pushRelayFirstDelay(
std::chrono::milliseconds{d}
);
}
}
}

Expand Down Expand Up @@ -358,21 +360,23 @@ void TcpRelaySession::do_downstream_read() {
if (ct) {
ct->relayGotoUp(downstream_data_, bytes_transferred);
}
if (!firstDelayDownEnd) {
firstDelayDownEnd = true;
int64_t tt = -1;
int64_t ttt = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
if (firstDelayTimestamp.compare_exchange_strong(tt, ttt)) {
// firstDelayTimestamp is first write
// the first package timestamp write ok
/*empty*/
} else {
// firstDelayTimestamp have first package timestamp now
int64_t d = ttt - tt;
nowServer->delayCollect->pushRelayFirstDelay(
std::chrono::milliseconds{d}
);
if (!traditionTcpRelay) {
if (!firstDelayDownEnd) {
firstDelayDownEnd = true;
int64_t tt = -1;
int64_t ttt = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
if (firstDelayTimestamp.compare_exchange_strong(tt, ttt)) {
// firstDelayTimestamp is first write
// the first package timestamp write ok
/*empty*/
} else {
// firstDelayTimestamp have first package timestamp now
int64_t d = ttt - tt;
nowServer->delayCollect->pushRelayFirstDelay(
std::chrono::milliseconds{d}
);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/UpstreamPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void UpstreamPool::setConfig(std::shared_ptr<ConfigLoader> configLoader) {
i, r.name,
r.host, r.port,
r.authUser, r.authPwd,
r.disable, r.slowImpl
r.disable, r.slowImpl,
_configLoader->config.traditionTcpRelay
);
_pool.push_back(u);
}
Expand Down
10 changes: 7 additions & 3 deletions src/UpstreamPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ struct UpstreamServer : public std::enable_shared_from_this<UpstreamServer> {
std::chrono::milliseconds lastOnlinePing{-1};
std::chrono::milliseconds lastConnectPing{-1};

std::shared_ptr<DelayCollection::DelayCollect> delayCollect = std::make_shared<DelayCollection::DelayCollect>();
bool traditionTcpRelay;
std::shared_ptr<DelayCollection::DelayCollect> delayCollect;

UpstreamServer(
size_t index,
Expand All @@ -81,7 +82,8 @@ struct UpstreamServer : public std::enable_shared_from_this<UpstreamServer> {
std::string authUser,
std::string authPwd,
bool disable,
bool slowImpl
bool slowImpl,
bool traditionTcpRelay
) :
host(std::move(host)),
port(port),
Expand All @@ -91,7 +93,9 @@ struct UpstreamServer : public std::enable_shared_from_this<UpstreamServer> {
authPwd(std::move(authPwd)),
isManualDisable(disable),
disable(disable),
slowImpl(slowImpl) {}
slowImpl(slowImpl),
traditionTcpRelay(traditionTcpRelay),
delayCollect(std::make_shared<DelayCollection::DelayCollect>(traditionTcpRelay)) {}

std::string print();

Expand Down

0 comments on commit 9425a54

Please sign in to comment.