Skip to content

Commit 25c82f1

Browse files
committed
fix pr
Signed-off-by: adi_holden <[email protected]>
1 parent 8a21890 commit 25c82f1

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

src/facade/dragonfly_listener.cc

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,22 @@ SSL_CTX* CreateSslServerCntx() {
101101
}
102102
#endif
103103

104-
bool ConfigureKeepAlive(int fd, unsigned interval_sec) {
105-
DCHECK_GT(interval_sec, 3u);
104+
uint32_t tcp_keepalive;
105+
106+
bool ConfigureKeepAlive(int fd) {
107+
DCHECK_GT(tcp_keepalive, 3u);
106108
int val = 1;
107109
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) < 0)
108110
return false;
109111

110-
val = interval_sec;
112+
val = tcp_keepalive;
111113
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0)
112114
return false;
113115

114116
/* Send next probes after the specified interval. Note that we set the
115117
* delay as interval / 3, as we send three probes before detecting
116118
* an error (see the next setsockopt call). */
117-
val = interval_sec / 3;
119+
val = tcp_keepalive / 3;
118120
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0)
119121
return false;
120122

@@ -141,21 +143,17 @@ Listener::Listener(Protocol protocol, ServiceInterface* si) : service_(si), prot
141143
http_base_->set_resource_prefix("http://static.dragonflydb.io/data-plane");
142144
si->ConfigureHttpHandlers(http_base_.get());
143145

144-
constexpr string_view kTcpFlagError = "tcp_keepalive expected to be greated than 3 seconds";
145-
tcp_keepalive_ = absl::GetFlag(FLAGS_tcp_keepalive);
146-
CHECK_GT(tcp_keepalive_, 3u) << kTcpFlagError;
147-
dfly::config_registry.Register("tcp_keepalive", [this](const absl::CommandLineFlag& flag) {
148-
auto res = flag.TryGet<uint32_t>();
149-
if (!res)
150-
return false;
151-
152-
if (*res <= 3) {
153-
LOG(ERROR) << kTcpFlagError;
154-
return false;
155-
}
156-
tcp_keepalive_ = *res;
157-
return true;
158-
});
146+
tcp_keepalive = absl::GetFlag(FLAGS_tcp_keepalive);
147+
CHECK_GT(tcp_keepalive, 3u) << FLAGS_tcp_keepalive.Name() << " " << FLAGS_tcp_keepalive.Help();
148+
}
149+
150+
bool Listener::SetTcpKeepalive(uint32_t val) {
151+
if (val <= 3) {
152+
LOG(ERROR) << FLAGS_tcp_keepalive.Name() << " " << FLAGS_tcp_keepalive.Help();
153+
return false;
154+
}
155+
tcp_keepalive = val;
156+
return true;
159157
}
160158

161159
Listener::~Listener() {
@@ -175,7 +173,7 @@ error_code Listener::ConfigureServerSocket(int fd) {
175173
LOG(WARNING) << "Could not set reuse addr on socket " << SafeErrorMessage(errno);
176174
}
177175

178-
bool success = ConfigureKeepAlive(fd, tcp_keepalive_);
176+
bool success = ConfigureKeepAlive(fd);
179177

180178
if (!success) {
181179
int myerr = errno;

src/facade/dragonfly_listener.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Listener : public util::ListenerInterface {
3434
// run out. Returns true if the all connections have stopped dispatching.
3535
bool AwaitDispatches(absl::Duration timeout,
3636
const std::function<bool(util::Connection*)>& filter);
37+
static bool SetTcpKeepalive(uint32_t val);
3738

3839
private:
3940
util::Connection* NewConnection(ProactorBase* proactor) final;
@@ -65,7 +66,6 @@ class Listener : public util::ListenerInterface {
6566

6667
Protocol protocol_;
6768
SSL_CTX* ctx_ = nullptr;
68-
uint32_t tcp_keepalive_;
6969
};
7070

7171
} // namespace facade

src/server/main_service.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,12 @@ void Service::Init(util::AcceptServer* acceptor, std::vector<facade::Listener*>
622622
config_registry.Register("dir");
623623
config_registry.Register("requirepass");
624624
config_registry.Register("masterauth");
625+
config_registry.Register("tcp_keepalive", [](const absl::CommandLineFlag& flag) {
626+
auto res = flag.TryGet<uint32_t>();
627+
if (!res)
628+
return false;
629+
return Listener::SetTcpKeepalive(*res);
630+
});
625631

626632
pp_.Await([](uint32_t index, ProactorBase* pb) { ServerState::Init(index); });
627633

0 commit comments

Comments
 (0)