Skip to content

Commit

Permalink
git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97…
Browse files Browse the repository at this point in the history
…f1-0310-9951-d761b3857238
  • Loading branch information
alobbs committed Sep 4, 2006
1 parent 63f1dd7 commit 7b0e48a
Show file tree
Hide file tree
Showing 56 changed files with 969 additions and 876 deletions.
8 changes: 5 additions & 3 deletions cherokee/Makefile.am
Expand Up @@ -749,6 +749,10 @@ endif




libcherokee_server_la_SOURCES = \ libcherokee_server_la_SOURCES = \
source.h \
source.c \
source_interpreter.h \
source_interpreter.c \
$(static_handler_file_src) \ $(static_handler_file_src) \
$(static_handler_admin_src) \ $(static_handler_admin_src) \
$(static_handler_dirlist_src) \ $(static_handler_dirlist_src) \
Expand Down Expand Up @@ -824,12 +828,10 @@ handler_error.c \
handler_error.h \ handler_error.h \
nonce.h \ nonce.h \
nonce.c \ nonce.c \
ext_source.h \
ext_source.c \
config_node.h \ config_node.h \
config_node.c \ config_node.c \
balancer.h \ balancer.h \
balancer.c balancer.c


libcherokee_config_la_SOURCES = \ libcherokee_config_la_SOURCES = \
$(config_common) \ $(config_common) \
Expand Down
129 changes: 103 additions & 26 deletions cherokee/balancer.c
Expand Up @@ -24,9 +24,11 @@


#include "common-internal.h" #include "common-internal.h"
#include "balancer.h" #include "balancer.h"
#include "module_loader.h"
#include "server-protected.h"
#include "source_interpreter.h"



#define DEFAULT_SOURCES_ALLOCATION 5
#define DEFAULT_HOSTS_ALLOCATION 5




ret_t ret_t
Expand All @@ -38,13 +40,13 @@ cherokee_balancer_init_base (cherokee_balancer_t *balancer)


/* Virtual methods /* Virtual methods
*/ */
balancer->dispatch = NULL; balancer->dispatch = NULL;


/* Hosts /* Sources
*/ */
balancer->hosts_len = 0; balancer->sources_len = 0;
balancer->hosts_size = 0; balancer->sources_size = 0;
balancer->hosts = NULL; balancer->sources = NULL;


return ret_ok; return ret_ok;
} }
Expand All @@ -53,61 +55,112 @@ cherokee_balancer_init_base (cherokee_balancer_t *balancer)
ret_t ret_t
cherokee_balancer_mrproper (cherokee_balancer_t *balancer) cherokee_balancer_mrproper (cherokee_balancer_t *balancer)
{ {
if (balancer->hosts != NULL) { if (balancer->sources != NULL) {
free (balancer->hosts); free (balancer->sources);
} }


return ret_ok; return ret_ok;
} }




ret_t
cherokee_balancer_configure (cherokee_balancer_t *balancer, cherokee_config_node_t *conf)
{
ret_t ret;
cherokee_list_t *i;
cherokee_buffer_t *buf;
cherokee_boolean_t interpreter = false;

/* Look for the type of the source objects
*/
ret = cherokee_config_node_read (conf, "type", &buf);
if (ret != ret_ok) {
PRINT_ERROR_S ("ERROR: Balancer: An entry 'type' is needed\n");
return ret;
}

if (equal_buf_str (buf, "interpreter")) {
interpreter = true;
} else {
PRINT_ERROR ("ERROR: Balancer: Unknown type '%s'\n", buf->buf);
return ret_error;
}

/* Add the source objects
*/
cherokee_config_node_foreach (i, conf) {
cherokee_source_t *src = NULL;
cherokee_config_node_t *subconf = CONFIG_NODE(i);

if (equal_buf_str (&subconf->key, "type"))
continue;

if (interpreter) {
cherokee_source_interpreter_t *src2;

ret = cherokee_source_interpreter_new (&src2);
if (ret != ret_ok) return ret;

ret = cherokee_source_interpreter_configure (src2, subconf);
if (ret != ret_ok) return ret;

src = SOURCE(src2);
}

cherokee_balancer_add_source (balancer, src);
}

return ret_ok;
}


