3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 static int ngx_http_busy_lock_look_cacheable(ngx_http_busy_lock_t *bl,
14 ngx_http_busy_lock_ctx_t *bc,
18 int ngx_http_busy_lock(ngx_http_busy_lock_t *bl, ngx_http_busy_lock_ctx_t *bc)
20 if (bl->busy < bl->max_busy) {
32 if (bc->time < bl->timeout) {
33 ngx_add_timer(bc->event, 1000);
42 if (bl->timeout == 0) {
46 if (bl->waiting < bl->max_waiting) {
50 ngx_add_timer(bc->event, 1000);
51 bc->event->event_handler = bc->event_handler;
54 /* TODO: ngx_handle_level_read_event() */
63 int ngx_http_busy_lock_cacheable(ngx_http_busy_lock_t *bl,
64 ngx_http_busy_lock_ctx_t *bc, int lock)
68 rc = ngx_http_busy_lock_look_cacheable(bl, bc, lock);
70 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, bc->event->log, 0,
71 "http busylock: %d w:%d mw::%d",
72 rc, bl->waiting, bl->max_waiting);
74 if (rc == NGX_OK) { /* no the same request, there's free slot */
78 if (rc == NGX_ERROR && !lock) { /* no the same request, no free slot */
82 /* rc == NGX_AGAIN: the same request */
85 if (bc->time < bl->timeout) {
86 ngx_add_timer(bc->event, 1000);
95 if (bl->timeout == 0) {
99 if (bl->waiting < bl->max_waiting) {
102 ngx_add_timer(bc->event, 1000);
103 bc->event->event_handler = bc->event_handler;
106 /* TODO: ngx_handle_level_read_event() */
115 void ngx_http_busy_unlock(ngx_http_busy_lock_t *bl,
116 ngx_http_busy_lock_ctx_t *bc)
123 bl->md5_mask[bc->slot / 8] &= ~(1 << (bc->slot & 7));
131 static int ngx_http_busy_lock_look_cacheable(ngx_http_busy_lock_t *bl,
132 ngx_http_busy_lock_ctx_t *bc,
135 int i, b, cacheable, free;
142 #if (NGX_SUPPRESS_WARN)
146 for (i = 0; i < bl->max_busy; i++) {
149 mask = bl->md5_mask[i / 8];
153 if (ngx_memcmp(&bl->md5[i * 16], bc->md5, 16) == 0) {
158 } else if (free == -1) {
163 if (cacheable == bl->cacheable) {
164 if (free == -1 && cacheable < bl->max_busy) {
181 if (bl->busy == bl->max_busy) {
185 ngx_memcpy(&bl->md5[free * 16], bc->md5, 16);
186 bl->md5_mask[free / 8] |= 1 << (free & 7);
197 char *ngx_http_set_busy_lock_slot(ngx_conf_t *cf, ngx_command_t *cmd,
202 ngx_uint_t i, dup, invalid;
203 ngx_str_t *value, line;
204 ngx_http_busy_lock_t *bl, **blp;
206 blp = (ngx_http_busy_lock_t **) (p + cmd->offset);
208 return "is duplicate";
211 /* ngx_calloc_shared() */
212 bl = ngx_pcalloc(cf->pool, sizeof(ngx_http_busy_lock_t));
214 return NGX_CONF_ERROR;
218 /* ngx_calloc_shared() */
219 bl->mutex = ngx_pcalloc(cf->pool, sizeof(ngx_event_mutex_t));
220 if (bl->mutex == NULL) {
221 return NGX_CONF_ERROR;
226 value = cf->args->elts;
228 for (i = 1; i < cf->args->nelts; i++) {
230 if (value[i].data[1] != '=') {
231 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
232 "invalid value \"%s\"", value[i].data);
233 return NGX_CONF_ERROR;
236 switch (value[i].data[0]) {
244 bl->max_busy = ngx_atoi(value[i].data + 2, value[i].len - 2);
245 if (bl->max_busy == NGX_ERROR) {
253 if (bl->max_waiting) {
258 bl->max_waiting = ngx_atoi(value[i].data + 2, value[i].len - 2);
259 if (bl->max_waiting == NGX_ERROR) {
272 line.len = value[i].len - 2;
273 line.data = value[i].data + 2;
275 bl->timeout = ngx_parse_time(&line, 1);
276 if (bl->timeout == NGX_ERROR) {
288 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
289 "duplicate value \"%s\"", value[i].data);
290 return NGX_CONF_ERROR;
294 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
295 "invalid value \"%s\"", value[i].data);
296 return NGX_CONF_ERROR;
300 if (bl->timeout == 0 && bl->max_waiting) {
301 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
302 "busy lock waiting is useless with zero timeout, ignoring");