Search code examples
g-wan

How to disable G-WAN servlet internal cache?


gwan version: 3.12.26

servlet type: C and Perl

problem:

gwan internal cache make request not re-read the script

test:

  1. create 'log' dir :

    [bash]# mkdir -p /dev/shm/random-c
    [bash]# chmod 777 /dev/shm/random-c
    
  2. create /path/to/gwan/0.0.0.0_8080/#0.0.0.0/csp/random.c

    // ============================================================================
    // C servlet sample for the G-WAN Web Application Server (http://trustleap.ch/)
    // ----------------------------------------------------------------------------
    // hello.c: just used with Lighty's Weighttp to benchmark a minimalist servlet
    // ============================================================================
    // imported functions:
    //   get_reply(): get a pointer on the 'reply' dynamic buffer from the server
    //    xbuf_cat(): like strcat(), but it works in the specified dynamic buffer
    // ----------------------------------------------------------------------------
    #include <sys/time.h>
    #include "gwan.h" // G-WAN exported functions
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    //------------------
    void init_random(){
        struct /*sys/time.h->*/timeval res;
        /*sys/time.h->*/gettimeofday(&res,NULL);
       /*stdlib.h->*/srand( (unsigned int)/*stdlib.h->*/time(NULL) + res.tv_usec);
    }
    
    //------------------
    char *get_rnd_char(int num){
        char *char_list = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int  char_list_len = 62;
        char *ret = (char *)/*stdlib.h->*/malloc((num * sizeof(char)) + 1);
        int i,r;
    
    
        for(i=0;i<num;i++){
            r=(int) (/*stdlib.h->*/rand() % char_list_len);
            ret[i] = char_list[r==char_list_len ? r-1 : r];
        }
        ret[num] = '\0';
        return ret;
    }
    
    //------------------
    int main(int argc, char *argv[])
    {
        char *rnd_out; //-- random data for browser output and file input
        char *rnd_file; //-- random file
        char *rnd_path; //-- for speed let's make on ramdisk /dev/shm/random-c/
        char *t;
        FILE *F;
    
        int num_char=10;
        int arg_cnt=1;
    
        if(argc>0){
            //-- why nobody love C ? one of the reason is these kind parsing thing
            while ((t = /*string.h->*/strtok(argv[0], "=")) != NULL) {
                argv[0] = NULL;
                if(arg_cnt == 2){
                    num_char = /*stdlib.h->*/atoi(t);
                }
                arg_cnt++;
            }
        }else{
            //-- get random number betwen 1 to 1000
            num_char = (rand() % 1000)+1;
        }
    
        init_random();
    
    
       //-- create random data
        rnd_out = get_rnd_char(num_char);
    
    
        //-- creating "log" path
        //-- why nobody love C ? more reason
       rnd_file = get_rnd_char(20);
       // "/dev/shm/random-c/xxxxxxxxxxxxxxxxxxxx" -> 38 chars + 1 for \0
       rnd_path = (char *)/*stdlib.h->*/malloc((38 * sizeof(char)) + 1);
       rnd_path[0] = '\0';
       /*string.h->*/strcat(rnd_path,"/dev/shm/random-c/");
       /*string.h->*/strcat(rnd_path,rnd_file);
    
        //-- save to file
        F = /*stdio.h->*/fopen(rnd_path,"w");
            /*stdio.h->*/fprintf(F,"%s",rnd_out);
        /*stdio.h->*/fclose(F);
    
    
       //-- send output to browser
       /*gwan.h->*/xbuf_cat(get_reply(argv), rnd_out);
    
    
        //-- cleanup memory
        //-- why nobody love C ? MAIN reason: no easy way of memory management
       /*stdlib.h->*/free(rnd_file);
       /*stdlib.h->*/free(rnd_out);
       /*stdlib.h->*/free(rnd_path);
    
       return 200; // return an HTTP code (200:'OK')
    }
    
    // ============================================================================
    // End of Source Code
    // ============================================================================
    
  3. run on browser:

    http://localhost:8080/?random.c 
    

    then you should have one 20char random file at /dev/shm/random-c/

  4. here the 'problem', run:

    ab -n 1000 'http://localhost:8080/?random.c'
    

    my ubuntu have output:

    Finished 1000 requests
    
    
    Server Software:        G-WAN
    Server Hostname:        localhost
    Server Port:            8080
    
    Document Path:          /?random.c
    Document Length:        440 bytes
    
    Concurrency Level:      1
    Time taken for tests:   0.368 seconds
    Complete requests:      1000
    Failed requests:        361
       (Connect: 0, Receive: 0, Length: 361, Exceptions: 0)
    Write errors:           0
    Total transferred:      556492 bytes
    HTML transferred:       286575 bytes
    Requests per second:    2718.73 [#/sec] (mean)
    Time per request:       0.368 [ms] (mean)
    Time per request:       0.368 [ms] (mean, across all concurrent requests)
    Transfer rate:          1477.49 [Kbytes/sec] received
    

    try:

    [bash]# ls /dev/shm/random-c/
    

    the directory only list 4 or 5 random files, which expected was 1000files

  5. tested on random.c and perl's version random.pl

so the back to beginning question, how to disable GWAN internal cache, I try to read gwan user guide for set something in handler, but found nothing (or I miss something in that guide ).

thanks for GWAN team for this great product. any answer welcome .. thanks


Solution

  • I think that the feature you are talking about is micro caching. To disable it, the URI needs to be unique on each request within 200 ms. (Like adding random number on URI)

    The G-WAN FAQ state:

    "To spare the need for a frontend cache server (and to let G-WAN be used as a caching reverse-proxy) G-WAN supports micro-caching, a RESTful feature. When a given URI is invoked at high concurrencies and when generating the payload take a lot of time, then G-WAN will automatically cache a page for 200 milliseconds (the average latency on the Internet) to make sure that the cache is up-to-date: within 200 ms, consecutive requests provide the expected result. To prevent micro-caching from being triggered, use a changing query parameter (per user session id, random, counter, etc.) for concurrent requests."

    Note that for v4.10+ caching is disabled by default, look at the gwan/init.c file.