static ngx_int_t
ngx_extra_var_time_msec(ngx_http_request_t *r,
    ngx_http_variable_value_t *v, uintptr_t data)
{
    u_char         *p;
    ngx_time_t     *tp, t;
    ngx_msec_int_t  ms;

    p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 4);
    if (p == NULL) {
        return NGX_ERROR;
    }

    switch (data) {
    case NGX_EXTRAVAR_TIME_NOW :
        tp = ngx_timeofday();
        break;

    case NGX_EXTRAVAR_TIME_ELAPSED :
        tp = ngx_timeofday();

        ms = (ngx_msec_int_t) ((tp->sec - r->start_sec) * 1000
                 + (tp->msec - r->start_msec));
        ms = ngx_max(ms, 0);

        tp = &t;
        tp->sec = ms / 1000;
        tp->msec = ms % 1000;
        break;

    case NGX_EXTRAVAR_TIME_REQUEST :
        tp = &t;
        tp->sec = r->start_sec;
        tp->msec = r->start_msec;
        break;

    default :
        v->not_found = 1;
        return NGX_OK;
    }

    v->len = ngx_sprintf(p, "%T.%03M", tp->sec, tp->msec) - p;
    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;
    v->data = p;

    return NGX_OK;
}
static ngx_int_t
ngx_http_status_log_handler(ngx_http_request_t *r)
{
    ngx_time_t                *tp;
    ngx_msec_int_t             ms;
    struct timeval             tv;
    ngx_http_core_loc_conf_t  *clcf;

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
    if (clcf->request_time_cache) {
        tp = ngx_timeofday();

        ms = (ngx_msec_int_t)
                 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
    } else {
        ngx_gettimeofday(&tv);

        ms = (ngx_msec_int_t) ((tv.tv_sec - r->start_sec) * 1000
                 + (tv.tv_usec / 1000 - r->start_msec));
    }

    ms = ngx_max(ms, 0);
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                   "http status: request_time %d", ms);

    (void) ngx_atomic_fetch_add(ngx_stat_request_time, ms);

    return NGX_OK;
}
static ngx_int_t
ngx_stream_variable_session_time(ngx_stream_session_t *s,
    ngx_stream_variable_value_t *v, uintptr_t data)
{
    u_char          *p;
    ngx_time_t      *tp;
    ngx_msec_int_t   ms;

    p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4);
    if (p == NULL) {
        return NGX_ERROR;
    }

    tp = ngx_timeofday();

    ms = (ngx_msec_int_t)
             ((tp->sec - s->start_sec) * 1000 + (tp->msec - s->start_msec));
    ms = ngx_max(ms, 0);

    v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;
    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;
    v->data = p;

    return NGX_OK;
}
Beispiel #4
0
static u_char *
ngx_http_log_request_time_usec(ngx_http_request_t *r, u_char *buf,
    ngx_http_log_op_t *op)
{
    ngx_time_t                *tp;
    ngx_usec_int_t             us;
    struct timeval             tv;
    ngx_http_core_loc_conf_t  *clcf;

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
    if (clcf->request_time_cache) {
        tp = ngx_timeofday();

        us = (ngx_usec_int_t) (1000 *
                 ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec)))
                 + tp->usec - r->start_usec;
    } else {
        ngx_gettimeofday(&tv);
        us = (ngx_usec_int_t) (1000 * ((tv.tv_sec - r->start_sec) * 1000
                 + (tv.tv_usec / 1000 - r->start_msec)))
                 + tv.tv_usec % 1000 - r->start_usec;
    }

    us = ngx_max(us, 0);

    return ngx_sprintf(buf, "%T", us);
}
static void
ngx_http_limit_req2_expire(ngx_http_request_t *r, ngx_http_limit_req2_ctx_t *ctx,
    ngx_uint_t n)
{
    ngx_int_t                   excess;
    ngx_time_t                 *tp;
    ngx_msec_t                  now;
    ngx_queue_t                *q;
    ngx_msec_int_t              ms;
    ngx_rbtree_node_t          *node;
    ngx_http_limit_req2_node_t *lr;

    tp = ngx_timeofday();

    now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);

    /*
     * n == 1 deletes one or two zero rate entries
     * n == 0 deletes oldest entry by force
     *        and one or two zero rate entries
     */

    while (n < 3) {

        if (ngx_queue_empty(&ctx->sh->queue)) {
            return;
        }

        q = ngx_queue_last(&ctx->sh->queue);

        lr = ngx_queue_data(q, ngx_http_limit_req2_node_t, queue);

        if (n++ != 0) {

            ms = (ngx_msec_int_t) (now - lr->last);
            ms = ngx_abs(ms);

            if (ms < 60000) {
                return;
            }

            excess = lr->excess - ctx->rate * ms / 1000;

            if (excess > 0) {
                return;
            }
        }

        ngx_queue_remove(q);

        node = (ngx_rbtree_node_t *)
                   ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));

        ngx_rbtree_delete(&ctx->sh->rbtree, node);

        ngx_slab_free_locked(ctx->shpool, node);
    }
}
Beispiel #6
0
static ngx_thread_value_t __stdcall
ngx_worker_thread(void *data)
{
    ngx_int_t     n;
    ngx_time_t   *tp;
    ngx_cycle_t  *cycle;

    tp = ngx_timeofday();
    srand((ngx_pid << 16) ^ (unsigned) tp->sec ^ tp->msec);

    cycle = (ngx_cycle_t *) ngx_cycle;

    for (n = 0; cycle->modules[n]; n++) {
        if (cycle->modules[n]->init_process) {
            if (cycle->modules[n]->init_process(cycle) == NGX_ERROR) {
                /* fatal */
                exit(2);
            }
        }
    }

    while (!ngx_quit) {

        if (ngx_exiting) {
            if (ngx_event_no_timers_left() == NGX_OK) {
                break;
            }
        }

        ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "worker cycle");

        ngx_process_events_and_timers(cycle);

        if (ngx_terminate) {
            return 0;
        }

        if (ngx_quit) {
            ngx_quit = 0;

            if (!ngx_exiting) {
                ngx_exiting = 1;
                ngx_set_shutdown_timer(cycle);
                ngx_close_listening_sockets(cycle);
                ngx_close_idle_connections(cycle);
            }
        }

        if (ngx_reopen) {
            ngx_reopen = 0;
            ngx_reopen_files(cycle, -1);
        }
    }

    ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");

    return 0;
}
Beispiel #7
0
static u_char *
ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
{
    ngx_time_t  *tp;

    tp = ngx_timeofday();

    return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
}
static int
ngx_tcp_lua_shdict_expire(ngx_tcp_lua_shdict_ctx_t *ctx, ngx_uint_t n)
{
    ngx_time_t                  *tp;
    uint64_t                     now;
    ngx_queue_t                 *q;
    int64_t                      ms;
    ngx_rbtree_node_t           *node;
    ngx_tcp_lua_shdict_node_t  *sd;
    int                          freed = 0;

    tp = ngx_timeofday();

    now = (uint64_t) tp->sec * 1000 + tp->msec;

    /*
     * n == 1 deletes one or two expired entries
     * n == 0 deletes oldest entry by force
     *        and one or two zero rate entries
     */

    while (n < 3) {

        if (ngx_queue_empty(&ctx->sh->queue)) {
            return freed;
        }

        q = ngx_queue_last(&ctx->sh->queue);

        sd = ngx_queue_data(q, ngx_tcp_lua_shdict_node_t, queue);

        if (n++ != 0) {

            if (sd->expires == 0) {
                return freed;
            }

            ms = sd->expires - now;
            if (ms > 0) {
                return freed;
            }
        }

        ngx_queue_remove(q);

        node = (ngx_rbtree_node_t *)
                   ((u_char *) sd - offsetof(ngx_rbtree_node_t, color));

        ngx_rbtree_delete(&ctx->sh->rbtree, node);

        ngx_slab_free_locked(ctx->shpool, node);

        freed++;
    }

    return freed;
}
Beispiel #9
0
static void
ngx_http_ip_behavior_expire(ngx_http_ip_behavior_ctx_t *ctx, ngx_uint_t force)
{
    ngx_time_t                   *tp;
    ngx_msec_t                    now;
    ngx_msec_int_t                ms;
    ngx_queue_t                  *q;
    ngx_rbtree_node_t            *node;
    ngx_http_ip_behavior_node_t  *ibn;
    ngx_uint_t                    i;
    ngx_uint_t                    n;


    tp = ngx_timeofday();

    now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);

    /* delete at most 2 oldest nodes when a new request comes in
     * or
     * delete 10 oldest nodes ignoring the expires when "force" is set to 1
     */
    if (force) {
        n = 10;
    } else {
        n = 2;
    }

    for (i = 0; i < n; i++) {
        if (ngx_queue_empty(&ctx->sh->queue)) {
            return;
        }

        q = ngx_queue_last(&ctx->sh->queue);

        ibn = ngx_queue_data(q, ngx_http_ip_behavior_node_t, queue);

        if (!force) {
            ms = (ngx_msec_int_t) (now - ibn->last);
            ms = ngx_abs(ms);

            if (ms < ctx->sample_cycle) {
                /* the oldest is not expired, no need to check prev nodes */
                return;
            }
        }

        ngx_queue_remove(q);

        node = (ngx_rbtree_node_t *)
                   ((u_char *) ibn - offsetof(ngx_rbtree_node_t, color));

        ngx_rbtree_delete(&ctx->sh->rbtree, node);

        ngx_slab_free_locked(ctx->shpool, node);
    }
}
Beispiel #10
0
static u_char *
ngx_http_log_msec(ngx_http_request_t *r, u_char *buf, ngx_http_log_op_t *op)
{
		syslog(LOG_INFO, "[%s:%s:%d]", __FILE__, __func__, __LINE__);
    ngx_time_t  *tp;

    tp = ngx_timeofday();

    return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
}
static u_char *
ngx_rtmp_log_var_msec_getdata(ngx_rtmp_session_t *s, u_char *buf,
    ngx_rtmp_log_op_t *op)
{
    ngx_time_t  *tp;

    tp = ngx_timeofday();
    
    return ngx_sprintf(buf, "%T.%03M", tp->sec, tp->msec);
}
static void ngx_proc_luashm_backup_thread_cycle(void *data)
{
	ngx_proc_luashm_backup_conf_t  	*pbcf;
    ngx_cycle_t 					*cycle;
    ngx_shm_zone_t   				*zone;
	ngx_str_t 						*name;
	ngx_uint_t 						i;
	ngx_time_t                  	*tp;
    int64_t                     	now,settimestamp;

	cycle = data;
	pbcf = ngx_proc_get_conf(cycle->conf_ctx, ngx_proc_luashm_backup_module);
	if(pbcf->settimestamp.data == NULL){
		pthread_exit((void *)0);
		return;
	}

	while(pbcf->running){
		ngx_time_update();
		if(ngx_strstr(pbcf->settimestamp.data,":")==NULL){
			settimestamp=ngx_atoi(pbcf->settimestamp.data,pbcf->settimestamp.len);
		}else{
			tp = ngx_timeofday();
	    	now = (int64_t) tp->sec * 1000 + tp->msec;
			settimestamp=GetTick((char*)pbcf->settimestamp.data)*1000;
			ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0, "========= now:%lu, settimestamp:%lu =========",now,settimestamp);
			if(settimestamp<now)
				settimestamp=(24*60*60*1000)-(now-settimestamp);
			else
				settimestamp=settimestamp-now;

			if(settimestamp==0)settimestamp=1;
		}
		ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0, "========= backup time:%s, sleeptime:%lu =========",pbcf->settimestamp.data,settimestamp);

		ngx_msleep(settimestamp);
		if(!pbcf->running)break;

		i = 0;
		for (; i < pbcf->shdict_names->nelts;i++)
		{
			name = ((ngx_str_t*)pbcf->shdict_names->elts)  + i;
			zone = ngx_http_lua_find_zone(name->data,name->len);
			dd("shm.name:%s,shm.name_len:%ld,shm.size:%ld  ", name->data,name->len,zone->shm.size);
			ngx_proc_luashm_backup_backup(cycle,zone);
		}
		ngx_log_error(NGX_LOG_DEBUG, cycle->log, 0, "luashm_backup %V",&ngx_cached_http_time);
		ngx_sleep(60);
		if(!pbcf->running)break;

	}

	pthread_exit((void *)0);
}
static int
ngx_tcp_lua_ngx_now(lua_State *L)
{
    ngx_time_t              *tp;

    tp = ngx_timeofday();

    lua_pushnumber(L, (lua_Number) (tp->sec + tp->msec / 1000.0L));

    return 1;
}
Beispiel #14
0
//-----------------------------------------------------------------------------
static void
ngx_c2h5oh_event_handler(ngx_event_t * ev)
{
  ngx_time_t * tp;
  ngx_http_request_t * r; 
  ngx_c2h5oh_ctx_t*  ctx; 

  r   = ev->data;
  ctx = ngx_http_get_module_ctx(r, ngx_c2h5oh_module);

  ctx->timer.timedout = 0;
  if (ctx->timer.timer_set) {
    ngx_del_timer(&ctx->timer);
  }

  tp = ngx_timeofday();
  if (tp->sec > ctx->timeout.sec || (tp->sec == ctx->timeout.sec &&
      tp->msec >= ctx->timeout.msec)) 
  {
    if (ctx->conn != NULL) {
      if (c2h5oh_is_error(ctx->conn)) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "[c2h5oh] error after timeout %s", c2h5oh_result(ctx->conn));
      }
    } else {
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                    "[c2h5oh] no free connections available");
    }
    return ngx_http_finalize_request(r, NGX_HTTP_GATEWAY_TIME_OUT);
  }

  if (ctx->conn == NULL) {

    ctx->conn = c2h5oh_create();
    if (ctx->conn == NULL) {
      ngx_add_timer(&ctx->timer, (ngx_msec_t)1);
      return;
    }

    if (c2h5oh_query(ctx->conn, (const char *)ctx->query.data) != 0) {
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                    "[c2h5oh] error create query");
      return ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
    }
  }

  if (c2h5oh_poll(ctx->conn) == 0) {
    ngx_add_timer(&ctx->timer, (ngx_msec_t)1);
  } else {
    ngx_c2h5oh_post_response(r, ctx);
  }
}
Beispiel #15
0
int ngx_shmap_flush_expired(ngx_shm_zone_t* zone, int attempts)
{
    ngx_queue_t                 *q, *prev;
    ngx_shmap_node_t  *sd;
    ngx_shmap_ctx_t   *ctx;
    ngx_time_t                  *tp;
    int                          freed = 0;
    ngx_rbtree_node_t           *node;
    uint64_t                     now;
	assert(zone != NULL);

    ctx = zone->data;

    ngx_shmtx_lock(&ctx->shpool->mutex);

    if (ngx_queue_empty(&ctx->sh->queue)) {
        return 0;
    }

    tp = ngx_timeofday();

    now = (uint64_t) tp->sec * 1000 + tp->msec;

    q = ngx_queue_last(&ctx->sh->queue);

    while (q != ngx_queue_sentinel(&ctx->sh->queue)) {
        prev = ngx_queue_prev(q);

        sd = ngx_queue_data(q, ngx_shmap_node_t, queue);

        if (sd->expires != 0 && sd->expires <= now) {
            ngx_queue_remove(q);

            node = (ngx_rbtree_node_t *)
                ((u_char *) sd - offsetof(ngx_rbtree_node_t, color));

            ngx_rbtree_delete(&ctx->sh->rbtree, node);
            ngx_slab_free_locked(ctx->shpool, node);
            freed++;

            if (attempts && freed == attempts) {
                break;
            }
        }

        q = prev;
    }

    ngx_shmtx_unlock(&ctx->shpool->mutex);

    return freed;
}
Beispiel #16
0
static ngx_int_t
ngx_http_session_gen_sid(ngx_http_request_t *r, ngx_str_t *sid)
{
    ngx_md5_t                        md5_state;
    u_char                           md5_digest[16];
    u_char                           hex_output[16 * 2 + 1], source[512];
    ngx_uint_t                       di, source_len = 0, port_time_len;
    struct sockaddr_in               *peer_addr;
    socklen_t                        peer_len = NGX_SOCKADDRLEN;
    u_char                           sa[NGX_SOCKADDRLEN];
    ngx_int_t                        ret = 0;
    ngx_time_t                       *time;

    memset(source, 0, 512);

    memcpy(source, r->connection->addr_text.data, r->connection->addr_text.len);
    source_len += r->connection->addr_text.len;
    
    ret = getpeername(r->connection->fd, (struct sockaddr *)&sa, &peer_len);
    if (ret < 0) {
        return NGX_ERROR;
    }

    time = ngx_timeofday();
    if (!time) {
        return NGX_ERROR;
    }

    peer_addr = (struct sockaddr_in *)sa;
    port_time_len = sprintf((char *)source + source_len, ":%d %d.%d", 
            peer_addr->sin_port, (int)ngx_time(), (int)time->msec);

    if (port_time_len <= 0) {
        return NGX_ERROR;
    }

    source_len += port_time_len;

    ngx_md5_init(&md5_state);
    ngx_md5_update(&md5_state, (void *)source, source_len);
    ngx_md5_final(md5_digest, &md5_state);

    for (di = 0; di < 16; di++) {
        sprintf((char *)hex_output + di * 2, "%02x", md5_digest[di]);
    }

    memcpy(sid->data, hex_output, NGX_HTTP_SESSION_DEFAULT_SID_LEN);
    sid->data[NGX_HTTP_SESSION_DEFAULT_SID_LEN] = 0;
    sid->len = NGX_HTTP_SESSION_DEFAULT_SID_LEN;

    return NGX_OK;
}
Beispiel #17
0
ngx_int_t
ngx_os_init(ngx_log_t *log)
{
    ngx_time_t  *tp;
    ngx_uint_t   n;

#if (NGX_HAVE_OS_SPECIFIC_INIT)
    if (ngx_os_specific_init(log) != NGX_OK) {
        return NGX_ERROR;
    }
#endif

    if (ngx_init_setproctitle(log) != NGX_OK) {
        return NGX_ERROR;
    }

    ngx_pagesize = getpagesize();
    ngx_cacheline_size = NGX_CPU_CACHE_LINE;

    for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }

