Permalink
Browse files

add a new option to helper_t, called HLP_OPT_CTONDMD, which will make…

… helper_t objects connect once the first call() attempt is made on them. This helps lessen the amount of dead connections that can be created and never used
  • Loading branch information...
1 parent bd9b38e commit c5daee949d3f2e8d884a8204c162562cae4eb0f5 @mmaxim mmaxim committed Oct 11, 2012
Showing with 41 additions and 13 deletions.
  1. +2 −0 libaok/ok.h
  2. +14 −1 libpub/pslave.h
  3. +25 −12 libpub/slave.C
View
@@ -690,6 +690,8 @@ class dbcon_t : public helper_inet_t {
public:
dbcon_t (const rpc_program &g, const str &h, u_int p)
: helper_inet_t (g, h, p, 0) {}
+ dbcon_t (const rpc_program &g, const str &h, u_int p, u_int o)
+ : helper_inet_t (g, h, p, o) {}
str getname () const
{ return strbuf ("database: ") << helper_inet_t::getname (); }
View
@@ -47,6 +47,7 @@ typedef enum { HLP_STATUS_NONE = 0,
#define HLP_OPT_PING (1 << 1) // ping helper on startup
#define HLP_OPT_NORETRY (1 << 2) // do not try to reconnect
#define HLP_OPT_CNCT1 (1 << 3) // connect only once on startup
+#define HLP_OPT_CTONDMD (1 << 4) // connect on first call()
#define HLP_MAX_RETRIES 100
#define HLP_RETRY_DELAY 4
@@ -83,6 +84,7 @@ class helper_base_t {
protected:
};
+
class helper_t : public helper_base_t {
public:
helper_t (const rpc_program &rp, u_int o = 0)
@@ -92,6 +94,17 @@ class helper_t : public helper_base_t {
retries (0), calls (0), status (HLP_STATUS_NONE),
opts (o), destroyed (New refcounted<bool> (false)) {}
+ struct connect_params_t {
+ connect_params_t(u_int32_t p, const void* i, void* o, aclnt_cb c,
+ time_t d) : procno(p), in(i), out(o), cb(c),
+ duration(d) { }
+ u_int32_t procno;
+ const void* in;
+ void* out;
+ aclnt_cb cb;
+ time_t duration;
+ };
+
virtual ~helper_t ();
virtual vec<str> *get_argv () { return NULL; }
int getfd () const { return fd; }
@@ -130,7 +143,7 @@ class helper_t : public helper_base_t {
void kill_aclnt_priv();
void retried (ptr<bool> df, bool b);
void connected (cbb::ptr cb, ptr<bool> df, bool b);
-
+ void call_connect_cb(connect_params_t cp, bool success);
void process_queue ();
void docall (u_int32_t procno, const void *in, void *out, aclnt_cb cb,
View
@@ -275,20 +275,33 @@ helper_inet_t::launch_cb (cbb c, ptr<bool> df, int f)
}
void
+helper_t::call_connect_cb(connect_params_t cp, bool success) {
+ if (!success) {
+ (*cp.cb)(RPC_PROCUNAVAIL);
+ } else
+ docall(cp.procno, cp.in, cp.out, cp.cb, cp.duration);
+}
+
+void
helper_t::call (u_int32_t procno, const void *in, void *out, aclnt_cb cb,
- time_t duration)
-{
- if (status != HLP_STATUS_OK || calls >= max_calls) {
- if ((opts & HLP_OPT_QUEUE) && (status != HLP_STATUS_HOSED) &&
- queue.size () < max_qlen) {
- queue.push_back (New queued_call_t (procno, in, out, cb, duration));
- return;
- } else {
- (*cb) (RPC_PROCUNAVAIL);
- return;
+ time_t duration)
+{
+ if (status != HLP_STATUS_OK || calls >= max_calls) {
+ if ((opts & HLP_OPT_QUEUE) && (status != HLP_STATUS_HOSED) &&
+ queue.size () < max_qlen) {
+ queue.push_back (New queued_call_t (procno, in, out, cb, duration));
+ return;
+ } else if (opts & HLP_OPT_CTONDMD && status != HLP_STATUS_HOSED &&
+ status != HLP_STATUS_RETRY) {
+ connect_params_t cp(procno, in, out, cb, duration);
+ connect( wrap(this, &helper_t::call_connect_cb, cp) );
+ return;
+ } else {
+ (*cb) (RPC_PROCUNAVAIL);
+ return;
+ }
}
- }
- docall (procno, in, out, cb, duration);
+ docall (procno, in, out, cb, duration);
}
void

0 comments on commit c5daee9

Please sign in to comment.