MCPcopy Create free account
hub / github.com/Tencent/phxsql / ConnectDest

Method ConnectDest

phxsqlproxy/io_router.cpp:56–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54}
55
56int IORouter::ConnectDest() {
57 std::string dest_ip = "";
58 int dest_port = 0;
59
60 if (!config_->GetOnlyProxy()) {
61 if (GetDestEndpoint(dest_ip, dest_port) != 0) {
62 LOG_ERR("get mysql ip port failed");
63 return -__LINE__;
64 }
65 } else {
66 dest_ip = "127.0.0.1";
67 dest_port = config_->GetMysqlPort();
68 }
69
70 MonitorPluginEntry::GetDefault()->GetMonitorPlugin()->ConnectDest();
71
72 int fd = socket(PF_INET, SOCK_STREAM, 0);
73
74 struct sockaddr_in addr;
75 if (SetAddr(dest_ip.c_str(), dest_port, addr) != 0) {
76 return -__LINE__;
77 }
78 uint64_t begin = GetTimestampMS();
79
80 SetNonBlock(fd);
81
82 int ret = RoutineConnectWithTimeout(fd, (struct sockaddr*) &addr, sizeof(addr), config_->ConnectTimeoutMs());
83 if (ret != 0) {
84 LOG_ERR("connect [%s:%d] ret %d, errno (%d:%s)", dest_ip.c_str(), dest_port, ret, errno, strerror(errno));
85 MonitorPluginEntry::GetDefault()->GetMonitorPlugin()->ConnectDestFail();
86 close(fd);
87 fd = -__LINE__;
88 } else {
89 LOG_DEBUG("connect [%s:%d] ret fd %d", dest_ip.c_str(), dest_port, fd);
90 connect_dest_ = dest_ip;
91 connect_port_ = dest_port;
92 }
93
94 if (dest_port == config_->GetMysqlPort()) {
95 uint64_t cost = GetTimestampMS() - begin;
96 LOG_DEBUG("connect [%s:%d] ret fd %d cost %llu", dest_ip.c_str(), dest_port, fd, cost);
97 MonitorPluginEntry::GetDefault()->GetMonitorPlugin()->ConnectMysqlSvr(fd);
98 MonitorPluginEntry::GetDefault()->GetMonitorPlugin()->ConnectMysqlSvrRunTime(cost);
99 }
100
101 return fd;
102}
103
104void IORouter::MarkFailure() {
105}

Callers 1

runMethod · 0.45

Calls 11

SetAddrFunction · 0.85
GetTimestampMSFunction · 0.85
SetNonBlockFunction · 0.85
GetOnlyProxyMethod · 0.80
GetMysqlPortMethod · 0.80
GetMonitorPluginMethod · 0.80
ConnectTimeoutMsMethod · 0.80
ConnectDestFailMethod · 0.80
ConnectMysqlSvrMethod · 0.80

Tested by

no test coverage detected