-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer.c
298 lines (279 loc) · 8.98 KB
/
balancer.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* $Id: balancer.c,v 1.15 2008/04/22 16:29:36 aaron Exp $ */
/* Copyright 2006-2007 Codemass, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file balancer.c
* @brief The Balancer is responsible for managing and establishing
* connections to all URL destinations.
* @author Aaron Bannert (aaron@codemass.com)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include "params.h"
#include "balancer.h"
static struct location *locations;
static int n_locations = 0;
static int count_locations(struct urls *urls)
{
int i = 0;
struct urls *p = urls;
while (p) {
i++;
if (p->next == urls)
break;
p = p->next;
}
return i;
}
static size_t resource_length(struct location *location)
{
size_t rlen = 0;
struct uri *uri = location->uri;
rlen += uri->path ? strlen(uri->path) : sizeof("/") - 1;
rlen += uri->query ? strlen(uri->query) + 1 : 0;
/* FIXME: do we add fragments to client requests? */
rlen += uri->fragment ? strlen(uri->fragment) + 1 : 0;
return rlen;
}
static size_t request_length(struct location *location)
{
size_t rlen = 0;
struct headers *header = config_opts.headers;
rlen += sizeof("GET ") - 1;
rlen += resource_length(location);
rlen += sizeof(" HTTP/1.1\r\n") - 1;
while (1) {
if (header->value) {
rlen += strlen(header->header);
rlen += sizeof(": ") - 1;
rlen += strlen(header->value);
rlen += sizeof("\r\n") - 1;
}
if (header->next == config_opts.headers)
break;
header = header->next;
}
rlen += sizeof("Host: ") - 1;
rlen += strlen(location->uri->hostname);
rlen += sizeof("\r\n\r\n") - 1;
return rlen;
}
static size_t write_resource(struct uri *uri, char *p)
{
char *q = p;
if (config_opts.verbose > 5) {
fprintf(stderr, "uri->path = '%s'\n", uri->path ? uri->path : "NULL");
fprintf(stderr, "uri->query = '%s'\n", uri->query ? uri->query : "NULL");
fprintf(stderr, "uri->fragment = '%s'\n", uri->fragment ? uri->fragment : "NULL");
}
if (uri->path)
p += sprintf(p, "GET %s", uri->path);
else
p += sprintf(p, "GET /");
if (uri->query)
p += sprintf(p, "?%s", uri->query);
if (uri->fragment)
p += sprintf(p, "#%s", uri->fragment);
p += sprintf(p, " HTTP/1.1\r\n");
return p - q;
}
static void create_request(struct location *location)
{
struct headers *header = config_opts.headers;
char *p;
location->rlen = request_length(location);
p = malloc(location->rlen + 1);
memset(p, 0, location->rlen + 1);
location->request = p;
p += write_resource(location->uri, p);
while (1) {
if (header->value)
p += sprintf(p, "%s: %s\r\n", header->header, header->value);
if (header->next == config_opts.headers)
break;
header = header->next;
}
p += sprintf(p, "Host: %s\r\n\r\n", location->uri->hostname);
}
static void set_locations(struct urls *urls)
{
int i;
for (i = 0; i < n_locations; i++) {
const char *host;
unsigned short port;
struct hostent *hostent;
locations[i].uristr = urls->url;
locations[i].uri = parse_uri(urls->url);
if (locations[i].uri == NULL) {
fprintf(stderr, "error parsing URI: %s, failing\n", urls->url);
exit(-4);
}
host = locations[i].uri->hostname;
port = htons(locations[i].uri->port);
if (config_opts.connect) {
host = config_opts.connect;
if (config_opts.connect_port) {
port = htons(config_opts.connect_port);
}
}
hostent = gethostbyname(host);
if (hostent == NULL) {
fprintf(stderr, "gethostbyname error resolving %s: %s\n",
host, hstrerror(h_errno));
exit(-4);
}
if (hostent->h_addrtype == AF_INET) {
struct sockaddr_in *sa = malloc(sizeof(*sa));
locations[i].name = (struct sockaddr *)sa;
locations[i].namelen = sizeof(*sa);
sa->sin_family = hostent->h_addrtype;
sa->sin_port = port;
memcpy(&(sa->sin_addr.s_addr), hostent->h_addr_list[0],
hostent->h_length);
} else if (hostent->h_addrtype == AF_INET6) {
fprintf(stderr, "ipv6 not yet supported\n");
exit(-3);
} else {
fprintf(stderr, "unknown address type\n");
exit(-3);
}
create_request(&locations[i]);
urls = urls->next;
}
for (i = 0; i < n_locations; i++) {
int rv = start_accumulator(&locations[i].accumulator);
if (rv < 0) {
perror("start_accumulator (gettimeofday()) from set_locations");
exit(-3);
}
}
}
static int max_connects_per_location;
void initialize_balancer()
{
n_locations = count_locations(config_opts.urls);
locations = malloc(sizeof(struct location) * n_locations);
memset(locations, 0, sizeof(struct location) * n_locations);
set_locations(config_opts.urls);
max_connects_per_location = config_opts.count / n_locations;
}
static int current_location_rr = -1;
struct location *get_next_location_rr()
{
/* Round-Robin */
if (++current_location_rr >= n_locations)
current_location_rr = 0;
return &locations[current_location_rr];
}
struct location *get_next_location_fair()
{
int i;
struct location *lowconn_loc = NULL;
/* FIXME: improve the efficiency of this for larger numbers of locations
* by using a heap */
for (i = 0; i < n_locations; i++) {
struct location *testloc = &locations[i];
if (testloc->n_connects > max_connects_per_location)
continue;
else if (lowconn_loc == NULL)
lowconn_loc = testloc;
else if (testloc->n_concurrent < lowconn_loc->n_concurrent)
lowconn_loc = testloc;
}
if (lowconn_loc == NULL) {
return NULL; /* no more locations left */
} else {
lowconn_loc->n_concurrent++;
return lowconn_loc;
}
}
struct location *get_next_location()
{
/* FIXME: allow user to chose between Round-Robin and Fair */
return get_next_location_fair();
}
int location_connect(struct location *location, int sock)
{
int e;
int rc;
if (config_opts.verbose > 4)
fprintf(stderr, "location_connect(location '%s', sock %d)\n",
location->uristr, sock);
retry_connect:
if (location->n_errors >= config_opts.max_connect_errors) {
fprintf(stderr, "Exceeded maximum connect errors for host: %s:%d\n",
location->uri->hostname,
location->uri->port);
return -1;
}
if (config_opts.verbose > 3)
fprintf(stderr, "connect()ing to socket %d\n", sock);
rc = connect(sock, location->name, location->namelen);
e = errno;
if (rc < 0) { /* failed connect */
switch (e) {
case EINTR:
goto retry_connect;
case ECONNREFUSED:
case ENETUNREACH:
if (config_opts.verbose > 2)
perror("connect");
location->n_errors++;
goto retry_connect;
case EAGAIN: // local port exhaustion on Linux
case EADDRNOTAVAIL: // local port exhaustion on Solaris
case EINPROGRESS: // non-blocking socket still connecting (good error)
default:
break;
};
}
location->n_connects++;
return rc;
}
int location_close(struct location *location, int sock)
{
int rc;
rc = close(sock);
if (rc == 0) { /* if successful close(), decrease concurrency count */
location->n_concurrent--;
if (location->n_connects > max_connects_per_location) {
(void)stop_accumulator(&location->accumulator);
}
}
return rc;
}
int balancer_display(FILE *stream)
{
int i, ret = 0;
for (i = 0; i < n_locations; i++)
(void)stop_accumulator(&locations[i].accumulator);
for (i = 0; i < n_locations; i++) {
ret += fprintf(stream, "Statistics for URL %d: %s\n", i + 1, locations[i].uristr);
if (n_locations == 1)
return ret;
ret += print_accumulator(stream, &locations[i].accumulator);
ret += fprintf(stream, "\n");
}
return ret;
}