this method will be called by keeper_waiter for adding one request task for the keeper_conns with one given addr.
| 58 | // this method will be called by keeper_waiter for adding one request task |
| 59 | // for the keeper_conns with one given addr. |
| 60 | void keeper_conns::add_task(task_req& task) |
| 61 | { |
| 62 | // try to peek one connection from the connections pool, if found |
| 63 | // one, move it the task and set hit flag with true, or create one |
| 64 | // fiber to connect the server addr and set hit flag with false. |
| 65 | |
| 66 | keeper_conn* fb = peek_ready(); |
| 67 | if (fb) { |
| 68 | task.set_hit(true); |
| 69 | double cost; |
| 70 | // peek the real connection from fiber |
| 71 | socket_stream* conn = fb->peek(cost); |
| 72 | if (conn == NULL) { |
| 73 | logger_error("maybe the connection was closed!"); |
| 74 | } |
| 75 | task.set_conn_cost(cost); |
| 76 | |
| 77 | // notify request task the connection got now |
| 78 | task.put(conn); |
| 79 | // notify keeper fiber re-connect the server |
| 80 | fb->ask_open(); |
| 81 | logger_debug(FIBER_DEBUG_KEEPER, 3, "[debug] addr=%s, hit", |
| 82 | addr_.c_str()); |
| 83 | } else { |
| 84 | // if no connection got, create one fiber to real connect |
| 85 | // the remote server addr now |
| 86 | task.set_hit(false); |
| 87 | fb = new keeper_conn(config_, addr_, NULL, *this); |
| 88 | task.set_stamp(); |
| 89 | |
| 90 | // set the request task, when the fiber started, |
| 91 | // the request task will be executed directly. |
| 92 | fb->set_task(task); |
| 93 | fb->start(); |
| 94 | |
| 95 | logger_debug(FIBER_DEBUG_KEEPER, 3, "[debug] addr=%s, miss", |
| 96 | addr_.c_str()); |
| 97 | |
| 98 | if (time(NULL) - last_trigger_ >= 1) { |
| 99 | trigger_more(); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void keeper_conns::stop() |
| 105 | { |