Example #1
0
svn_error_t *
svn_cache__make_memcache_from_config(svn_memcache_t **memcache_p,
                                    svn_config_t *config,
                                    apr_pool_t *pool)
{
  int server_count;
  apr_pool_t *subpool = svn_pool_create(pool);

  server_count =
    svn_config_enumerate2(config,
                          SVN_CACHE_CONFIG_CATEGORY_MEMCACHED_SERVERS,
                          nop_enumerator, NULL, subpool);

  if (server_count == 0)
    {
      *memcache_p = NULL;
      svn_pool_destroy(subpool);
      return SVN_NO_ERROR;
    }

  if (server_count > APR_INT16_MAX)
    return svn_error_create(SVN_ERR_TOO_MANY_MEMCACHED_SERVERS, NULL, NULL);

#ifdef SVN_HAVE_MEMCACHE
  {
    struct ams_baton b;
    svn_memcache_t *memcache = apr_pcalloc(pool, sizeof(*memcache));
    apr_status_t apr_err = apr_memcache_create(pool,
                                               (apr_uint16_t)server_count,
                                               0, /* flags */
                                               &(memcache->c));
    if (apr_err != APR_SUCCESS)
      return svn_error_wrap_apr(apr_err,
                                _("Unknown error creating apr_memcache_t"));

    b.memcache = memcache->c;
    b.memcache_pool = pool;
    b.err = SVN_NO_ERROR;
    svn_config_enumerate2(config,
                          SVN_CACHE_CONFIG_CATEGORY_MEMCACHED_SERVERS,
                          add_memcache_server, &b,
                          subpool);

    if (b.err)
      return b.err;

    *memcache_p = memcache;

    svn_pool_destroy(subpool);
    return SVN_NO_ERROR;
  }
#else /* ! SVN_HAVE_MEMCACHE */
  {
    return svn_error_create(SVN_ERR_NO_APR_MEMCACHE, NULL, NULL);
  }
#endif /* SVN_HAVE_MEMCACHE */
}
Example #2
0
/**
 * \private \memberof mapcache_cache_memcache
 */
static void _mapcache_cache_memcache_configuration_parse_xml(mapcache_context *ctx, ezxml_t node, mapcache_cache *cache, mapcache_cfg *config) {
   ezxml_t cur_node;
   mapcache_cache_memcache *dcache = (mapcache_cache_memcache*)cache;
   int servercount = 0;
   for(cur_node = ezxml_child(node,"server"); cur_node; cur_node = cur_node->next) {
      servercount++;
   }
   if(!servercount) {
      ctx->set_error(ctx,400,"memcache cache %s has no <server>s configured",cache->name);
      return;
   }
   if(APR_SUCCESS != apr_memcache_create(ctx->pool, servercount, 0, &dcache->memcache)) {
      ctx->set_error(ctx,400,"cache %s: failed to create memcache backend", cache->name);
      return;
   }
   for(cur_node = ezxml_child(node,"server"); cur_node; cur_node = cur_node->next) {
      ezxml_t xhost = ezxml_child(cur_node,"host");
      ezxml_t xport = ezxml_child(cur_node,"port");
      const char *host;
      apr_memcache_server_t *server;
      apr_port_t port;
      if(!xhost || !xhost->txt || ! *xhost->txt) {
         ctx->set_error(ctx,400,"cache %s: <server> with no <host>",cache->name);
         return;
      } else {
         host = apr_pstrdup(ctx->pool,xhost->txt);
      }

      if(!xport || !xport->txt || ! *xport->txt) {
         ctx->set_error(ctx,400,"cache %s: <server> with no <port>", cache->name);
         return;
      } else {
         char *endptr;
         int iport = (int)strtol(xport->txt,&endptr,10);
         if(*endptr != 0) {
            ctx->set_error(ctx,400,"failed to parse value %s for memcache cache %s", xport->txt,cache->name);
            return;
         }
         port = iport;
      }
      if(APR_SUCCESS != apr_memcache_server_create(ctx->pool,host,port,4,5,50,10000,&server)) {
         ctx->set_error(ctx,400,"cache %s: failed to create server %s:%d",cache->name,host,port);
         return;
      }
      if(APR_SUCCESS != apr_memcache_add_server(dcache->memcache,server)) {
         ctx->set_error(ctx,400,"cache %s: failed to add server %s:%d",cache->name,host,port);
         return;
      }
      if(APR_SUCCESS != apr_memcache_set(dcache->memcache,"mapcache_test_key","mapcache",8,0,0)) {
         ctx->set_error(ctx,400,"cache %s: failed to add test key to server %s:%d",cache->name,host,port);
         return;
      }
   }
}
Example #3
0
void mapcache_memcache_connection_constructor(mapcache_context *ctx, void **conn_, void *params, apr_pool_t *process_pool) {
  struct mapcache_memcache_conn_param *param = params;
  mapcache_cache_memcache *cache = param->cache;
  struct mapcache_memcache_pooled_connection *pc;
  int i;
  pc = calloc(1,sizeof(struct mapcache_memcache_pooled_connection));
  apr_pool_create(&pc->pool,process_pool);
  if(APR_SUCCESS != apr_memcache_create(pc->pool, cache->nservers, 0, &(pc->memcache))) {
    ctx->set_error(ctx,500,"cache %s: failed to create memcache backend", cache->cache.name);
    return;
  }
  for(i=0; i<param->cache->nservers; i++) {
    apr_memcache_server_t *server;
    if(APR_SUCCESS != apr_memcache_server_create(pc->pool,cache->servers[i].host,cache->servers[i].port,4,5,50,10000,&server)) {
      ctx->set_error(ctx,500,"cache %s: failed to create server %s:%d",cache->cache.name,cache->servers[i].host,cache->servers[i].port);
      return;
    }
    if(APR_SUCCESS != apr_memcache_add_server(pc->memcache,server)) {
      ctx->set_error(ctx,500,"cache %s: failed to add server %s:%d",cache->cache.name,cache->servers[i].host,cache->servers[i].port);
      return;
    }
  }
  *conn_ = pc;
}
Example #4
0
/*
 * initialize the memcache struct to a number of memcache servers
 */