#if (NGX_HAVE_SC_NPROCESSORS_ONLN)
    if (ngx_ncpu == 0) {
        ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN);
    }
#endif

    if (ngx_ncpu < 1) {
        ngx_ncpu = 1;
    }

    ngx_cpuinfo();

    if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
        ngx_log_error(NGX_LOG_ALERT, log, errno,
                      "getrlimit(RLIMIT_NOFILE) failed");
        return NGX_ERROR;
    }

    ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur;

#if (NGX_HAVE_INHERITED_NONBLOCK || NGX_HAVE_ACCEPT4)
    ngx_inherited_nonblocking = 1;
#else
    ngx_inherited_nonblocking = 0;
#endif

    tp = ngx_timeofday();
    srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);

    return NGX_OK;
}
Beispiel #18
0
ngx_int_t
ngx_http_echo_timer_elapsed_variable(ngx_http_request_t *r,
        ngx_http_variable_value_t *v, uintptr_t data)
{
    ngx_http_echo_ctx_t     *ctx;
    ngx_msec_int_t          ms;
    u_char                  *p;
    ngx_time_t              *tp;
    size_t                  size;

    ctx = ngx_http_get_module_ctx(r, ngx_http_echo_module);
    if (ctx->timer_begin.sec == 0) {
        ctx->timer_begin.sec  = r->start_sec;
        ctx->timer_begin.msec = (ngx_msec_t) r->start_msec;
    }

    /* force the ngx timer to update */

#if (nginx_version >= 8035) || (nginx_version < 8000 && nginx_version >= 7066)
    ngx_time_update();
#else
    ngx_time_update(0, 0);
#endif

    tp = ngx_timeofday();

    dd("old sec msec: %ld %d\n", ctx->timer_begin.sec, ctx->timer_begin.msec);
    dd("new sec msec: %ld %d\n", tp->sec, tp->msec);

    ms = (ngx_msec_int_t)
             ((tp->sec - ctx->timer_begin.sec) * 1000 +
              (tp->msec - ctx->timer_begin.msec));
    ms = (ms >= 0) ? ms : 0;

    size = sizeof("-9223372036854775808.000") - 1;

    p = ngx_palloc(r->pool, size);
    if (p == NULL) {
        return NGX_ERROR;
    }

    v->len = ngx_snprintf(p, size, "%T.%03M",
             ms / 1000, ms % 1000) - p;
    v->data = p;

    v->valid = 1;
    v->no_cacheable = 1;
    v->not_found = 0;

    return NGX_OK;
}
u_char *
ngx_http_req_stat_request_time(ngx_http_request_t *r, u_char *buf,
    ngx_http_req_stat_op_t *op)
{
    ngx_time_t      *tp;
    ngx_msec_int_t   ms;

    tp = ngx_timeofday();

    ms = (ngx_msec_int_t)
             ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
    ms = ngx_max(ms, 0);

    return ngx_sprintf(buf, "%T.%04M", ms / 1000, ms % 1000);
}
Beispiel #20
0
static u_char *
ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
    ngx_http_log_op_t *op)
{
    ngx_time_t      *tp;
    ngx_msec_int_t   ms;

    tp = ngx_timeofday();

    ms = (ngx_msec_int_t)
             ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
    ms = (ms >= 0) ? ms : 0;

    return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
}
static void
ngx_tcp_upstream_finalize_session(ngx_tcp_session_t *s,
        ngx_tcp_upstream_t *u, ngx_int_t rc) 
{
    ngx_time_t  *tp;

    ngx_log_debug1(NGX_LOG_DEBUG_TCP, s->connection->log, 0,
            "finalize tcp upstream session: %i", rc);

    if (u->cleanup) {
        *u->cleanup = NULL;
        u->cleanup = NULL;
    }

    if (u->state && u->state->response_sec) {
        tp = ngx_timeofday();
        u->state->response_sec = tp->sec - u->state->response_sec;
        u->state->response_msec = tp->msec - u->state->response_msec;
    }

    if (u->peer.free) {
        u->peer.free(&u->peer, u->peer.data, 0);
    }

    if (u->peer.check_index != NGX_INVALID_INDEX) {
        ngx_tcp_check_free_peer(u->peer.check_index);
        u->peer.check_index = NGX_INVALID_INDEX;
    }

    if (u->peer.connection) {

        ngx_log_debug1(NGX_LOG_DEBUG_TCP, s->connection->log, 0,
                "close tcp upstream connection: %d",
                u->peer.connection->fd);

            ngx_close_connection(u->peer.connection);
    }

    u->peer.connection = NULL;

    if (rc == NGX_DECLINED || rc == NGX_DONE) {
        return;
    }

    s->connection->log->action = "sending to client";

    ngx_tcp_finalize_session(s);
}
static void *
ngx_http_status_code_counter_create_conf(ngx_conf_t *cf)
{
    ngx_http_status_code_counter_conf_t    *conf;
    ngx_time_t  *time;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_status_code_counter_conf_t));
    if (NULL == conf) {
        return NULL;
    }

    time = ngx_timeofday();
    conf->startup = time->sec;

    return conf;
}
Beispiel #23
0
static u_char *
ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
    ngx_http_log_op_t *op)
{
		syslog(LOG_INFO, "[%s:%s:%d]", __FILE__, __func__, __LINE__);
    ngx_time_t      *tp;
    ngx_msec_int_t   ms;

    tp = ngx_timeofday();

    ms = (ngx_msec_int_t)
             ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
    ms = ngx_max(ms, 0);

    return ngx_sprintf(buf, "%T.%03M", ms / 1000, ms % 1000);
}
ngx_int_t
ngx_http_echo_exec_echo_reset_timer(ngx_http_request_t *r,
    ngx_http_echo_ctx_t *ctx)
{
    dd("Exec timer...");

    /* force the ngx timer to update */

#if (nginx_version >= 8035) || (nginx_version < 8000 && nginx_version >= 7066)
    ngx_time_update();
#else
    ngx_time_update(0, 0);
#endif

    ctx->timer_begin = *ngx_timeofday();
    return NGX_OK;
}
ngx_int_t
ngx_http_statu_update_requesttime(ngx_http_request_t *r)

