diff options
author | Stefan Fritsch <sf@apache.org> | 2011-09-15 21:53:59 +0200 |
---|---|---|
committer | Stefan Fritsch <sf@apache.org> | 2011-09-15 21:53:59 +0200 |
commit | 2d4e23d88b52302bada92e581bd48e9f993e2df6 (patch) | |
tree | d3d5ef0d76c876f3ebc69dae5a27500693395394 /modules | |
parent | - start definitive list of modules not to be included in 2.4 (diff) | |
download | apache2-2d4e23d88b52302bada92e581bd48e9f993e2df6.tar.xz apache2-2d4e23d88b52302bada92e581bd48e9f993e2df6.zip |
Create wrapper API for apr_random;
use in mod_lbmethod_heartbeat and mod_serf to
- replace some needles use of apr_generate_random_bytes
- remove code duplication
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1171247 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'modules')
-rw-r--r-- | modules/http/http_core.c | 6 | ||||
-rw-r--r-- | modules/proxy/balancers/mod_lbmethod_heartbeat.c | 39 | ||||
-rw-r--r-- | modules/proxy/mod_serf.c | 32 |
3 files changed, 8 insertions, 69 deletions
diff --git a/modules/http/http_core.c b/modules/http/http_core.c index fe26de70cd..8db3ad7a83 100644 --- a/modules/http/http_core.c +++ b/modules/http/http_core.c @@ -41,6 +41,8 @@ AP_DECLARE_DATA ap_filter_rec_t *ap_chunk_filter_handle; AP_DECLARE_DATA ap_filter_rec_t *ap_http_outerror_filter_handle; AP_DECLARE_DATA ap_filter_rec_t *ap_byterange_filter_handle; +AP_DECLARE_DATA const char *ap_multipart_boundary; + /* If we are using an MPM That Supports Async Connections, * use a different processing function */ @@ -255,9 +257,13 @@ static int http_send_options(request_rec *r) static int http_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) { + apr_uint64_t val; if (ap_mpm_query(AP_MPMQ_IS_ASYNC, &async_mpm) != APR_SUCCESS) { async_mpm = 0; } + ap_random_insecure_bytes(&val, sizeof(val)); + ap_multipart_boundary = apr_psprintf(p, "%0" APR_UINT64_T_HEX_FMT, val); + return OK; } diff --git a/modules/proxy/balancers/mod_lbmethod_heartbeat.c b/modules/proxy/balancers/mod_lbmethod_heartbeat.c index e8dbbc122a..65f7575e53 100644 --- a/modules/proxy/balancers/mod_lbmethod_heartbeat.c +++ b/modules/proxy/balancers/mod_lbmethod_heartbeat.c @@ -253,36 +253,6 @@ static apr_status_t read_heartbeats(const char *path, apr_hash_t *servers, return rv; } -/* - * Finding a random number in a range. - * n' = a + n(b-a+1)/(M+1) - * where: - * n' = random number in range - * a = low end of range - * b = high end of range - * n = random number of 0..M - * M = maxint - * Algorithm 'borrowed' from PHP's rand() function. - */ -#define RAND_RANGE(__n, __min, __max, __tmax) \ -(__n) = (__min) + (long) ((double) ((__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0))) - -static apr_status_t random_pick(apr_uint32_t *number, - apr_uint32_t min, - apr_uint32_t max) -{ - apr_status_t rv = - apr_generate_random_bytes((void*)number, sizeof(apr_uint32_t)); - - if (rv) { - return rv; - } - - RAND_RANGE(*number, min, max, APR_UINT32_MAX); - - return APR_SUCCESS; -} - static proxy_worker *find_best_hb(proxy_balancer *balancer, request_rec *r) { @@ -343,14 +313,7 @@ static proxy_worker *find_best_hb(proxy_balancer *balancer, apr_uint32_t c = 0; apr_uint32_t pick = 0; - rv = random_pick(&pick, 0, openslots); - - if (rv) { - ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, - "lb_heartbeat: failed picking a random number. how random."); - apr_pool_destroy(tpool); - return NULL; - } + pick = ap_random_pick(0, openslots); for (i = 0; i < up_servers->nelts; i++) { server = APR_ARRAY_IDX(up_servers, i, hb_server_t *); diff --git a/modules/proxy/mod_serf.c b/modules/proxy/mod_serf.c index cc00eceed5..408179b238 100644 --- a/modules/proxy/mod_serf.c +++ b/modules/proxy/mod_serf.c @@ -399,35 +399,6 @@ static apr_status_t setup_request(serf_request_t *request, return APR_SUCCESS; } -/* - * Finding a random number in a range. - * n' = a + n(b-a+1)/(M+1) - * where: - * n' = random number in range - * a = low end of range - * b = high end of range - * n = random number of 0..M - * M = maxint - * Algorithm 'borrowed' from PHP's rand() function. (See mod_lbmethod_heartbeat.c). - */ -#define RAND_RANGE(__n, __min, __max, __tmax) \ -(__n) = (__min) + (long) ((double) ((__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0))) - -static apr_status_t random_pick(apr_uint32_t *number, - apr_uint32_t min, - apr_uint32_t max) -{ - apr_status_t rv = - apr_generate_random_bytes((void*)number, sizeof(apr_uint32_t)); - - if (rv) { - return rv; - } - - RAND_RANGE(*number, min, max, APR_UINT32_MAX); - - return APR_SUCCESS; -} /* TOOD: rewrite drive_serf to make it async */ static int drive_serf(request_rec *r, serf_config_t *conf) { @@ -499,8 +470,7 @@ static int drive_serf(request_rec *r, serf_config_t *conf) } /* TOOD: restructure try all servers in the array !! */ - if (random_pick(&pick, 0, servers->nelts-1) != APR_SUCCESS) - pick = 0; + pick = ap_random_pick(0, servers->nelts-1); choice = APR_ARRAY_IDX(servers, pick, ap_serf_server_t *); rv = apr_sockaddr_info_get(&address, choice->ip, |