static ret_t static ret_t
alloc_more_hosts (cherokee_balancer_t *balancer) alloc_more_sources (cherokee_balancer_t *balancer)
{ {
size_t size; size_t size;


if (balancer->hosts == NULL) { if (balancer->sources == NULL) {
size = DEFAULT_HOSTS_ALLOCATION * sizeof(cherokee_balancer_host_t *); size = DEFAULT_SOURCES_ALLOCATION * sizeof(cherokee_source_t *);
balancer->hosts = (cherokee_balancer_host_t **) malloc (size); balancer->sources = (cherokee_source_t **) malloc (size);
} else { } else {
size = (balancer->hosts_size + DEFAULT_HOSTS_ALLOCATION ) * sizeof(cherokee_balancer_host_t *); size = (balancer->sources_size + DEFAULT_SOURCES_ALLOCATION ) * sizeof(cherokee_source_t *);
balancer->hosts = (cherokee_balancer_host_t **) realloc (balancer->hosts, size); balancer->sources = (cherokee_source_t **) realloc (balancer->sources, size);
} }


if (balancer->hosts == NULL) if (balancer->sources == NULL)
return ret_nomem; return ret_nomem;


memset (balancer->hosts + balancer->hosts_len, 0, DEFAULT_HOSTS_ALLOCATION); memset (balancer->sources + balancer->sources_len, 0, DEFAULT_SOURCES_ALLOCATION);


balancer->hosts_size += DEFAULT_HOSTS_ALLOCATION; balancer->sources_size += DEFAULT_SOURCES_ALLOCATION;
return ret_ok; return ret_ok;
} }




ret_t ret_t
cherokee_balancer_add_host (cherokee_balancer_t *balancer, cherokee_balancer_host_t *host) cherokee_balancer_add_source (cherokee_balancer_t *balancer, cherokee_source_t *source)
{ {
ret_t ret; ret_t ret;

if (balancer->hosts_len >= balancer->hosts_size) { if (balancer->sources_len >= balancer->sources_size) {
ret = alloc_more_hosts (balancer); ret = alloc_more_sources (balancer);
if (ret != ret_ok) return ret; if (ret != ret_ok) return ret;
} }


balancer->hosts[balancer->hosts_len] = host; balancer->sources[balancer->sources_len] = source;
balancer->hosts_len++; balancer->sources_len++;


return ret_ok; return ret_ok;
} }




ret_t ret_t
cherokee_balancer_dispatch (cherokee_balancer_t *balancer, cherokee_connection_t *conn, cherokee_balancer_host_t **host) cherokee_balancer_dispatch (cherokee_balancer_t *balancer, cherokee_connection_t *conn, cherokee_source_t **source)
{ {
if (balancer->dispatch == NULL) if (balancer->dispatch == NULL)
return ret_error; return ret_error;


return balancer->dispatch (balancer, conn, host); return balancer->dispatch (balancer, conn, source);
} }




Expand Down Expand Up @@ -135,3 +188,27 @@ cherokee_balancer_free (cherokee_balancer_t *bal)
free (bal); free (bal);
return ret_ok; return ret_ok;
} }


ret_t
cherokee_balancer_instance (cherokee_buffer_t *name,
cherokee_config_node_t *conf,
cherokee_server_t *srv,
cherokee_balancer_t **balancer)
{
ret_t ret;
balancer_new_func_t new_func;
cherokee_module_info_t *info = NULL;

ret = cherokee_module_loader_get (&srv->loader, name->buf, &info);
if (ret != ret_ok) return ret;

new_func = (balancer_new_func_t) info->new_func;
ret = new_func (balancer);
if (ret != ret_ok) return ret;

ret = cherokee_balancer_configure (*balancer, conf);
if (ret != ret_ok) return ret;

return ret_ok;
}
53 changes: 25 additions & 28 deletions cherokee/balancer.h
Expand Up @@ -32,55 +32,52 @@
#include <cherokee/common.h> #include <cherokee/common.h>
#include <cherokee/module.h> #include <cherokee/module.h>
#include <cherokee/connection.h> #include <cherokee/connection.h>
#include <cherokee/source.h>


