| 195 | } |
| 196 | |
| 197 | connect_client* connect_pool::peek(bool on, double* tc, bool* old) |
| 198 | { |
| 199 | struct timeval begin; |
| 200 | |
| 201 | #define SET_TIME_COST do { \ |
| 202 | if (tc) { \ |
| 203 | struct timeval end; \ |
| 204 | gettimeofday(&end, NULL); \ |
| 205 | *tc = stamp_sub(end, begin); \ |
| 206 | } \ |
| 207 | } while (0) |
| 208 | |
| 209 | if (tc) { |
| 210 | *tc = 0.0; |
| 211 | gettimeofday(&begin, NULL); |
| 212 | } |
| 213 | |
| 214 | if (old) { |
| 215 | *old = false; |
| 216 | } |
| 217 | |
| 218 | lock_.lock(); |
| 219 | if (alive_ == false) { |
| 220 | time_t now = time(NULL); |
| 221 | if (retry_inter_ <= 0 || now - last_dead_ < retry_inter_) { |
| 222 | lock_.unlock(); |
| 223 | return NULL; |
| 224 | } |
| 225 | alive_ = true; |
| 226 | |
| 227 | // ���÷��������״̬���Ա����� |
| 228 | logger("reset server: %s", get_addr()); |
| 229 | } |
| 230 | |
| 231 | connect_client* conn; |
| 232 | |
| 233 | std::list<connect_client*>::iterator it = pool_.begin(); |
| 234 | if (it != pool_.end()) { |
| 235 | conn = *it; |
| 236 | pool_.erase(it); |
| 237 | total_used_++; |
| 238 | current_used_++; |
| 239 | |
| 240 | lock_.unlock(); |
| 241 | |
| 242 | SET_TIME_COST; |
| 243 | |
| 244 | if (old) { |
| 245 | *old = true; |
| 246 | } |
| 247 | |
| 248 | return conn; |
| 249 | } else if (max_ > 0 && count_ >= max_) { |
| 250 | logger_error("too many connections, max: %zd, curr: %zd," |
| 251 | " server: %s", max_, count_, addr_); |
| 252 | lock_.unlock(); |
| 253 | |
| 254 | SET_TIME_COST; |
no test coverage detected