Help building an extension with a persistent resource

From: Date: Wed, 04 Oct 2000 16:23:41 +0000
Subject: Help building an extension with a persistent resource
Groups: php.dev 
Request: Send a blank email to php-dev+get-34234@lists.php.net to get a copy of this message
Hello, I'm trying to build an extension with a function (test_fnc1) that maintains a persistent resource, just to learn how to use persistent resources. Every time you call this function with a given id, it increases the value of the resource (a simple int) by one and returns its value. Every id has its own counter. It works OK, but because my Apache server has 4 childs running, every instance maintains its own resource, so there are 4 counters and every time I call the function it increases one of the counters, I can't choose which one. And my questions are: - Is there a method to maintain just one counter between all apache childs? - How can I release the counter after the client is disconnected? The idea is release the counter after a given timeout. Any way to do it? - The code below is ok to maintain a persistent resource? Do I need something else? Thank you V. Jove vjove@ctc.es -------------------------------------------------- My configuration is: PHP 4.0.2 Apache 1.3.19 PHP built with: ./configure --enable-test --with-apxs=/usr/local/apache/bin/apxs -------------------------------------------------- typedef { int counter; } RESOURCE_T; /* {{{ proto int test_fnc1(int i) Return a string to confirm that the module is compiled in */ PHP_FUNCTION(test_fnc1) { RESOURCE_T *res; int type; int id; char buffer[25]; char *hd; int lenhd; list_entry *le; zval **i; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &i) == FAILURE) { WRONG_PARAM_COUNT; } convert_to_long_ex(i); TESTLS_FETCH(); hd = (char *)emalloc(25); sprintf(hd, "test_%d", (*i)->value.lval); lenhd = strlen(hd); if ( zend_hash_find(&EG(persistent_list), hd, lenhd+1, (void **)&le) == FAILURE ) { /* first time we call the function with this id --> we must create the persistent resource */ list_entry new_le; res = (RESOURCE_T *)malloc(sizeof(RESOURCE_T)); if ( !res ) { php3_error(E_WARNING, "memory error"); RETURN_FALSE; } res->counter = 0; new_le.type = TESTG(le_test); new_le.ptr = res; if ( zend_hash_update(&EG(persistent_list), hd, lenhd+1, (void *)&new_le, sizeof(list_entry), NULL) == FAILURE ) { free(res); efree(hd); php3_error(E_WARNING, "failed update"); RETURN_FALSE; } } else { /* this counter already exists, we just increment it and return the new value */ if ( le->type != TESTG(le_test) ) { php3_error(E_WARNING, "different type"); RETURN_FALSE; } res = (RESOURCE_T *)le->ptr; res->counter++; } sprintf(buffer, "%d", res->counter); RETURN_STRING(buffer,1); } /* }}} */

« previous php.dev (#34234) next »