/*
TODO: copyright
*/

#include <ngx_core.h>
#include <ngx_http.h>

typedef struct {
  ngx_array_t limits;
  ngx_uint_t limit_log_level;
  ngx_uint_t delay_log_level;
  ngx_uint_t status_code;
  ngx_flag_t dry_run;
} ngx_http_limit_req_conf_t;

typedef struct {
  ngx_shm_zone_t *shm_zone;
  /* integer value, 1 corresponds to 0.001 r/s */
  ngx_uint_t burst;
  ngx_uint_t delay;
} ngx_http_limit_req_limit_t;

typedef struct {
  u_char color;
  u_char dummy;
  u_short len;
  ngx_queue_t queue;
  ngx_msec_t last;
  /* integer value, 1 corresponds to 0.001 r/s */
  ngx_uint_t excess;
  ngx_uint_t count;
  u_char data[1];
} ngx_http_limit_req_node_t;

typedef struct {
  ngx_rbtree_t rbtree;
  ngx_rbtree_node_t sentinel;
  ngx_queue_t queue;
} ngx_http_limit_req_shctx_t;

typedef struct {
  ngx_http_limit_req_shctx_t *sh;
  ngx_slab_pool_t *shpool;
  /* integer value, 1 corresponds to 0.001 r/s */
  ngx_uint_t rate;
  ngx_http_complex_value_t key;
  ngx_http_limit_req_node_t *node;
} ngx_http_limit_req_ctx_t;

extern ngx_module_t ngx_http_limit_req_module;