CHEROKEE_BEGIN_DECLS CHEROKEE_BEGIN_DECLS



typedef ret_t (* balancer_dispatch_func_t) (void *balancer, cherokee_connection_t *conn, cherokee_source_t **src);
/* Hosts
*/
typedef struct {
int foo;
} cherokee_balancer_host_t;


/* Handler properties
*/
typedef ret_t (* balancer_props_func_free_t) (void *balancerp);

typedef struct {
balancer_props_func_free_t free;
} cherokee_balancer_props_t;


/* Balancer
*/
typedef ret_t (* balancer_dispatch_func_t) (void *balancer, cherokee_connection_t *conn, cherokee_balancer_host_t **hosts);
typedef ret_t (* balancer_free_func_t) (void *balancer);


typedef struct { typedef struct {
cherokee_module_t module; cherokee_module_t module;


/* Properties */ /* Properties */
cherokee_balancer_host_t **hosts; cherokee_source_t **sources;
cuint_t hosts_len; cuint_t sources_len;
cuint_t hosts_size; cuint_t sources_size;


/* Virtual methods */ /* Virtual methods */
balancer_dispatch_func_t dispatch; balancer_dispatch_func_t dispatch;

} cherokee_balancer_t; } cherokee_balancer_t;


#define BAL(b) ((cherokee_balancer_t *)(b)) #define BAL(b) ((cherokee_balancer_t *)(b))



typedef ret_t (* balancer_new_func_t) (cherokee_balancer_t **balancer);
typedef ret_t (* balancer_free_func_t) (cherokee_balancer_t *balancer);


ret_t cherokee_balancer_init_base (cherokee_balancer_t *balancer); ret_t cherokee_balancer_init_base (cherokee_balancer_t *balancer);
ret_t cherokee_balancer_mrproper (cherokee_balancer_t *balancer); ret_t cherokee_balancer_mrproper (cherokee_balancer_t *balancer);
ret_t cherokee_balancer_configure (cherokee_balancer_t *balancer, cherokee_config_node_t *conf);


ret_t cherokee_balancer_add_host (cherokee_balancer_t *balancer, cherokee_balancer_host_t *host); /* Public methods
*/
ret_t cherokee_balancer_add_source (cherokee_balancer_t *balancer, cherokee_source_t *source);


/* Virtual methods /* Virtual methods
*/ */
ret_t cherokee_balancer_dispatch (cherokee_balancer_t *balancer, cherokee_connection_t *conn, cherokee_balancer_host_t **host); ret_t cherokee_balancer_dispatch (cherokee_balancer_t *balancer, cherokee_connection_t *conn, cherokee_source_t **source);
ret_t cherokee_balancer_free (cherokee_balancer_t *balancer); ret_t cherokee_balancer_free (cherokee_balancer_t *balancer);


/* Commodity
*/
ret_t cherokee_balancer_instance (cherokee_buffer_t *name,
cherokee_config_node_t *conf,
cherokee_server_t *srv,
cherokee_balancer_t **balancer);

CHEROKEE_END_DECLS CHEROKEE_END_DECLS


#endif /* CHEROKEE_BALANCER_H */ #endif /* CHEROKEE_BALANCER_H */
32 changes: 22 additions & 10 deletions cherokee/balancer_round_robin.c
Expand Up @@ -27,15 +27,26 @@
#include "balancer_round_robin.h" #include "balancer_round_robin.h"
#include "module_loader.h" #include "module_loader.h"


static ret_t
dispatch (cherokee_balancer_round_robin_t *balancer,
cherokee_connection_t *conn,
cherokee_source_t **src);



ret_t ret_t
cherokee_balancer_round_robin_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, void **props) cherokee_balancer_round_robin_configure (cherokee_balancer_t *balancer, cherokee_config_node_t *conf)
{ {
ret_t ret;

ret = cherokee_balancer_configure (BAL(balancer), conf);
if (ret != ret_ok) return ret;

return ret_ok; return ret_ok;
} }



