Skip to content

Commit 8db4971

Browse files
lsmcpq
lsm
authored andcommitted
Fix choose_backend() logic
1 parent 0f2006f commit 8db4971

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

apps/load_balancer/Makefile

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1+
# To build with SSL under windows, do:
2+
# wine make load_balancer.exe SSL=openssl # OpenSSL build
3+
# wine make load_balancer.exe SSL=krypton # Krypton build
4+
15
PROG = load_balancer
26
SOURCES = $(PROG).c ../../fossa.c
37
CFLAGS = -W -Wall -pthread $(CFLAGS_EXTRA)
48

9+
ifeq ($(SSL), openssl)
10+
OPENSSL_PATH = ./openssl-0.9.8
11+
CFLAGS_EXTRA += -DNS_ENABLE_SSL -I$(OPENSSL_PATH)/include
12+
CFLAGS_EXTRA += /link /libpath:$(OPENSSL_PATH)/lib ssleay32.lib libeay32.lib
13+
endif
14+
15+
ifeq ($(SSL), krypton)
16+
KRYPTON_PATH = ../../../krypton
17+
CFLAGS_EXTRA += -DNS_ENABLE_SSL $(KRYPTON_PATH)/krypton.c -I$(KRYPTON_PATH)
18+
endif
19+
520
all: $(PROG)
621

722
$(PROG): $(SOURCES)
823
$(CC) $(SOURCES) -o $@ $(CFLAGS)
924

1025
$(PROG).exe: $(SOURCES)
11-
cl $(SOURCES) /I.. /MD /Fe$@
26+
cl $(SOURCES) /I.. /MD /Fe$@ /DNS_ENABLE_THREADS advapi32.lib $(CFLAGS_EXTRA)
1227

1328
test: $(PROG)
1429
$(MAKE) -C ../api_server

apps/load_balancer/load_balancer.c

+21-10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ static int matches_vhost(const struct ns_str *host, const char *vhost) {
5252
return host->len == vhost_len && memcmp(host->p, vhost, vhost_len) == 0;
5353
}
5454

55+
static void write_log(const char *fmt, ...) {
56+
va_list ap;
57+
if (s_log_file != NULL) {
58+
va_start(ap, fmt);
59+
vfprintf(s_log_file, fmt, ap);
60+
fflush(s_log_file);
61+
va_end(ap);
62+
}
63+
}
64+
5565
/*
5666
* choose_backend parses incoming HTTP request and routes it to the appropriate
5767
* backend. It assumes that clients don't do HTTP pipelining, handling only
@@ -88,18 +98,17 @@ static void choose_backend(struct ns_connection *nc) {
8898
/* Prefer most specific URI prefixes */
8999
strlen(s_http_backends[i].uri_prefix) >
90100
strlen(s_http_backends[chosen].uri_prefix) ||
91-
/* Prefer least used backends */
92-
s_http_backends[i].usage_counter <
93-
s_http_backends[chosen].usage_counter)) {
101+
/* Among prefixes of the same length chose the least used. */
102+
(strlen(s_http_backends[i].uri_prefix) ==
103+
strlen(s_http_backends[chosen].uri_prefix) &&
104+
s_http_backends[i].usage_counter <
105+
s_http_backends[chosen].usage_counter))) {
94106
chosen = i;
95107
}
96108
}
97109

98-
if (s_log_file != NULL) {
99-
fprintf(s_log_file, "%.*s %.*s backend=%d\n", (int) hm.method.len,
100-
hm.method.p, (int) hm.uri.len, hm.uri.p, chosen);
101-
fflush(s_log_file);
102-
}
110+
write_log("%.*s %.*s backend=%d\n", (int) hm.method.len, hm.method.p,
111+
(int) hm.uri.len, hm.uri.p, chosen);
103112

104113
if (chosen == -1) {
105114
/* No backend with given uri_prefix found, bail out */
@@ -113,6 +122,8 @@ static void choose_backend(struct ns_connection *nc) {
113122
nc->mgr, s_http_backends[chosen].host_port, ev_handler)) ==
114123
NULL) {
115124
/* Connection to backend failed */
125+
write_log("Connection to [%s] failed\n",
126+
s_http_backends[chosen].host_port);
116127
ns_printf(nc, "%s%s%s\r\n", s_error_500, s_content_len_0,
117128
s_connection_close);
118129
} else {
@@ -206,8 +217,8 @@ static void ev_handler(struct ns_connection *nc, int ev, void *ev_data) {
206217
case NS_CONNECT:
207218
if (*(int *) ev_data != 0) {
208219
/* TODO(lsm): mark backend as defunct, try it later on */
209-
fprintf(stderr, "connect(%s) failed\n",
210-
s_http_backends[(int) nc->user_data].host_port);
220+
write_log("connect(%s) failed\n",
221+
s_http_backends[(int) nc->user_data].host_port);
211222
ns_printf(nc->proto_data, "%s%s%s\r\n", s_error_500, s_content_len_0,
212223
s_connection_close);
213224
}

platforms/arduino_wifi_CC3000/avrsupport.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ void blink(int times, int ms) {
2323
}
2424

2525
extern unsigned int __heap_start;
26-
extern void *__brkval;
26+
extern void* __brkval;
2727

2828
struct __freelist {
2929
size_t sz;
30-
struct __freelist *nx;
30+
struct __freelist* nx;
3131
};
3232

33-
extern struct __freelist *__flp;
33+
extern struct __freelist* __flp;
3434

3535
int get_freelistsize() {
36-
struct __freelist *current;
36+
struct __freelist* current;
3737
int total = 0;
3838
for (current = __flp; current; current = current->nx) {
3939
total += 2;

platforms/arduino_wifi_CC3000/avrsupport.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ int get_freememsize();
4444
#endif
4545

4646
#endif /* NS_AVRDEBUG_HEADER_INCLUDED */
47-
/*
48-
* Copyright (c) 2015 Cesanta Software Limited
49-
* All rights reserved
50-
*/
47+
/*
48+
* Copyright (c) 2015 Cesanta Software Limited
49+
* All rights reserved
50+
*/
5151

5252
#ifndef AVRLIBC_COMPAT_HEADER_INCLUDED
5353
#define AVRLIBC_COMPAT_HEADER_INCLUDED
@@ -107,10 +107,12 @@ time_t time(time_t* timer);
107107
#endif
108108

109109
#endif
110+
110111
/*
111112
* Copyright (c) 2015 Cesanta Software Limited
112113
* All rights reserved
113114
*/
115+
114116
#ifndef CC3000UTILS_HEADER_INCLUDED
115117
#define CC3000UTILS_HEADER_INCLUDED
116118

0 commit comments

Comments
 (0)