Nginx 基于slab内存实现的客户端访问限制(IP+URL)

本实例来源于《深入理解Nginx》书中的源码。网上未查到可复制使用的源码,因此亲自编译测试并写此博客,定为转载类型,供nginx爱好者学习交流。

 

测试结果:

10秒内第一次访问结果:HelloWorld

10秒内第2、3..次访问结果:403 Forbidden

 

源码:

config:


http {

test_slab 10 10M; #限制10s一次的访问频率,slab内存大小为10M

server {

          location /hello {
            default_type text/html;
            content_by_lua_block {
                ngx.say("HelloWorld")
            }
        }
}

}

ngx_http_testslab_module.c

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

typedef struct
{
    u_char rbtree_node_data;

    ngx_queue_t queue;

    ngx_msec_t last;

    u_short len;

    u_char data[1];

} ngx_http_testslab_node_t;

typedef struct
{
   ngx_rbtree_t rbtree;

   ngx_rbtree_node_t sentinel;

   ngx_queue_t queue;
} ngx_http_testslab_shm_t;

typedef struct
{
    ssize_t shmsize;

    ngx_int_t interval;

    ngx_slab_pool_t *shpool;

    ngx_http_testslab_shm_t *sh;
} ngx_http_testslab_conf_t;

static ngx_int_t
ngx_http_testslab_handler(ngx_http_request_t *r);

static char * ngx_http_testslab_createmem(ngx_conf_t *cf, ngx_conf_t *cmd, void *conf);

static ngx_int_t ngx_http_testslab_init(ngx_conf_t *cf);

static void *ngx_http_testslab_create_main_conf(ngx_conf_t *cf);

static ngx_command_t  ngx_http_testslab_commands[] =
{

    {
        ngx_string("test_slab"),
        NGX_HTTP_MAIN_CONF | NGX_CONF_TAKE2,
        ngx_http_testslab_createmem,
        0,
        0,
        NULL
    },

    ngx_null_command
};


static ngx_http_module_t  ngx_http_testslab_module_ctx =
{
    NULL, /* preconfiguration */
    ngx_http_testslab_init, /* postconfiguration */

    ngx_http_testslab_create_main_conf, /* create main configuration */
    NULL, /* init main configuration */

    NULL, /* create server configuration */
    NULL, /* merge server configuration */



    NULL,/* create location configuration */
    NULL  /* merge location configuration */
};


ngx_module_t  ngx_http_testslab_module =
{
    NGX_MODULE_V1,
    &ngx_http_testslab_module_ctx,           /* module context */
    ngx_http_testslab_commands,              /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};

static void *ngx_http_testslab_create_main_conf(ngx_conf_t *cf)
{
    ngx_http_testslab_conf_t *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_testslab_conf_t));

    if (NULL == conf)
    {
        return NULL;
    }

    conf->interval = -1;
    conf->shmsize = -1;

    return conf;
}

static void
ngx_http_testslab_rbtree_insert_value(ngx_rbtree_node_t *temp,
        ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
{
   ngx_rbtree_node_t **p;
   ngx_http_testslab_node_t *lrn, *lrnt;

    for (;;)
    {
        if (node->key < temp->key)
        {
            p = &temp->left;
        }
        else if (node->key > temp->key) {
           p = &temp->right;
        }
        else
        {
          lrn = (ngx_http_testslab_node_t *)&node->data;
          lrnt = (ngx_http_testslab_node_t *)&temp->data;

          p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0) ?
            &temp->left : &temp->right;
        }

        if (*p == sentinel)
        {
            break;
        }

        temp = *p;
    }

    *p = node;

    node->parent = temp;
    node->left = sentinel;
    node->right = sentinel;
    ngx_rbt_red(node);
}

static void

ngx_http_testslab_expire(ngx_http_request_t *r, ngx_http_testslab_conf_t *conf)
{
    ngx_time_t *tp;
    ngx_msec_t now;
    ngx_queue_t *q;
    ngx_msec_int_t ms;
    ngx_rbtree_node_t *node;
    ngx_http_testslab_node_t *lr;

    tp = ngx_timeofday();

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

    while (1)
    {
        if (ngx_queue_empty(&conf->sh->queue))
        {
            return;
        }

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

        lr = ngx_queue_data(q, ngx_http_testslab_node_t, queue);

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

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

        if (ms < conf->interval)
        {
            return;
        }

        ngx_queue_remove(q);

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

        ngx_slab_free_locked(conf->shpool, node);
   }

}

