Skip to content

Commit cf065f7

Browse files
committed
Add timeout / pool config for apache module
1 parent e26d283 commit cf065f7

File tree

2 files changed

+157
-47
lines changed

2 files changed

+157
-47
lines changed

Diff for: src/mod_redirectionio.c

+137-35
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ static const char *redirectionio_set_project_key(cmd_parms *cmd, void *cfg, cons
3434
static const char *redirectionio_set_logs_enable(cmd_parms *cmd, void *cfg, const char *arg);
3535
static const char *redirectionio_set_scheme(cmd_parms *cmd, void *cfg, const char *arg);
3636
static const char *redirectionio_set_show_rule_ids(cmd_parms *cmd, void *cfg, const char *arg);
37-
static const char *redirectionio_set_pass(cmd_parms *cmd, void *cfg, const char *arg);
37+
static const char *redirectionio_set_server(cmd_parms *cmd, void *dc, int argc, char *const argv[]);
3838
static void redirectionio_apache_log_callback(const char* log_str, const void* data, short level);
39+
static apr_status_t redirectionio_atoi(const char *line, apr_size_t len);
3940

4041
static const command_rec redirectionio_directives[] = {
4142
AP_INIT_TAKE1("redirectionio", redirectionio_set_enable, NULL, OR_ALL, "Enable or disable redirectionio"),
42-
AP_INIT_TAKE1("redirectionioPass", redirectionio_set_pass, NULL, OR_ALL, "Agent server location"),
43+
AP_INIT_TAKE_ARGV("redirectionioPass", redirectionio_set_server, NULL, OR_ALL, "Agent server location"),
4344
AP_INIT_TAKE1("redirectionioProjectKey", redirectionio_set_project_key, NULL, OR_ALL, "RedirectionIO project key"),
4445
AP_INIT_TAKE1("redirectionioLogs", redirectionio_set_logs_enable, NULL, OR_ALL, "Enable or disable logging for redirectionio"),
4546
AP_INIT_TAKE1("redirectionioScheme", redirectionio_set_scheme, NULL, OR_ALL, "Force scheme to use when matching request"),
@@ -87,9 +88,9 @@ static int redirectionio_match_handler(request_rec *r) {
8788
if (config->connection_pool == NULL) {
8889
if (apr_reslist_create(
8990
&config->connection_pool,
90-
RIO_MIN_CONNECTIONS,
91-
RIO_KEEP_CONNECTIONS,
92-
RIO_MAX_CONNECTIONS,
91+
config->server.min_conns,
92+
config->server.keep_conns,
93+
config->server.max_conns,
9394
0,
9495
redirectionio_pool_construct,
9596
redirectionio_pool_destruct,
@@ -103,7 +104,7 @@ static int redirectionio_match_handler(request_rec *r) {
103104
return DECLINED;
104105
}
105106

106-
apr_reslist_timeout_set(config->connection_pool, RIO_TIMEOUT);
107+
apr_reslist_timeout_set(config->connection_pool, config->server.timeout * 1000);
107108
apr_pool_cleanup_register(config->pool, config->connection_pool, redirectionio_child_exit, redirectionio_child_exit);
108109
}
109110

@@ -386,14 +387,14 @@ static apr_status_t redirectionio_create_connection(redirectionio_connection *co
386387
apr_status_t rv;
387388
apr_int32_t family = APR_INET;
388389

389-
if (config->protocol == UNIX) {
390+
if (config->server.protocol == UNIX) {
390391
family = APR_UNIX;
391392
}
392393

393-
rv = apr_sockaddr_info_get(&conn->rio_addr, config->server, family, config->port, 0, pool);
394+
rv = apr_sockaddr_info_get(&conn->rio_addr, config->server.pass, family, config->server.port, 0, pool);
394395

395396
if (rv != APR_SUCCESS) {
396-
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool, "mod_redirectionio: apr_sockaddr_info_get failed %s:%d %s", config->server, config->port, apr_strerror(rv, errbuf, sizeof(errbuf)));
397+
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool, "mod_redirectionio: apr_sockaddr_info_get failed %s:%d %s", config->server.pass, config->server.port, apr_strerror(rv, errbuf, sizeof(errbuf)));
397398

398399
return rv;
399400
}
@@ -439,7 +440,7 @@ static apr_status_t redirectionio_create_connection(redirectionio_connection *co
439440
return rv;
440441
}
441442

442-
rv = apr_socket_timeout_set(conn->rio_sock, RIO_TIMEOUT);
443+
rv = apr_socket_timeout_set(conn->rio_sock, config->server.timeout * 1000);
443444

444445
if (rv != APR_SUCCESS) {
445446
ap_log_perror(APLOG_MARK, APLOG_ERR, 0, pool, "mod_redirectionio: Error setting socket timeout: %s", apr_strerror(rv, errbuf, sizeof(errbuf)));
@@ -495,10 +496,14 @@ static void *create_redirectionio_dir_conf(apr_pool_t *pool, char *context) {
495496
config->enable_logs = -1;
496497
config->project_key = NULL;
497498
config->scheme = NULL;
498-
config->protocol = TCP;
499-
config->port = 10301;
500-
config->server = "127.0.0.1";
501-
config->pass_set = -1;
499+
config->server.protocol = TCP;
500+
config->server.pass = "127.0.0.1";
501+
config->server.pass_set = -1;
502+
config->server.port = 10301;
503+
config->server.min_conns = RIO_MIN_CONNECTIONS;
504+
config->server.max_conns = RIO_MAX_CONNECTIONS;
505+
config->server.keep_conns = RIO_KEEP_CONNECTIONS;
506+
config->server.timeout = RIO_TIMEOUT;
502507
config->pool = pool;
503508
config->show_rule_ids = -1;
504509
}
@@ -542,16 +547,24 @@ static void *merge_redirectionio_dir_conf(apr_pool_t *pool, void *parent, void *
542547
conf->scheme = conf_current->scheme;
543548
}
544549

545-
if (conf_current->pass_set == -1) {
546-
conf->port = conf_parent->port;
547-
conf->protocol = conf_parent->protocol;
548-
conf->server = conf_parent->server;
549-
conf->pass_set = conf_parent->pass_set;
550+
if (conf_current->server.pass_set == -1) {
551+
conf->server.port = conf_parent->server.port;
552+
conf->server.protocol = conf_parent->server.protocol;
553+
conf->server.pass = conf_parent->server.pass;
554+
conf->server.pass_set = conf_parent->server.pass_set;
555+
conf->server.min_conns = conf_parent->server.min_conns;
556+
conf->server.max_conns = conf_parent->server.max_conns;
557+
conf->server.keep_conns = conf_parent->server.keep_conns;
558+
conf->server.timeout = conf_parent->server.timeout;
550559
} else {
551-
conf->port = conf_current->port;
552-
conf->protocol = conf_current->protocol;
553-
conf->server = conf_current->server;
554-
conf->pass_set = conf_current->pass_set;
560+
conf->server.port = conf_current->server.port;
561+
conf->server.protocol = conf_current->server.protocol;
562+
conf->server.pass = conf_current->server.pass;
563+
conf->server.pass_set = conf_current->server.pass_set;
564+
conf->server.min_conns = conf_current->server.min_conns;
565+
conf->server.max_conns = conf_current->server.max_conns;
566+
conf->server.keep_conns = conf_current->server.keep_conns;
567+
conf->server.timeout = conf_current->server.timeout;
555568
}
556569

557570
conf->pool = pool;
@@ -688,44 +701,108 @@ static const char *redirectionio_set_show_rule_ids(cmd_parms *cmd, void *cfg, co
688701
return NULL;
689702
}
690703

691-
static const char *redirectionio_set_pass(cmd_parms *cmd, void *cfg, const char *arg) {
704+
static const char *redirectionio_set_server(cmd_parms *cmd, void *cfg, int argc, char *const argv[]) {
692705
apr_uri_t uri;
693706
redirectionio_config *conf = (redirectionio_config*)cfg;
707+
const char *server_pass;
708+
int i;
709+
size_t arg_len;
694710

695-
if (apr_uri_parse(cmd->pool, arg, &uri) != APR_SUCCESS) {
696-
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "mod_redirectionio: Could not parse agent url %s, disable module.", arg);
711+
if (argc < 1) {
712+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "mod_redirectionio: Could not parse agent server, no parameter, disable module.");
697713
conf->enable = 0;
698714

699715
return NULL;
700716
}
701717

702-
conf->pass_set = 1;
718+
server_pass = argv[0];
719+
720+
if (apr_uri_parse(cmd->pool, server_pass, &uri) != APR_SUCCESS) {
721+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "mod_redirectionio: Could not parse agent url %s, disable module.", server_pass);
722+
conf->enable = 0;
723+
724+
return NULL;
725+
}
726+
727+
conf->server.pass_set = 1;
703728

704729
if (uri.scheme != NULL && apr_strnatcmp(uri.scheme, "unix") == 0) {
705-
conf->protocol = UNIX;
730+
conf->server.protocol = UNIX;
706731
}
707732

708733
if (uri.scheme != NULL && apr_strnatcmp(uri.scheme, "tcp") == 0) {
709-
conf->protocol = TCP;
734+
conf->server.protocol = TCP;
710735
}
711736

712-
if (conf->protocol != UNIX && conf->protocol != TCP) {
737+
if (conf->server.protocol != UNIX && conf->server.protocol != TCP) {
713738
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "mod_redirectionio: Server protocol is %s, but must be 'unix://' or 'tcp://', disable module.", uri.scheme);
714739
conf->enable = 0;
715740
}
716741

717-
if (conf->protocol == UNIX && uri.path) {
718-
conf->server = uri.path;
742+
if (conf->server.protocol == UNIX && uri.path) {
743+
conf->server.pass = uri.path;
719744
}
720745

721-
if (conf->protocol == TCP && uri.hostname) {
722-
conf->server = uri.hostname;
746+
if (conf->server.protocol == TCP && uri.hostname) {
747+
conf->server.pass = uri.hostname;
723748
}
724749

725750
if (uri.port) {
726-
conf->port = uri.port;
751+
conf->server.port = uri.port;
752+
}
753+
754+
for (i = 1; i < argc; i++) {
755+
arg_len = strlen(argv[i]);
756+
757+
if (strncasecmp(argv[i], "min_conns=", 10) == 0) {
758+
conf->server.min_conns = redirectionio_atoi(&argv[i][10], arg_len - 10);
759+
760+
if (conf->server.min_conns == APR_EGENERAL) {
761+
goto invalid;
762+
}
763+
764+
continue;
765+
}
766+
767+
if (strncasecmp(argv[i], "max_conns=", 10) == 0) {
768+
conf->server.max_conns = redirectionio_atoi(&argv[i][10], arg_len - 10);
769+
770+
if (conf->server.max_conns == APR_EGENERAL) {
771+
goto invalid;
772+
}
773+
774+
continue;
775+
}
776+
777+
if (strncasecmp(argv[i], "keep_conns=", 11) == 0) {
778+
conf->server.keep_conns = redirectionio_atoi(&argv[i][11], arg_len - 11);
779+
780+
if (conf->server.keep_conns == APR_EGENERAL) {
781+
goto invalid;
782+
}
783+
784+
continue;
785+
}
786+
787+
if (strncasecmp(argv[i], "timeout=", 8) == 0) {
788+
conf->server.timeout = (apr_interval_time_t) redirectionio_atoi(&argv[i][8], arg_len - 8);
789+
790+
if (conf->server.timeout == (apr_interval_time_t) APR_EGENERAL) {
791+
goto invalid;
792+
}
793+
794+
continue;
795+
}
796+
797+
goto invalid;
727798
}
728799

800+
return NULL;
801+
802+
invalid:
803+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "mod_redirectionio: invalid parameter when setting pass: %s.", argv[i]);
804+
conf->enable = 0;
805+
729806
return NULL;
730807
}
731808

@@ -734,3 +811,28 @@ static void redirectionio_apache_log_callback(const char* log_str, const void* d
734811
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, (request_rec *)data, "mod_redirectionio api error: %s", log_str);
735812
}
736813
}
814+
815+
static apr_status_t redirectionio_atoi(const char *line, apr_size_t len) {
816+
int value, cutoff, cutlim;
817+
818+
if (len == 0) {
819+
return APR_EGENERAL;
820+
}
821+
822+
cutoff = INT_MAX / 10;
823+
cutlim = INT_MAX % 10;
824+
825+
for (value = 0; len--; line++) {
826+
if (*line < '0' || *line > '9') {
827+
return APR_EGENERAL;
828+
}
829+
830+
if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
831+
return APR_EGENERAL;
832+
}
833+
834+
value = value * 10 + (*line - '0');
835+
}
836+
837+
return value;
838+
}

Diff for: src/mod_redirectionio.h

+20-12
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,33 @@
3434
#define STRINGIZE(x) #x
3535
#define PROXY_VERSION_STR(x) STRINGIZE(x)
3636

37-
#define RIO_TIMEOUT 100000 // Timeout in microseconds
37+
#define RIO_TIMEOUT 100 // Timeout in microseconds
3838
#define RIO_RECONNECT_INTERVAL 0
3939
#define RIO_MIN_CONNECTIONS 1
4040
#define RIO_KEEP_CONNECTIONS 10
4141
#define RIO_MAX_CONNECTIONS 100
4242
#define RIO_SEND_BUFFER 1048576
4343

4444
typedef struct {
45-
const char* project_key;
46-
const char* scheme;
47-
char* server;
48-
int port;
49-
int protocol;
50-
int enable;
51-
int enable_logs;
52-
int pass_set;
53-
int show_rule_ids;
54-
apr_reslist_t *connection_pool;
55-
apr_pool_t *pool;
45+
char* pass;
46+
int port;
47+
int protocol;
48+
int min_conns;
49+
int max_conns;
50+
int keep_conns;
51+
apr_interval_time_t timeout;
52+
int pass_set;
53+
} redirectionio_server;
54+
55+
typedef struct {
56+
const char* project_key;
57+
const char* scheme;
58+
redirectionio_server server;
59+
int enable;
60+
int enable_logs;
61+
int show_rule_ids;
62+
apr_reslist_t *connection_pool;
63+
apr_pool_t *pool;
5664
} redirectionio_config;
5765

5866
typedef struct {

0 commit comments

Comments
 (0)