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