| 54 | } |
| 55 | |
| 56 | int 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 | |
| 104 | void IORouter::MarkFailure() { |
| 105 | } |
no test coverage detected