Skip to content
Browse files

allows configuring total maximum number of sessions per server and pe…

…r-virtualhost
  • Loading branch information...
1 parent da65d34 commit e36a2f8a00b98f2b8db103790486174eed063328 @rdeioris rdeioris committed
Showing with 64 additions and 8 deletions.
  1. +10 −2 blastbeat.h
  2. +3 −0 blastbeat.ini
  3. +10 −0 src/config.c
  4. +31 −4 src/main.c
  5. +6 −0 src/sessions_ht.c
  6. +4 −2 src/zmq.c
View
12 blastbeat.h
@@ -124,6 +124,10 @@ struct bb_virtualhost {
uint32_t ght_size;
struct bb_group_entry *ght;
+
+ uint64_t max_sessions;
+ uint64_t active_sessions;
+
char *ssl_certificate;
char *ssl_key;
@@ -273,9 +277,12 @@ struct bb_session {
// contains the virtualhost mapped to the session
struct bb_virtualhost *vhost;
+ // used for monitoring inactivity
+ time_t last_seen;
+
// this is the death timer
struct bb_session_timer timer;
-
+
// persistent sessions can be re-called (useful for socket.io in xhr-polling)
int persistent;
// stealth sessions never touch dealers
@@ -367,13 +374,14 @@ struct blastbeat_server {
char *uid;
char *gid;
+ uint64_t max_sessions;
uint64_t active_sessions;
void *router;
int zmq_fd;
struct ev_loop *loop;
- int max_fd;
+ uint64_t max_fd;
int ssl_initialized;
char *ssl_certificate;
View
3 blastbeat.ini
@@ -12,6 +12,8 @@ uid = www-data
gid = 1000
max-hops = 17
+sessions = 100
+
daemon = 1
[blastbeat:quantal64.local:8443]
@@ -26,6 +28,7 @@ node = FOOBAR5
[blastbeat:quantal64.local:8080]
node = FOOBAR1
+sessions = 17
[blastbeat:192.168.173.5:8081]
certificate = quantal.crt
View
10 src/config.c
@@ -395,6 +395,11 @@ static void bb_main_config_add(char *key, char *value) {
return;
}
+ is_opt("sessions") {
+ blastbeat.max_sessions = strtoll(value, NULL, 10);
+ return;
+ }
+
}
static void bb_vhost_config_add(char *vhostname, char *key, char *value) {
@@ -422,6 +427,11 @@ static void bb_vhost_config_add(char *vhostname, char *key, char *value) {
return;
}
+ is_opt( "sessions") {
+ vhost->max_sessions = strtoll(value, NULL, 10);
+ return;
+ }
+
is_opt( "node") {
create_vhost_dealer(vhost, value);
return;
View
35 src/main.c
@@ -43,7 +43,6 @@ void bb_session_close(struct bb_session *bbs) {
// if linked to a dealer (and not in stealth mode), send a 'end' message
if (bbs->dealer && !bbs->stealth) {
- bbs->dealer->load--;
bb_zmq_send_msg(bbs->dealer->identity, bbs->dealer->len, (char *) &bbs->uuid_part1, BB_UUID_LEN, "end", 3, "", 0);
}
}
@@ -188,8 +187,16 @@ int bb_set_dealer(struct bb_session *bbs, char *name, size_t len) {
if (best_dealer) {
best_dealer->load++;
- bbs->dealer = best_dealer;
bbs->vhost = vhost->vhost;
+ // increase only if it is not a moving session
+ if (!bbs->dealer) {
+ if (bbs->vhost->max_sessions > 0 && bbs->vhost->active_sessions+1 > bbs->vhost->max_sessions) {
+ fprintf(stderr,"!!! maximum number of sessions (%llu) for virtualhost \"%.*s\" reached !!!\n", (unsigned long long) bbs->vhost->max_sessions, (int) bbs->vhost->len, bbs->vhost->name);
+ return -1;
+ }
+ bbs->vhost->active_sessions++;
+ }
+ bbs->dealer = best_dealer;
return 0;
}
return -1;
@@ -288,6 +295,10 @@ void bb_initialize_response(struct bb_session *bbs) {
// allocate a new session
struct bb_session *bb_session_new(struct bb_connection *bbc) {
+ if (blastbeat.active_sessions+1 > blastbeat.max_sessions) {
+ fprintf(stderr,"!!! maximum number of total sessions (%llu) reached !!!\n", (unsigned long long) blastbeat.max_sessions);
+ return NULL;
+ }
struct bb_session *bbs = malloc(sizeof(struct bb_session));
if (!bbs) {
bb_error("malloc()");
@@ -584,6 +595,7 @@ int main(int argc, char *argv[]) {
blastbeat.uid = "nobody";
blastbeat.gid = "nogroup";
blastbeat.max_hops = 10;
+ blastbeat.max_sessions = 10000;
bb_ini_config(argv[1]);
// validate config
@@ -600,12 +612,28 @@ int main(int argc, char *argv[]) {
// fix acceptors/vhosts
bb_acceptors_fix();
+ fprintf(stderr,"*** starting BlastBeat ***\n");
+
struct rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl)) {
- bb_error_exit("unable to get the maximum file descriptors number: getrlimit()");
+ bb_error("unable to get the maximum file descriptors number: getrlimit()");
}
blastbeat.max_fd = rl.rlim_cur;
+ if (blastbeat.max_fd < blastbeat.max_sessions*2) {
+ rl.rlim_cur = blastbeat.max_sessions*2;
+ rl.rlim_max = blastbeat.max_sessions*2;
+ if (setrlimit(RLIMIT_NOFILE, &rl)) {
+ bb_error("unable to set the maximum file descriptors number: setrlimit()");
+ fprintf(stderr,"lowering max sessions to %llu\n", (unsigned long long) blastbeat.max_fd/2);
+ blastbeat.max_sessions = blastbeat.max_fd/2;
+ }
+ else {
+ blastbeat.max_fd = rl.rlim_max;
+ }
+ }
+
+ fprintf(stderr, "allowed sessions: %llu\n", (unsigned long long) blastbeat.max_sessions);
blastbeat.sht = malloc(sizeof(struct bb_session_entry) * blastbeat.sht_size);
if (!blastbeat.sht) {
@@ -617,7 +645,6 @@ int main(int argc, char *argv[]) {
// report config, bind sockets and assign ssl keys/certificates
struct bb_acceptor *acceptor = blastbeat.acceptors;
- fprintf(stdout,"*** starting BlastBeat ***\n");
while(acceptor) {
fprintf(stdout, "\n[acceptor %s]\n", acceptor->name);
bb_acceptor_bind(acceptor);
View
6 src/sessions_ht.c
@@ -109,5 +109,11 @@ void bb_sht_remove(struct bb_session *bbs) {
bbs->next->prev = bbs->prev;
}
+ if (bbs->vhost) {
+ bbs->vhost->active_sessions--;
+ }
+ if (bbs->dealer) {
+ bbs->dealer->load--;
+ }
blastbeat.active_sessions--;
}
View
6 src/zmq.c
@@ -154,8 +154,10 @@ void bb_zmq_receiver(struct ev_loop *loop, struct ev_io *w, int revents) {
// dead/invalid session ?
struct bb_session *bbs = bb_sht_get(zmq_msg_data(&msg[1]));
if (!bbs) goto next;
-
- update_dealer(bbs->dealer, time(NULL));
+
+ time_t now = time(NULL);
+ bbs->last_seen = now;
+ update_dealer(bbs->dealer, now);
char *command = zmq_msg_data(&msg[2]);
size_t command_len = zmq_msg_size(&msg[2]);

0 comments on commit e36a2f8

Please sign in to comment.
Something went wrong with that request. Please try again.