Skip to content

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

@joke-lee

Description

@joke-lee
#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 次?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions