Commit 09339280 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Call VTCP_open/close through method pointers

parent 688ccfce
...@@ -44,30 +44,82 @@ ...@@ -44,30 +44,82 @@
#include "cache_tcp_pool.h" #include "cache_tcp_pool.h"
#include "cache_pool.h" #include "cache_pool.h"
typedef int cp_open_f(const struct tcp_pool *, double tmo, const void **privp);
typedef void cp_close_f(struct pfd *);
struct cp_methods {
cp_open_f *open;
cp_close_f *close;
};
struct tcp_pool { struct tcp_pool {
unsigned magic; unsigned magic;
#define TCP_POOL_MAGIC 0x28b0e42a #define TCP_POOL_MAGIC 0x28b0e42a
const struct cp_methods *methods;
const void *id; const void *id;
struct suckaddr *ip4; struct suckaddr *ip4;
struct suckaddr *ip6; struct suckaddr *ip6;
VTAILQ_ENTRY(tcp_pool) list; VTAILQ_ENTRY(tcp_pool) list;
int refcnt; int refcnt;
struct lock mtx; struct lock mtx;
VTAILQ_HEAD(, pfd) connlist; VTAILQ_HEAD(, pfd) connlist;
int n_conn; int n_conn;
VTAILQ_HEAD(, pfd) killlist; VTAILQ_HEAD(, pfd) killlist;
int n_kill; int n_kill;
int n_used; int n_used;
}; };
static struct lock tcp_pools_mtx; static struct lock tcp_pools_mtx;
static VTAILQ_HEAD(, tcp_pool) tcp_pools = VTAILQ_HEAD_INITIALIZER(tcp_pools); static VTAILQ_HEAD(, tcp_pool) tcp_pools = VTAILQ_HEAD_INITIALIZER(tcp_pools);
/*--------------------------------------------------------------------
*/
static int v_matchproto_(cp_open_f)
vtp_open(const struct tcp_pool *tp, double tmo, const void **privp)
{
int s;
int msec;
CHECK_OBJ_NOTNULL(tp, TCP_POOL_MAGIC);
msec = (int)floor(tmo * 1000.0);
if (cache_param->prefer_ipv6) {
*privp = tp->ip6;
s = VTCP_connect(tp->ip6, msec);
if (s >= 0)
return (s);
}
*privp = tp->ip4;
s = VTCP_connect(tp->ip4, msec);
if (s >= 0)
return (s);
if (!cache_param->prefer_ipv6) {
*privp = tp->ip6;
s = VTCP_connect(tp->ip6, msec);
}
return (s);
}
static void v_matchproto_(cp_close_f)
vtp_close(struct pfd *pfd)
{
CHECK_OBJ_NOTNULL(pfd, PFD_MAGIC);
VTCP_close(&pfd->fd);
}
static const struct cp_methods vtp_methods = {
.open = vtp_open,
.close = vtp_close,
};
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
* Waiter-handler * Waiter-handler
*/ */
...@@ -94,13 +146,13 @@ tcp_handle(struct waited *w, enum wait_event ev, double now) ...@@ -94,13 +146,13 @@ tcp_handle(struct waited *w, enum wait_event ev, double now)
AZ(pthread_cond_signal(pfd->cond)); AZ(pthread_cond_signal(pfd->cond));
break; break;
case PFD_STATE_AVAIL: case PFD_STATE_AVAIL:
VTCP_close(&pfd->fd); tp->methods->close(pfd);
VTAILQ_REMOVE(&tp->connlist, pfd, list); VTAILQ_REMOVE(&tp->connlist, pfd, list);
tp->n_conn--; tp->n_conn--;
FREE_OBJ(pfd); FREE_OBJ(pfd);
break; break;
case PFD_STATE_CLEANUP: case PFD_STATE_CLEANUP:
VTCP_close(&pfd->fd); tp->methods->close(pfd);
tp->n_kill--; tp->n_kill--;
VTAILQ_REMOVE(&tp->killlist, pfd, list); VTAILQ_REMOVE(&tp->killlist, pfd, list);
memset(pfd, 0x11, sizeof *pfd); memset(pfd, 0x11, sizeof *pfd);
...@@ -154,6 +206,7 @@ VTP_Ref(const struct suckaddr *ip4, const struct suckaddr *ip6, const void *id) ...@@ -154,6 +206,7 @@ VTP_Ref(const struct suckaddr *ip4, const struct suckaddr *ip6, const void *id)
ALLOC_OBJ(tp, TCP_POOL_MAGIC); ALLOC_OBJ(tp, TCP_POOL_MAGIC);
AN(tp); AN(tp);
tp->methods = &vtp_methods;
if (ip4 != NULL) if (ip4 != NULL)
tp->ip4 = VSA_Clone(ip4); tp->ip4 = VSA_Clone(ip4);
if (ip6 != NULL) if (ip6 != NULL)
...@@ -241,27 +294,8 @@ VTP_Rel(struct tcp_pool **tpp) ...@@ -241,27 +294,8 @@ VTP_Rel(struct tcp_pool **tpp)
int int
VTP_Open(const struct tcp_pool *tp, double tmo, const void **privp) VTP_Open(const struct tcp_pool *tp, double tmo, const void **privp)
{ {
int s;
int msec;
CHECK_OBJ_NOTNULL(tp, TCP_POOL_MAGIC); return (vtp_open(tp, tmo, privp));
msec = (int)floor(tmo * 1000.0);
if (cache_param->prefer_ipv6) {
*privp = tp->ip6;
s = VTCP_connect(tp->ip6, msec);
if (s >= 0)
return (s);
}
*privp = tp->ip4;
s = VTCP_connect(tp->ip4, msec);
if (s >= 0)
return (s);
if (!cache_param->prefer_ipv6) {
*privp = tp->ip6;
s = VTCP_connect(tp->ip6, msec);
}
return (s);
} }
/*-------------------------------------------------------------------- /*--------------------------------------------------------------------
...@@ -295,7 +329,7 @@ VTP_Recycle(const struct worker *wrk, struct pfd **pfdp) ...@@ -295,7 +329,7 @@ VTP_Recycle(const struct worker *wrk, struct pfd **pfdp)
pfd->waited->func = tcp_handle; pfd->waited->func = tcp_handle;
pfd->waited->tmo = &cache_param->backend_idle_timeout; pfd->waited->tmo = &cache_param->backend_idle_timeout;
if (Wait_Enter(wrk->pool->waiter, pfd->waited)) { if (Wait_Enter(wrk->pool->waiter, pfd->waited)) {
VTCP_close(&pfd->fd); tp->methods->close(pfd);
memset(pfd, 0x33, sizeof *pfd); memset(pfd, 0x33, sizeof *pfd);
free(pfd); free(pfd);
// XXX: stats // XXX: stats
...@@ -356,7 +390,7 @@ VTP_Close(struct pfd **pfdp) ...@@ -356,7 +390,7 @@ VTP_Close(struct pfd **pfdp)
tp->n_kill++; tp->n_kill++;
} else { } else {
assert(pfd->state == PFD_STATE_USED); assert(pfd->state == PFD_STATE_USED);
VTCP_close(&pfd->fd); tp->methods->close(pfd);
memset(pfd, 0x44, sizeof *pfd); memset(pfd, 0x44, sizeof *pfd);
free(pfd); free(pfd);
} }
...@@ -402,7 +436,7 @@ VTP_Get(struct tcp_pool *tp, double tmo, struct worker *wrk, ...@@ -402,7 +436,7 @@ VTP_Get(struct tcp_pool *tp, double tmo, struct worker *wrk,
INIT_OBJ(pfd->waited, WAITED_MAGIC); INIT_OBJ(pfd->waited, WAITED_MAGIC);
pfd->state = PFD_STATE_USED; pfd->state = PFD_STATE_USED;
pfd->tcp_pool = tp; pfd->tcp_pool = tp;
pfd->fd = VTP_Open(tp, tmo, &pfd->priv); pfd->fd = tp->methods->open(tp, tmo, &pfd->priv);
if (pfd->fd < 0) { if (pfd->fd < 0) {
FREE_OBJ(pfd); FREE_OBJ(pfd);
Lck_Lock(&tp->mtx); Lck_Lock(&tp->mtx);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment