[DEV] nginx ngx_hash_t usage, and other questions...

Igor Sysoev is at rambler-co.ru
Mon Sep 24 23:07:51 MSD 2007

On Mon, Sep 24, 2007 at 08:14:19PM +0200, Brice Figureau wrote:

> I'm in the progress of hacking an upload progress module for nginx, where
> I will track some request and answer from other requests what was
> uploaded.
> I need to store an upload request in a hash table whose key is a
> connection id sent in an upload header.
> My idea was to use the the ngx_hash_t type to do that, but looking at the
> source code I'm puzzled and have the following questions:
> 1) Is it possible to init the hash with nelts=0 and names=NULL ?
> I don't know the elements at the hash creation time.
> 2) What is the purpose of the ngx_hash_keys_arrays_t parameter in
> ngx_hash_add_key ?
> After initing one, what should I put in there ?

ngx_hash_t is not dynamic hash. It's special hash that build
at reconfiguration time from ready data. While hash initalizing nginx
finds the optimal hash size to fit one or several keys in single bucket.
The bucket size is one or several CPU cacheline, so this allows to lookup
in hash with minimum CPU cache misses.

For dynamic data I use rbtree.

> I also have a few more general or basic questions:
> 1) There are a lot of ngx_str_t used, but all the ngx_string.c functions
> are using u_char* . Does it mean the strings are always null terminated
> and the ngx_str_t len parameter is used for something else ?
> So to use ngx_strcmp with 2 ngx_str_t, I just have to call it with the value?

No, ngx_str_t may be null terminated (and len does count null in the case),
but in general it is not null terminated.

> 2) Can I register an handler or a phase handler that will be triggered at
> the end of a request (and if yes how) ?
> This is to do the cleanup in my hash table containing the uploading requests.

Use ngx_pool_cleanup_add(r->pool, size).

Igor Sysoev

More information about the nginx mailing list