Skip to content
This repository has been archived by the owner on Jan 16, 2021. It is now read-only.

Commit

Permalink
add a new option to helper_t, called HLP_OPT_CTONDMD, which will make…
Browse files Browse the repository at this point in the history
… 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
mmaxim committed Oct 11, 2012
1 parent bd9b38e commit c5daee9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
2 changes: 2 additions & 0 deletions libaok/ok.h
Expand Up @@ -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 (); }
Expand Down
15 changes: 14 additions & 1 deletion libpub/pslave.h
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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; }
Expand Down Expand Up @@ -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,
Expand Down
37 changes: 25 additions & 12 deletions libpub/slave.C
Expand Up @@ -274,21 +274,34 @@ helper_inet_t::launch_cb (cbb c, ptr<bool> df, int f)
ping (c);
}

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
Expand Down

0 comments on commit c5daee9

Please sign in to comment.