5月 262012
 

VMOD processing want to before/after VCL function processing.
But, I do not want to write extra line.
How to do it?


Can be realized by the replace the pointer to sp->vcl->XXXX_func (VCL_XXXX)


Can be added to the processing by replacing pointer



static vcl_func_f         *vmod_redirect_Hook_vcl_error = NULL;
static pthread_mutex_t    vmod_redirect_mutex = PTHREAD_MUTEX_INITIALIZER;
static unsigned           hook_done = 0;

static int vmod_Hook_vcl_error(struct sess *sp){

    ....    //write the processing

    //call original vcl_error
    return(vmod_redirect_Hook_vcl_error(sp));

}

int
vmod_location(struct sess *sp, int status, const char*p,...)
{
        //vcl reload detection.
        if(hook_done == 1
           && sp->vcl->error_func != vmod_redirect_Hook_vcl_error ) hook_done = 0;

        if(hook_done == 0){
            //lock to prevent another thread write
            AZ(pthread_mutex_lock(&vmod_redirect_mutex));

            if(hook_done == 0)
            {
                //store the original vcl_error pointer
                vmod_redirect_Hook_vcl_error = sp->vcl->error_func;

                //hook
                sp->vcl->error_func = vmod_Hook_vcl_error;

                hook_done = 1;
            }
            //unlock
            AZ(pthread_mutex_unlock(&vmod_redirect_mutex));
        }

    ....

    return (status);
}

(via:vmod_redirect)

Hook to vcl_error at vmod processing in first time.
because there is no way to write-access the pointer at vmod_Init.


also, has locked using the mutex, because to prevent the loop by thread concurrent access.

I hope that this code is of help to you.

ATTENTION
don’t use varnishadm’s command “vcl.use” and “vcl.discard” . because to the segfault or call to other vcl function.


modify(2012-08-01)
when you vcl reloaded, hook method be off.


 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください