/
cproxy_config.c
98 lines (82 loc) · 2.74 KB
/
cproxy_config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sysexits.h>
#include <pthread.h>
#include <assert.h>
#include <libmemcached/memcached.h>
#include "memcached.h"
#include "cproxy.h"
#include "work.h"
int cproxy_init_string(const char *cfg, int nthreads,
int downstream_max);
int cproxy_init_agent(const char *cfg, int nthreads,
int downstream_max);
int cproxy_init(const char *cfg, int nthreads,
int downstream_max) {
assert(nthreads > 1); // Main + at least one worker.
assert(nthreads == settings.num_threads);
assert(downstream_max > 0);
if (cfg == NULL ||
strlen(cfg) <= 0)
return 0;
if (settings.verbose > 1)
fprintf(stderr, "cproxy_init %s\n", cfg);
if (strchr(cfg, '@') == NULL) // Not jid format.
return cproxy_init_string(cfg, nthreads, downstream_max);
#ifdef BUILD_MEMAGENT
return cproxy_init_agent(cfg, nthreads, downstream_max);
#else
return 1;
#endif
}
int cproxy_init_string(const char *cfg, int nthreads,
int downstream_max) {
/* cfg should look like "local_port=host:port,host:port;local_port=host:port"
* like "11222=memcached1.foo.net:11211" This means local port 11222
* will be a proxy to downstream memcached server running at
* host memcached1.foo.net on port 11211.
*/
assert(nthreads > 1); // Main + at least one worker.
assert(nthreads == settings.num_threads);
assert(downstream_max > 0);
if (cfg == NULL ||
strlen(cfg) <= 0)
return 0;
char *buff;
char *next;
char *proxy_name = "default";
char *proxy_sect;
char *proxy_port_str;
int proxy_port;
buff = strdup(cfg);
next = buff;
while (next != NULL) {
proxy_sect = strsep(&next, ";");
proxy_port_str = strsep(&proxy_sect, "=");
if (proxy_sect == NULL) {
fprintf(stderr, "bad cproxy config, missing =\n");
exit(EXIT_FAILURE);
}
proxy_port = atoi(proxy_port_str);
if (proxy_port <= 0) {
fprintf(stderr, "bad cproxy config, bad proxy port\n");
exit(EXIT_FAILURE);
}
proxy *p = cproxy_create(proxy_name, proxy_port, proxy_sect, 0,
nthreads, downstream_max);
if (p != NULL) {
int n = cproxy_listen(p);
if (n > 0) {
if (settings.verbose > 1)
fprintf(stderr, "cproxy listening on %d conns\n", n);
}
} else {
fprintf(stderr, "could not alloc proxy\n");
exit(EXIT_FAILURE);
}
}
free(buff);
return 0;
}