{
	ngx_time_t                *tp;
    ngx_msec_int_t             ms;
    
    tp = ngx_timeofday();

    ms = (ngx_msec_int_t)
         ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));

    ms = ngx_max(ms, 0);
	
/*	(void) ngx_atomic_fetch_add(ngx_stat_requesttime, ms);
*/
    return NGX_OK;
Beispiel #26
0
/* TODO: change impl when we'll get SFMT? */
ngx_int_t
ngx_nats_create_inbox(u_char *buf, size_t bufsize)
{
    ngx_time_t     *tp;
    ngx_addr_t     *local_ip;
    u_char         *pend;
    size_t          i;
    uint32_t        partA, partB, partC, partD, ipvar;
    uint32_t        r1, r2, r3, r4;

    if (bufsize < 34) {
        return NGX_ERROR;
    }

    ngx_time_update();
    tp = ngx_timeofday();

    local_ip = ngx_nats_get_local_ip();

    ipvar = 0;

    if (local_ip != NULL) {
        for (i = 0; i < local_ip->name.len; i++) {
            ipvar = (ipvar * 31) + (uint32_t)local_ip->name.data[i];
        }
    }

    ngx_nats_init_random();

    r1 = (uint32_t) ngx_nats_next_random();
    r2 = (uint32_t) ngx_nats_next_random();
    r3 = (uint32_t) ngx_nats_next_random();
    r4 = (uint32_t) ngx_nats_next_random();

    partA = _nats_rand4(ipvar, r1, (uint32_t)ngx_pid, (uint32_t)tp->msec);
    partB = _nats_rand4(ipvar, r2, (uint32_t)ngx_pid, (uint32_t)tp->sec);
    partC = _nats_rand4(ipvar, r3, (uint32_t)tp->sec, (uint32_t)tp->msec);
    partD = (uint32_t) (r4 & 0x00ff);   /* 1 byte only */

    pend = ngx_snprintf(buf, bufsize, "_INBOX.%08xD%08xD%08xD%02xD", partA, partB, partC, partD);

    *pend = 0;

    return (ngx_int_t)(pend - buf);
}
static void
ngx_http_poller_event(ngx_event_t *ev)
{
  ngx_http_poller_t   *poller = ev->data;
  ngx_http_request_t  *r;
  ngx_time_t          *tp;

  if (ngx_exiting) {
    return;
  }

  tp = ngx_timeofday();
  poller->poll_start = tp->sec * 1000 + tp->msec;

  r = ngx_http_poller_request(poller);
  if (r != NULL) {
    ngx_http_upstream_init(r);
  }
}
static void
worker_process_alarm_handler(ngx_event_t *ev)
{
    ngx_time_t  *time;
    ngx_msec_t   next;

    time = ngx_timeofday();

    ngx_http_accounting_old_time = ngx_http_accounting_new_time;
    ngx_http_accounting_new_time = time->sec;

    ngx_http_accounting_hash_iterate(&stats_hash, worker_process_write_out_stats, NULL, NULL);

    if (ngx_exiting || ev == NULL)
        return;

    next = (ngx_msec_t)WORKER_PROCESS_TIMER_INTERVAL * 1000;

    ngx_add_timer(ev, next);
}
static void
ngx_http_poller_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
{
  ngx_http_poller_t  *poller;
  ngx_str_t           interval;
  ngx_time_t         *tp;
  ngx_msec_t          msec;
  uint64_t            now;

  poller = ngx_http_get_module_ctx(r, ngx_http_poller_module);
  if (poller->handler.finalize != NULL) {
    poller->handler.finalize(r, rc);
  }

  if (ngx_http_complex_value(r, &poller->interval, &interval) != NGX_OK) {
    return;
  }

  msec = ngx_parse_time(&interval, 0);
  if (msec == (ngx_msec_t)NGX_ERROR) {
    return;
  }

  tp = ngx_timeofday();
  now = tp->sec * 1000 + tp->msec;
  if (now > poller->poll_start) {
    if (now - poller->poll_start < msec) {
      msec -= (now - poller->poll_start);
    } else {
      msec = 0;
    }
  }

  ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
		 "[poller] %V: calling again in %uD ms",
		 &poller->name, (uint32_t)msec);

  ngx_add_timer(&poller->poll_event, msec);

  r->connection->log->log_level = NGX_LOG_EMERG;
}
static ngx_int_t
ngx_http_req_status_log_handler(ngx_http_request_t *r)
{
    // off_t                                   bytes;
    ngx_uint_t                              i;
    // ngx_msec_t                              td;
    ngx_http_req_status_ctx_t              *r_ctx;
    ngx_http_req_status_data_t             *data;
    ngx_http_req_status_zone_node_t        *pzn;
    // ngx_http_req_status_main_conf_t        *rmcf;

    ngx_time_t                   *tp;
    ngx_msec_int_t                ms;
    r_ctx = ngx_http_get_module_ctx(r, ngx_http_req_status_module);
    if (r_ctx == NULL || r_ctx->req_zones.nelts == 0){
        return NGX_DECLINED;
    }

    // rmcf = ngx_http_get_module_main_conf(r, ngx_http_req_status_module);

    pzn = r_ctx->req_zones.elts;

    for (i = 0; i < r_ctx->req_zones.nelts; i++){
        data = &pzn[i].node->data;

        ngx_shmtx_lock(&pzn[i].zone->shpool->mutex);

        // add response time
        tp = ngx_timeofday();
        ms = (ngx_msec_int_t)
             ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
        ms = ngx_max(ms, 0);
        data->rt += ms;
        // add response time

        ngx_shmtx_unlock(&pzn[i].zone->shpool->mutex);
    }

    return NGX_DECLINED;
}