[master] f658f830d Unify some of the code from client/server using a "session" concept, which will become more and more important.
Poul-Henning Kamp
phk at FreeBSD.org
Mon Aug 31 14:25:07 UTC 2020
commit f658f830df003e7258cba883c3ccd3a2f2cbfb95
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Mon Aug 31 14:23:06 2020 +0000
Unify some of the code from client/server using a "session"
concept, which will become more and more important.
diff --git a/bin/varnishtest/Makefile.am b/bin/varnishtest/Makefile.am
index 34f11f77e..61c2be73d 100644
--- a/bin/varnishtest/Makefile.am
+++ b/bin/varnishtest/Makefile.am
@@ -47,6 +47,7 @@ varnishtest_SOURCES = \
vtc_process.c \
vtc_proxy.c \
vtc_server.c \
+ vtc_sess.c \
vtc_subr.c \
vtc_syslog.c \
vtc_varnish.c
diff --git a/bin/varnishtest/vtc.h b/bin/varnishtest/vtc.h
index cd21edb75..23a3a6472 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -88,6 +88,27 @@ extern int ign_unknown_macro;
void init_server(void);
void init_syslog(void);
+/* Sessions */
+struct vtc_sess *Sess_New(struct vtclog *vl, const char *name);
+void Sess_Destroy(struct vtc_sess **spp);
+int Sess_GetOpt(struct vtc_sess *, char * const **);
+int sess_process(struct vtclog *vl, const struct vtc_sess *,
+ const char *spec, int sock, int *sfd, const char *addr);
+
+typedef int sess_conn_f(void *priv, struct vtclog *);
+typedef void sess_disc_f(void *priv, struct vtclog *, int *fd);
+pthread_t
+Sess_Start_Thread(
+ void *priv,
+ struct vtc_sess *vsp,
+ sess_conn_f *conn,
+ sess_disc_f *disc,
+ const char *listen_addr,
+ int *asocket,
+ const char *spec
+);
+
+
int http_process(struct vtclog *vl, const char *spec, int sock, int *sfd,
const char *addr, int rcvbuf);
diff --git a/bin/varnishtest/vtc_client.c b/bin/varnishtest/vtc_client.c
index 6ccfbb8d3..41cc3a581 100644
--- a/bin/varnishtest/vtc_client.c
+++ b/bin/varnishtest/vtc_client.c
@@ -52,19 +52,16 @@ struct client {
char *name;
struct vtclog *vl;
VTAILQ_ENTRY(client) list;
+ struct vtc_sess *vsp;
char *spec;
char connect[256];
- const char *addr;
- int rcvbuf;
+ char *addr;
char *proxy_spec;
int proxy_version;
- int repeat;
- unsigned keepalive;
-
unsigned running;
pthread_t tp;
};
@@ -207,54 +204,21 @@ client_connect(struct vtclog *vl, struct client *c)
* Client thread
*/
-static void *
-client_thread(void *priv)
+static int
+client_conn(void *priv, struct vtclog *vl)
{
struct client *c;
- struct vtclog *vl;
- int fd;
- int i;
- struct vsb *vsb;
CAST_OBJ_NOTNULL(c, priv, CLIENT_MAGIC);
- AN(*c->connect);
-
- vl = vtc_logopen(c->name);
- pthread_cleanup_push(vtc_logclose, vl);
+ return (client_connect(vl, c));
+}
- vsb = macro_expand(vl, c->connect);
- AN(vsb);
-#if !defined(__sun)
- pthread_cleanup_push((void (*)(void *))VSB_destroy, &vsb);
-#endif
- c->addr = VSB_data(vsb);
-
- if (c->repeat == 0)
- c->repeat = 1;
- if (c->repeat != 1)
- vtc_log(vl, 2, "Started (%u iterations%s)", c->repeat,
- c->keepalive ? " using keepalive" : "");
- for (i = 0; i < c->repeat; i++) {
- fd = client_connect(vl, c);
-
- if (! c->keepalive)
- fd = http_process(vl, c->spec, fd, NULL, c->addr,
- c->rcvbuf);
- else
- while (fd >= 0 && i++ < c->repeat)
- fd = http_process(vl, c->spec, fd, NULL,
- c->addr, c->rcvbuf);
- vtc_log(vl, 3, "closing fd %d", fd);
- VTCP_close(&fd);
- }
- vtc_log(vl, 2, "Ending");
-#if !defined(__sun)
- pthread_cleanup_pop(0);
-#endif
- pthread_cleanup_pop(0);
- VSB_destroy(&vsb);
- vtc_logclose(vl);
- return (NULL);
+static void
+client_disc(void *priv, struct vtclog *vl, int *fdp)
+{
+ (void)priv;
+ vtc_log(vl, 3, "closing fd %d", *fdp);
+ VTCP_close(fdp);
}
/**********************************************************************
@@ -271,6 +235,8 @@ client_new(const char *name)
REPLACE(c->name, name);
c->vl = vtc_logopen(name);
AN(c->vl);
+ c->vsp = Sess_New(c->vl, name);
+ AN(c->vsp);
bprintf(c->connect, "%s", "${v1_sock}");
VTAILQ_INSERT_TAIL(&clients, c, list);
@@ -286,9 +252,11 @@ client_delete(struct client *c)
{
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC);
+ Sess_Destroy(&c->vsp);
vtc_logclose(c->vl);
free(c->spec);
free(c->name);
+ free(c->addr);
free(c->proxy_spec);
/* XXX: MEMLEAK (?)*/
FREE_OBJ(c);
@@ -301,11 +269,24 @@ client_delete(struct client *c)
static void
client_start(struct client *c)
{
+ struct vsb *vsb;
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC);
vtc_log(c->vl, 2, "Starting client");
- AZ(pthread_create(&c->tp, NULL, client_thread, c));
c->running = 1;
+ vsb = macro_expand(c->vl, c->connect);
+ AN(vsb);
+ REPLACE(c->addr, VSB_data(vsb));
+ VSB_destroy(&vsb);
+ c->tp = Sess_Start_Thread(
+ c,
+ c->vsp,
+ client_conn,
+ client_disc,
+ c->addr,
+ NULL,
+ c->spec
+ );
}
/**********************************************************************
@@ -386,6 +367,10 @@ cmd_client(CMD_ARGS)
if (c->running)
client_wait(c);
+ AZ(c->running);
+ if (Sess_GetOpt(c->vsp, &av))
+ continue;
+
if (!strcmp(*av, "-connect")) {
bprintf(c->connect, "%s", av[1]);
av++;
@@ -403,20 +388,6 @@ cmd_client(CMD_ARGS)
av++;
continue;
}
- if (!strcmp(*av, "-repeat")) {
- c->repeat = atoi(av[1]);
- av++;
- continue;
- }
- if (!strcmp(*av, "-keepalive")) {
- c->keepalive = 1;
- continue;
- }
- if (!strcmp(*av, "-rcvbuf")) {
- c->rcvbuf = atoi(av[1]);
- av++;
- continue;
- }
if (!strcmp(*av, "-start")) {
client_start(c);
continue;
diff --git a/bin/varnishtest/vtc_server.c b/bin/varnishtest/vtc_server.c
index 9ed58d923..4043c27da 100644
--- a/bin/varnishtest/vtc_server.c
+++ b/bin/varnishtest/vtc_server.c
@@ -50,16 +50,14 @@ struct server {
char *name;
struct vtclog *vl;
VTAILQ_ENTRY(server) list;
+ struct vtc_sess *vsp;
char run;
- int repeat;
- unsigned keepalive;
char *spec;
int depth;
int sock;
int fd;
- int rcvbuf;
char listen[256];
char aaddr[32];
char aport[32];
@@ -87,9 +85,10 @@ server_new(const char *name, struct vtclog *vl)
REPLACE(s->name, name);
s->vl = vtc_logopen(s->name);
AN(s->vl);
+ s->vsp = Sess_New(s->vl, name);
+ AN(s->vsp);
bprintf(s->listen, "%s", "127.0.0.1 0");
- s->repeat = 1;
s->depth = 10;
s->sock = -1;
s->fd = -1;
@@ -108,6 +107,7 @@ server_delete(struct server *s)
{
CHECK_OBJ_NOTNULL(s, SERVER_MAGIC);
+ Sess_Destroy(&s->vsp);
macro_undef(s->vl, s->name, "addr");
macro_undef(s->vl, s->name, "port");
macro_undef(s->vl, s->name, "sock");
@@ -219,56 +219,61 @@ server_listen(struct server *s)
* Server thread
*/
-static void *
-server_thread(void *priv)
+static int
+server_conn(void *priv, struct vtclog *vl)
{
struct server *s;
- struct vtclog *vl;
- int i, j, fd;
struct sockaddr_storage addr_s;
struct sockaddr *addr;
- socklen_t l;
char abuf[VTCP_ADDRBUFSIZE];
char pbuf[VTCP_PORTBUFSIZE];
+ socklen_t l;
+ int fd;
CAST_OBJ_NOTNULL(s, priv, SERVER_MAGIC);
- assert(s->sock >= 0);
- vl = vtc_logopen(s->name);
- pthread_cleanup_push(vtc_logclose, vl);
+ addr = (void*)&addr_s;
+ l = sizeof addr_s;
+ fd = accept(s->sock, addr, &l);
+ if (fd < 0)
+ vtc_fatal(vl, "Accept failed: %s", strerror(errno));
+ if (*s->listen != '/') {
+ VTCP_hisname(fd, abuf, sizeof abuf, pbuf, sizeof pbuf);
+ vtc_log(vl, 3, "accepted fd %d %s %s", fd, abuf, pbuf);
+ } else
+ vtc_log(vl, 3, "accepted fd %d 0.0.0.0 0", fd);
+ return (fd);
+}
- vtc_log(vl, 2, "Started on %s (%u iterations%s)", s->listen,
- s->repeat, s->keepalive ? " using keepalive" : "");
- for (i = 0; i < s->repeat; i++) {
- addr = (void*)&addr_s;
- l = sizeof addr_s;
- fd = accept(s->sock, addr, &l);
- if (fd < 0)
- vtc_fatal(vl, "Accept failed: %s", strerror(errno));
- if (*s->listen != '/') {
- VTCP_hisname(fd, abuf, sizeof abuf, pbuf, sizeof pbuf);
- vtc_log(vl, 3, "accepted fd %d %s %s", fd, abuf, pbuf);
- } else
- vtc_log(vl, 3, "accepted fd %d 0.0.0.0 0", fd);
- if (! s->keepalive)
- fd = http_process(vl, s->spec, fd, &s->sock, s->listen,
- s->rcvbuf);
- else
- while (fd >= 0 && i++ < s->repeat)
- fd = http_process(vl, s->spec, fd,
- &s->sock, s->listen, s->rcvbuf);
- vtc_log(vl, 3, "shutting fd %d", fd);
- j = shutdown(fd, SHUT_WR);
- if (!VTCP_Check(j))
- vtc_fatal(vl, "Shutdown failed: %s", strerror(errno));
- VTCP_close(&fd);
- }
- vtc_log(vl, 2, "Ending");
- pthread_cleanup_pop(0);
- vtc_logclose(vl);
- return (NULL);
+static void
+server_disc(void *priv, struct vtclog *vl, int *fdp)
+{
+ int j;
+ struct server *s;
+
+ CAST_OBJ_NOTNULL(s, priv, SERVER_MAGIC);
+ vtc_log(vl, 3, "shutting fd %d", *fdp);
+ j = shutdown(*fdp, SHUT_WR);
+ if (!VTCP_Check(j))
+ vtc_fatal(vl, "Shutdown failed: %s", strerror(errno));
+ VTCP_close(fdp);
}
+static void
+server_start_thread(struct server *s)
+{
+
+ s->run = 1;
+ s->tp = Sess_Start_Thread(
+ s,
+ s->vsp,
+ server_conn,
+ server_disc,
+ s->listen,
+ &s->sock,
+ s->spec
+ );
+}
/**********************************************************************
* Start the server thread
@@ -281,8 +286,7 @@ server_start(struct server *s)
vtc_log(s->vl, 2, "Starting server");
server_listen(s);
vtc_log(s->vl, 1, "Listen on %s", s->listen);
- s->run = 1;
- AZ(pthread_create(&s->tp, NULL, server_thread, s));
+ server_start_thread(s);
}
/**********************************************************************
@@ -304,7 +308,7 @@ server_dispatch_wrk(void *priv)
fd = s->fd;
vtc_log(vl, 3, "start with fd %d", fd);
- fd = http_process(vl, s->spec, fd, &s->sock, s->listen, s->rcvbuf);
+ fd = sess_process(vl, s->vsp, s->spec, fd, &s->sock, s->listen);
vtc_log(vl, 3, "shutting fd %d", fd);
j = shutdown(fd, SHUT_WR);
if (!VTCP_Check(j))
@@ -530,15 +534,10 @@ cmd_server(CMD_ARGS)
server_wait(s);
AZ(s->run);
- if (!strcmp(*av, "-repeat")) {
- s->repeat = atoi(av[1]);
- av++;
- continue;
- }
- if (!strcmp(*av, "-keepalive")) {
- s->keepalive = 1;
+
+ if (Sess_GetOpt(s->vsp, &av))
continue;
- }
+
if (!strcmp(*av, "-listen")) {
if (s->sock >= 0)
VTCP_close(&s->sock);
@@ -546,11 +545,6 @@ cmd_server(CMD_ARGS)
av++;
continue;
}
- if (!strcmp(*av, "-rcvbuf")) {
- s->rcvbuf = atoi(av[1]);
- av++;
- continue;
- }
if (!strcmp(*av, "-start")) {
server_start(s);
continue;
More information about the varnish-commit
mailing list