static ngx_int_t
ngx_http_testslab_lookup(ngx_http_request_t *r, ngx_http_testslab_conf_t *conf,
ngx_uint_t hash, u_char* data, size_t len)
{
    size_t size;
    ngx_int_t rc;
    ngx_time_t *tp;
    ngx_msec_t now;
    ngx_msec_int_t ms;
    ngx_rbtree_node_t *node, *sentinel;
    ngx_http_testslab_node_t *lr;

    tp = ngx_timeofday();

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

    node = conf->sh->rbtree.root;
    sentinel = conf->sh->rbtree.sentinel;

    while (node != sentinel)
    {
        if (hash < node->key)
        {
             node = node->left;
             continue;
        }

        if (hash > node->key)
        {
           node = node->right;
           continue;
        }

        lr = (ngx_http_testslab_node_t *)&node->data;

        rc = ngx_memn2cmp(data, lr->data, len, (size_t)lr->len);

        if (rc == 0)
        {
            ms = (ngx_msec_int_t)(now - lr->last);

            if (ms > conf->interval)
            {
                lr->last = now;

                ngx_queue_remove(&lr->queue);
                ngx_queue_insert_head(&conf->sh->queue, &lr->queue);

                return NGX_DECLINED;
            }
            else
            {
                return NGX_HTTP_FORBIDDEN;
            }
        }

        node = (rc < 0) ? node->left : node->right;
    }

    size = offsetof(ngx_rbtree_node_t, data) + offsetof(ngx_http_testslab_node_t, data)
        + len;

    ngx_http_testslab_expire(r, conf);

    node = ngx_slab_alloc_locked(conf->shpool, size);
    if (node == NULL)
    {
        return NGX_ERROR;
    }

    node->key = hash;

    lr = (ngx_http_testslab_node_t *)&node->data;

    lr->last = now;

    lr->len = (u_char)len;
    ngx_memcpy(lr->data, data, len);
    ngx_rbtree_insert(&conf->sh->rbtree, node);

    ngx_queue_insert_head(&conf->sh->queue, &lr->queue);

    return NGX_DECLINED;
}

static ngx_int_t ngx_http_testslab_init(ngx_conf_t *cf)
{
    ngx_http_handler_pt *h;
    ngx_http_core_main_conf_t *cmcf;

    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

    h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
    if (NULL == h)
    {
        return NGX_ERROR;
    }

    *h = ngx_http_testslab_handler;

    return NGX_OK;
}

static ngx_int_t
ngx_http_testslab_handler(ngx_http_request_t *r)
{
    size_t len;
    uint32_t hash;
    ngx_int_t rc;
    ngx_http_testslab_conf_t *conf;

    conf = ngx_http_get_module_main_conf(r, ngx_http_testslab_module);
    rc = NGX_DECLINED;

    if (conf->interval == -1)
    {
        return rc;
    }

    len = r->connection->addr_text.len + r->uri.len;
    u_char* data = ngx_palloc(r->pool, len);
    ngx_memcpy(data, r->uri.data, r->uri.len);
    ngx_memcpy(data + r->uri.len, r->connection->addr_text.data,
        r->connection->addr_text.len);

    hash = ngx_crc32_short(data, len);
    ngx_shmtx_lock(&conf->shpool->mutex);

    rc = ngx_http_testslab_lookup(r, conf, hash, data, len);

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

    return rc;
}

static ngx_int_t
ngx_http_testslab_shm_init(ngx_shm_zone_t *shm_zone, void *data)
{
    ngx_http_testslab_conf_t *conf;

    ngx_http_testslab_conf_t *oconf = data;
    size_t len;

    conf = (ngx_http_testslab_conf_t *)shm_zone->data;

    if (oconf)
    {
        conf->sh = oconf->sh;
        conf->shpool = oconf->shpool;

        return NGX_OK;
    }

    conf->shpool = (ngx_slab_pool_t *)shm_zone->shm.addr;

    conf->sh = ngx_slab_alloc(conf->shpool, sizeof(ngx_http_testslab_shm_t));
    if (conf->sh == NULL)
    {
        return NGX_ERROR;
    }

    conf->shpool->data = conf->sh;

    ngx_rbtree_init(&conf->sh->rbtree, &conf->sh->sentinel,
                ngx_http_testslab_rbtree_insert_value);

    ngx_queue_init(&conf->sh->queue);

    len = sizeof(" in testslab \"\"") + shm_zone->shm.name.len;

    conf->shpool->log_ctx = ngx_slab_alloc(conf->shpool, len);
    if (conf->shpool->log_ctx == NULL)
    {
        return NGX_ERROR;
    }

    ngx_sprintf(conf->shpool->log_ctx, " in testslab \"%V\"%z",
        &shm_zone->shm.name);

    return NGX_OK;
}