ret_t ret_t
cherokee_balancer_round_robin_new (cherokee_balancer_t **bal, cherokee_connection_t *cnt, cherokee_balancer_props_t *props) cherokee_balancer_round_robin_new (cherokee_balancer_t **bal)
{ {
CHEROKEE_NEW_STRUCT (n, balancer_round_robin); CHEROKEE_NEW_STRUCT (n, balancer_round_robin);


Expand All @@ -44,7 +55,7 @@ cherokee_balancer_round_robin_new (cherokee_balancer_t **bal, cherokee_connectio
cherokee_balancer_init_base (BAL(n)); cherokee_balancer_init_base (BAL(n));


MODULE(n)->free = (module_func_free_t) cherokee_balancer_round_robin_free; MODULE(n)->free = (module_func_free_t) cherokee_balancer_round_robin_free;
BAL(n)->dispatch = (balancer_dispatch_func_t) cherokee_balancer_round_robin_dispatch; BAL(n)->dispatch = (balancer_dispatch_func_t) dispatch;


/* Init properties /* Init properties
*/ */
Expand All @@ -66,20 +77,20 @@ cherokee_balancer_round_robin_free (cherokee_balancer_round_robin_t *balancer)
} }




ret_t static ret_t
cherokee_balancer_round_robin_dispatch (cherokee_balancer_round_robin_t *balancer, dispatch (cherokee_balancer_round_robin_t *balancer,
cherokee_connection_t *conn, cherokee_connection_t *conn,
cherokee_balancer_host_t **host) cherokee_source_t **src)
{ {
cherokee_balancer_t *gbal = BAL(balancer); cherokee_balancer_t *gbal = BAL(balancer);


CHEROKEE_MUTEX_LOCK (&balancer->last_one_mutex); CHEROKEE_MUTEX_LOCK (&balancer->last_one_mutex);


if (gbal->hosts_len <= 0) if (gbal->sources_len <= 0)
goto error; goto error;


balancer->last_one = (balancer->last_one + 1) % gbal->hosts_len; balancer->last_one = (balancer->last_one + 1) % gbal->sources_len;
*host = gbal->hosts[balancer->last_one]; *src = gbal->sources[balancer->last_one];


CHEROKEE_MUTEX_UNLOCK (&balancer->last_one_mutex); CHEROKEE_MUTEX_UNLOCK (&balancer->last_one_mutex);
return ret_ok; return ret_ok;
Expand All @@ -90,6 +101,7 @@ cherokee_balancer_round_robin_dispatch (cherokee_balancer_round_robin_t *balance
} }





/* Module stuff /* Module stuff
*/ */


Expand Down
10 changes: 2 additions & 8 deletions cherokee/balancer_round_robin.h
Expand Up @@ -38,17 +38,11 @@ typedef struct {
#endif #endif
} cherokee_balancer_round_robin_t; } cherokee_balancer_round_robin_t;



#define PROP_RR(x) ((cherokee_balancer_round_robin_props_t *)(x))
#define BAL_RR(x) ((cherokee_balancer_round_robin_file_t *)(x)) #define BAL_RR(x) ((cherokee_balancer_round_robin_file_t *)(x))
#define BAL_RR_PROP(x) (PROP_RR(BAL(x)->props))



ret_t cherokee_balancer_round_robin_new (cherokee_balancer_t **hdl, cherokee_connection_t *cnt, cherokee_balancer_props_t *props);
ret_t cherokee_balancer_round_robin_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, void **props);


ret_t cherokee_balancer_round_robin_new (cherokee_balancer_t **balancer);
ret_t cherokee_balancer_round_robin_configure (cherokee_balancer_t *balancer, cherokee_config_node_t *conf);
ret_t cherokee_balancer_round_robin_free (cherokee_balancer_round_robin_t *balancer); ret_t cherokee_balancer_round_robin_free (cherokee_balancer_round_robin_t *balancer);
ret_t cherokee_balancer_round_robin_dispatch (cherokee_balancer_round_robin_t *balancer, cherokee_connection_t *conn, cherokee_balancer_host_t **host);



#endif /* CHEROKEE_BALANCER_ROUND_ROBIN_H */ #endif /* CHEROKEE_BALANCER_ROUND_ROBIN_H */

0 comments on commit 7b0e48a

Please sign in to comment.