MCPcopy Create free account
hub / github.com/apache/kvrocks / SockSendWithTimeout

Function SockSendWithTimeout

src/common/io_util.cc:474–514  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

472}
473
474Status SockSendWithTimeout(int fd, const std::string &data, int timeout_ms) {
475 // Fall back to blocking send if timeout is non-positive
476 if (timeout_ms <= 0) {
477 return SockSend(fd, data);
478 }
479
480 ssize_t n = 0;
481 auto start = std::chrono::steady_clock::now();
482
483 while (n < static_cast<ssize_t>(data.size())) {
484 // Check if we've exceeded the timeout
485 auto elapsed =
486 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
487 if (elapsed >= timeout_ms) {
488 return {Status::NotOK, fmt::format("send timeout after {} ms, sent {} of {} bytes", elapsed, n, data.size())};
489 }
490
491 // Calculate remaining timeout
492 int remaining_ms = timeout_ms - static_cast<int>(elapsed);
493
494 // Wait for socket to be writable with timeout
495 int ready = AeWait(fd, AE_WRITABLE, remaining_ms);
496 if (ready == 0) {
497 return {Status::NotOK, fmt::format("send timeout waiting for socket, sent {} of {} bytes", n, data.size())};
498 }
499 if (ready < 0) {
500 return Status::FromErrno("poll error while sending");
501 }
502
503 ssize_t nwritten = write(fd, data.data() + n, data.size() - n);
504 if (nwritten == -1) {
505 if (errno == EAGAIN || errno == EWOULDBLOCK) {
506 // Socket buffer is full, continue waiting
507 continue;
508 }
509 return Status::FromErrno();
510 }
511 n += nwritten;
512 }
513 return Status::OK();
514}
515
516Status SockSendWithTimeout(int fd, const std::string &data, [[maybe_unused]] bufferevent *bev, int timeout_ms) {
517 // Fall back to blocking send if timeout is non-positive

Callers 1

loopMethod · 0.85

Calls 5

SockSendFunction · 0.85
AeWaitFunction · 0.85
SockSetBlockingFunction · 0.85
MakeScopeExitFunction · 0.85
IsOKMethod · 0.45

Tested by

no test coverage detected