static char * ngx_http_testslab_createmem(ngx_conf_t *cf, ngx_conf_t *cmd, void *conf)
{
    ngx_str_t *value;
    ngx_shm_zone_t *shm_zone;

    ngx_http_testslab_conf_t *mconf = (ngx_http_testslab_conf_t *)conf;

    ngx_str_t name = ngx_string("test_slab_shm");

    value = cf->args->elts;

    mconf->interval = 1000 *ngx_atoi(value[1].data, value[1].len);
    if (mconf->interval == NGX_ERROR || mconf->interval == 0)
    {
        mconf->interval = -1;
        return "invalid value";
    }

    mconf->shmsize = ngx_parse_size(&value[2]);
    if (mconf->shmsize == (ssize_t) NGX_ERROR || mconf->shmsize == 0)
    {
        mconf->interval = -1;
        return "invalid value";
    }

    shm_zone = ngx_shared_memory_add(cf, &name, mconf->shmsize,
            &ngx_http_testslab_module);
    if (NULL == shm_zone)
    {
        mconf->interval = -1;
        return NGX_CONF_ERROR;
    }

    shm_zone->init = ngx_http_testslab_shm_init;

    shm_zone->data = mconf;

    return NGX_CONF_OK;
}

 

 

 

 

通过把nginx slab的精简,把需要的头文件单独整理出来,增加了main方法,可以单独运行,代码包含了大量的中文注释,方便了了解和学习slab的运行机制 int main(int argc, char **argv) { ngx_log_t log; ngx_shm_t shm; ngx_slab_pool_t *shpool; char *ssl_session[10]; int n; ngx_pid = ngx_getpid(); memset(&log;, 0, sizeof(ngx_log_t)); memset(&shm;, 0, sizeof(ngx_shm_t)); shm.size = 512000; ngx_pagesize_shift = 0; ngx_pagesize = getpagesize(); for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ } printf("--%d\n", 1 << ngx_pagesize_shift); if (ngx_shm_alloc(&shm;) != NGX_OK) { return 1; } if (ngx_init_zone_pool(&log;, &shm;)) { return 1; } shpool = (ngx_slab_pool_t *) shm.addr; ssl_session[0] = (char *) ngx_slab_alloc_locked(shpool, 12); ssl_session[0] = (char *) ngx_slab_calloc(shpool, 56); ssl_session[1] = (char *) ngx_slab_alloc_locked(shpool, 14); ssl_session[2] = (char *) ngx_slab_alloc_locked(shpool, 11); ngx_slab_free(shpool, ssl_session[2]); ngx_slab_free(shpool, ssl_session[0]); ssl_session[2] = (char *) ngx_slab_alloc_locked(shpool, 65); return 0; } nginx的slab内存管理方式,这种方式的内存管理在nginx中,主要是与nginx 的共享内存协同使用的。nginx的slab管理与linux的slab管理相同的地方在于均是利用了内存 的缓存与对齐机制,slab内存管理中一些设计相当巧妙的地方,也有一些地方个人感觉设计 不是很完美,或许是作为nginx设计综合考虑的结果。 nginx slab实现中的一大特色就是大量的位操作,这部分操作很多是与slot分级数组相关的。 为方便描述下面做一些说明: 1.将整个slab的管理结构称slab pool. 2.将slab pool前部的ngx_slab_pool_t结构称slab header. 3.将管理内存分级的ngx_slab_page_t结构称slot分级数组. 4.将管理page页使用的ngx_slab_page_t结构称slab page管理结构. 5.将具体page页的存放位置称pages数组. 6.将把page分成的各个小块称为chunk. 7.将标记chunk使用的位图结构称为bitmap.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值