Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义重试次数没有生效 #2860

Open
joke-lee opened this issue Jan 3, 2025 · 2 comments
Open

自定义重试次数没有生效 #2860

joke-lee opened this issue Jan 3, 2025 · 2 comments

Comments

@joke-lee
Copy link

joke-lee commented Jan 3, 2025

#include <iostream>
#include <string>
//#include <braft/protobuf_file.h>
//#include <braft/raft.h>
//#include <braft/route_table.h>
//#include <braft/storage.h>
//#include <braft/util.h>
//#include <braft/cli.h>
//#include <braft/cli.pb.h>
#include <brpc/channel.h>
#include <brpc/controller.h>
#include <brpc/server.h>
#include <bthread/bthread.h>
#include <butil/logging.h>
#include <bvar/bvar.h>

using namespace std;

int main() {
  brpc::ChannelOptions options;
  options.protocol = "http";
  options.max_retry = 100;
  options.connect_timeout_ms = 30;
  options.timeout_ms = 1000;
  string endpoint = "10.0.0.1:8000";
  brpc::Channel channel;
  brpc::Controller cntl;
  cntl.set_max_retry(30);
  if (channel.Init(endpoint.c_str(), "", &options) != 0) {
    return 0;
  }
  cntl.http_request().uri() = endpoint;  // Request URL
  cntl.http_request().set_method(brpc::HTTP_METHOD_GET);
  channel.CallMethod(NULL, &cntl, NULL, NULL, NULL/*done*/);
  if (cntl.Failed()) {
//cntl.ErrorCode() != brpc::EHTTP
    std::cout << " cntl.max_retry=" << cntl.max_retry()
      << " retry_count=" << cntl.retried_count() << " ErrorText=" << cntl.ErrorText() << ", ErrorCode=" << cntl.ErrorCode() << std::endl;
  } else {
    std::cout << "success" << std::endl;
  }
  return 0;
}

设置一个网络不可达的地址 10.0.0.1:8000

上面设置了重试 30 次,但是实际测试结果只有重试 2 次

cntl.max_retry=30 retry_count=2 ErrorText=[E110]Fail to connect Socket{id=1 addr=10.0.0.1:8000} (0x0x1815a00): Connection timed out [R1][E110]Fail to connect Socket{id=102 addr=10.0.0.1:8000} (0x0x7f7d7c00bfa0): Connection timed out [R2][E101]Fail to connect Socket{id=8589934594 addr=10.0.0.1:8000} (0x0x1815c80): Network is unreachable, ErrorCode=101

但是重试设置 1 次

#include <iostream>
#include <string>
//#include <braft/protobuf_file.h>
//#include <braft/raft.h>
//#include <braft/route_table.h>
//#include <braft/storage.h>
//#include <braft/util.h>
//#include <braft/cli.h>
//#include <braft/cli.pb.h>
#include <brpc/channel.h>
#include <brpc/controller.h>
#include <brpc/server.h>
#include <bthread/bthread.h>
#include <butil/logging.h>
#include <bvar/bvar.h>

using namespace std;

int main() {
  brpc::ChannelOptions options;
  options.protocol = "http";
  options.max_retry = 100;
  options.connect_timeout_ms = 1;
  options.timeout_ms = 1000;
  string endpoint = "10.0.0.1:8000";
  brpc::Channel channel;
  brpc::Controller cntl;
  cntl.set_max_retry(1);
  if (channel.Init(endpoint.c_str(), "", &options) != 0) {
    return 0;
  }
  cntl.http_request().uri() = endpoint;  // Request URL
  cntl.http_request().set_method(brpc::HTTP_METHOD_GET);
  channel.CallMethod(NULL, &cntl, NULL, NULL, NULL/*done*/);
  if (cntl.Failed()) {
//cntl.ErrorCode() != brpc::EHTTP
    std::cout << " cntl.max_retry=" << cntl.max_retry()
      << " retry_count=" << cntl.retried_count() << " ErrorText=" << cntl.ErrorText() << ", ErrorCode=" << cntl.ErrorCode() << std::endl;
  } else {
    std::cout << "success" << std::endl;
  }
  return 0;
}

看报错信息是符合预期,只重试了一次

/root/CLionProjects/untitled/cmake-build-debug/untitled
 cntl.max_retry=1 retry_count=1 ErrorText=[E110]Fail to connect Socket{id=1 addr=10.0.0.1:8000} (0x0xb11a00): Connection timed out [R1][E110]Fail to connect Socket{id=102 addr=10.0.0.1:8000} (0x0x7f092800c020): Connection timed out, ErrorCode=110

Process finished with exit code 0

是不是重试次数只能是 0 1 2 才有效果?,即使设置重试的大于 3 次,最多也只能重试 2 次?

@joke-lee
Copy link
Author

joke-lee commented Jan 3, 2025

@chenBright 用的是 apache-brpc-1.8.0-src ,能帮忙看看么?

@chenBright
Copy link
Contributor

#2295 不可达不重试。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants