| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|---|
| 2 |
|
|---|
| 3 |
/* Cherokee |
|---|
| 4 |
* |
|---|
| 5 |
* Authors: |
|---|
| 6 |
* Alvaro Lopez Ortega <alvaro@alobbs.com> |
|---|
| 7 |
* |
|---|
| 8 |
* Copyright (C) 2001-2008 Alvaro Lopez Ortega |
|---|
| 9 |
* |
|---|
| 10 |
* This program is free software; you can redistribute it and/or |
|---|
| 11 |
* modify it under the terms of version 2 of the GNU General Public |
|---|
| 12 |
* License as published by the Free Software Foundation. |
|---|
| 13 |
* |
|---|
| 14 |
* This program is distributed in the hope that it will be useful, |
|---|
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 |
* GNU General Public License for more details. |
|---|
| 18 |
* |
|---|
| 19 |
* You should have received a copy of the GNU General Public License |
|---|
| 20 |
* along with this program; if not, write to the Free Software |
|---|
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 22 |
* USA |
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
#include "common-internal.h" |
|---|
| 26 |
|
|---|
| 27 |
#include "balancer_round_robin.h" |
|---|
| 28 |
#include "plugin_loader.h" |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
/* Plug-in initialization |
|---|
| 32 |
*/ |
|---|
| 33 |
PLUGIN_INFO_BALANCER_EASIEST_INIT (round_robin); |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
static ret_t |
|---|
| 37 |
dispatch (cherokee_balancer_round_robin_t *balancer, |
|---|
| 38 |
cherokee_connection_t *conn, |
|---|
| 39 |
cherokee_source_t **src); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
ret_t |
|---|
| 43 |
cherokee_balancer_round_robin_configure (cherokee_balancer_t *balancer, cherokee_config_node_t *conf) |
|---|
| 44 |
{ |
|---|
| 45 |
ret_t ret; |
|---|
| 46 |
|
|---|
| 47 |
ret = cherokee_balancer_configure (BAL(balancer), conf); |
|---|
| 48 |
if (ret != ret_ok) return ret; |
|---|
| 49 |
|
|---|
| 50 |
return ret_ok; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
ret_t |
|---|
| 55 |
cherokee_balancer_round_robin_new (cherokee_balancer_t **bal) |
|---|
| 56 |
{ |
|---|
| 57 |
CHEROKEE_NEW_STRUCT (n, balancer_round_robin); |
|---|
| 58 |
|
|---|
| 59 |
/* Init |
|---|
| 60 |
*/ |
|---|
| 61 |
cherokee_balancer_init_base (BAL(n), PLUGIN_INFO_PTR(round_robin)); |
|---|
| 62 |
|
|---|
| 63 |
MODULE(n)->free = (module_func_free_t) cherokee_balancer_round_robin_free; |
|---|
| 64 |
BAL(n)->dispatch = (balancer_dispatch_func_t) dispatch; |
|---|
| 65 |
|
|---|
| 66 |
/* Init properties |
|---|
| 67 |
*/ |
|---|
| 68 |
n->last_one = 0; |
|---|
| 69 |
CHEROKEE_MUTEX_INIT (&n->last_one_mutex, NULL); |
|---|
| 70 |
|
|---|
| 71 |
/* Return obj |
|---|
| 72 |
*/ |
|---|
| 73 |
*bal = BAL(n); |
|---|
| 74 |
return ret_ok; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
ret_t |
|---|
| 79 |
cherokee_balancer_round_robin_free (cherokee_balancer_round_robin_t *balancer) |
|---|
| 80 |
{ |
|---|
| 81 |
CHEROKEE_MUTEX_DESTROY (&balancer->last_one_mutex); |
|---|
| 82 |
return ret_ok; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
static ret_t |
|---|
| 87 |
dispatch (cherokee_balancer_round_robin_t *balancer, |
|---|
| 88 |
cherokee_connection_t *conn, |
|---|
| 89 |
cherokee_source_t **src) |
|---|
| 90 |
{ |
|---|
| 91 |
cherokee_balancer_t *gbal = BAL(balancer); |
|---|
| 92 |
|
|---|
| 93 |
UNUSED(conn); |
|---|
| 94 |
CHEROKEE_MUTEX_LOCK (&balancer->last_one_mutex); |
|---|
| 95 |
|
|---|
| 96 |
if (gbal->sources_len <= 0) |
|---|
| 97 |
goto error; |
|---|
| 98 |
|
|---|
| 99 |
balancer->last_one = (balancer->last_one + 1) % gbal->sources_len; |
|---|
| 100 |
*src = gbal->sources[balancer->last_one]; |
|---|
| 101 |
|
|---|
| 102 |
CHEROKEE_MUTEX_UNLOCK (&balancer->last_one_mutex); |
|---|
| 103 |
return ret_ok; |
|---|
| 104 |
|
|---|
| 105 |
error: |
|---|
| 106 |
CHEROKEE_MUTEX_UNLOCK (&balancer->last_one_mutex); |
|---|
| 107 |
return ret_error; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|