static int oidc_cache_memcache_post_config(server_rec *s) {
	oidc_cfg *cfg = (oidc_cfg *) ap_get_module_config(s->module_config,
			&auth_openidc_module);

	if (cfg->cache_cfg != NULL)
		return APR_SUCCESS;
	oidc_cache_cfg_memcache_t *context = oidc_cache_memcache_cfg_create(
			s->process->pool);
	cfg->cache_cfg = context;

	apr_status_t rv = APR_SUCCESS;
	int nservers = 0;
	char* split;
	char* tok;
	apr_pool_t *p = s->process->pool;

	if (cfg->cache_memcache_servers == NULL) {
		oidc_serror(s,
				"cache type is set to \"memcache\", but no valid OIDCMemCacheServers setting was found");
		return HTTP_INTERNAL_SERVER_ERROR;
	}

	/* loop over the provided memcache servers to find out the number of servers configured */
	char *cache_config = apr_pstrdup(p, cfg->cache_memcache_servers);
	split = apr_strtok(cache_config, " ", &tok);
	while (split) {
		nservers++;
		split = apr_strtok(NULL, " ", &tok);
	}

	/* allocated space for the number of servers */
	rv = apr_memcache_create(p, nservers, 0, &context->cache_memcache);
	if (rv != APR_SUCCESS) {
		oidc_serror(s, "failed to create memcache object of '%d' size",
				nservers);
		return HTTP_INTERNAL_SERVER_ERROR;
	}

	/* loop again over the provided servers */
	cache_config = apr_pstrdup(p, cfg->cache_memcache_servers);
	split = apr_strtok(cache_config, " ", &tok);
	while (split) {
		apr_memcache_server_t* st;
		char* host_str;
		char* scope_id;
		apr_port_t port;

		/* parse out host and port */
		rv = apr_parse_addr_port(&host_str, &scope_id, &port, split, p);
		if (rv != APR_SUCCESS) {
			oidc_serror(s, "failed to parse cache server: '%s'", split);
			return HTTP_INTERNAL_SERVER_ERROR;
		}

		if (host_str == NULL) {
			oidc_serror(s,
					"failed to parse cache server, no hostname specified: '%s'",
					split);
			return HTTP_INTERNAL_SERVER_ERROR;
		}

		if (port == 0)
			port = 11211;

		/* create the memcache server struct */
		// TODO: tune this
		rv = apr_memcache_server_create(p, host_str, port, 0, 1, 1, 60, &st);
		if (rv != APR_SUCCESS) {
			oidc_serror(s, "failed to create cache server: %s:%d", host_str,
					port);
			return HTTP_INTERNAL_SERVER_ERROR;
		}

		/* add the memcache server struct to the list */
		rv = apr_memcache_add_server(context->cache_memcache, st);
		if (rv != APR_SUCCESS) {
			oidc_serror(s, "failed to add cache server: %s:%d", host_str, port);
			return HTTP_INTERNAL_SERVER_ERROR;
		}

		/* go to the next entry */
		split = apr_strtok(NULL, " ", &tok);
	}

	return OK;
}