--- /data/projets/rel/haproxy/haproxy-1.1.27/haproxy.c Mon Oct 27 16:00:28 2003 +++ haproxy.c-sebastien-brize Tue Mar 16 11:56:20 2004 @@ -94,6 +94,7 @@ #define DEF_CHKINTR 2000 #define DEF_FALLTIME 3 #define DEF_RISETIME 2 +#define DEF_WEIGHT 1 //SB #define DEF_CHECK_REQ "OPTIONS / HTTP/1.0\r\n\r\n" /* default connections limit */ @@ -354,6 +355,8 @@ int health; /* 0->rise-1 = bad; rise->rise+fall-1 = good */ int rise, fall; /* time in iterations */ int inter; /* time in milliseconds */ + int weight; /* initial weight in kilos SB */ + int fat; /* weight the server loses SB */ int result; /* 0 = connect OK, -1 = connect KO */ int curfd; /* file desc used for current test, or -1 if not in test */ struct proxy *proxy; /* the proxy this server belongs to */ @@ -435,6 +438,9 @@ int conn_retries; /* maximum number of connect retries */ int options; /* PR_O_REDISP, PR_O_TRANSP */ int mode; /* mode = PR_MODE_TCP, PR_MODE_HTTP or PR_MODE_HEALTH */ + int wactive; /* is weighted mode active SB */ + int weight; /* overall proxy's server weight SB */ + int fat; /* weight the proxy looses SB */ struct sockaddr_in source_addr; /* the address to which we want to bind for connect() */ struct proxy *next; struct sockaddr_in logsrv1, logsrv2; /* 2 syslog servers */ @@ -1424,14 +1430,37 @@ if (srv == NULL) srv = px->srv; if (srv->state & SRV_RUNNING - && !((srv->state & SRV_BACKUP) && ignore_backup)) - return srv; + && !((srv->state & SRV_BACKUP) && ignore_backup) && srv->fat > 0) //SB + { + if(srv->fat > 0 && px->wactive) { //SB + srv->fat--; //SB + px->fat--; //SB + return srv; + } //SB + else if(!px->wactive) return srv; //SB + } srv = srv->next; } while (srv != px->cursrv); } while (ignore_backup--); return NULL; } +void reinit_weights(struct proxy *px) { //SB + struct server *srv = px->cursrv; //SB + int ignore_backup = 1; //SB + do { //SB + if (srv == NULL) //SB + srv = px->srv; //SB + if (srv->state & SRV_RUNNING && !((srv->state & SRV_BACKUP) && ignore_backup)) //SB + { //SB + srv->fat = srv->weight; //SB + } //SB + srv = srv->next; //SB + } while (srv != px->cursrv); //SB + px->fat = px->weight; //SB +} //SB + + /* * This function initiates a connection to the current server (s->srv) if (s->direct) * is set, or to the dispatch server if (s->direct) is 0. It returns 0 if @@ -1449,9 +1478,9 @@ else if (s->proxy->options & PR_O_BALANCE) { if (s->proxy->options & PR_O_BALANCE_RR) { struct server *srv; - + if(!s->proxy->fat && s->proxy->wactive) //SB + reinit_weights(s->proxy); //SB srv = find_server(s->proxy); - if (srv == NULL) /* no server left */ return -1; @@ -4491,6 +4520,9 @@ curproxy->contimeout = defproxy.contimeout; curproxy->srvtimeout = defproxy.srvtimeout; curproxy->mode = defproxy.mode; + curproxy->wactive = 0; //SB + curproxy->weight = 0; //SB + curproxy->fat = 0; //SB curproxy->logfac1 = defproxy.logfac1; curproxy->logsrv1 = defproxy.logsrv1; curproxy->loglev1 = defproxy.loglev1; @@ -4846,6 +4878,8 @@ newsrv->inter = DEF_CHKINTR; newsrv->rise = DEF_RISETIME; newsrv->fall = DEF_FALLTIME; + newsrv->weight = DEF_WEIGHT; //SB + newsrv->fat = DEF_WEIGHT; //SB newsrv->health = newsrv->rise; /* up, but will fall down at first failure */ cur_arg = 3; while (*args[cur_arg]) { @@ -4867,6 +4901,14 @@ newsrv->inter = atol(args[cur_arg + 1]); cur_arg += 2; } + else if (!strcmp(args[cur_arg], "weight")) { //SB + newsrv->weight = atol(args[cur_arg + 1]); //SB + newsrv->fat = newsrv->weight; //SB + newsrv->proxy->wactive = 1; //SB + newsrv->proxy->weight += newsrv->weight; //SB + newsrv->proxy->fat += newsrv->weight; //SB + cur_arg += 2; //SB + } //SB else if (!strcmp(args[cur_arg], "port")) { newsrv->check_port = atol(args[cur_arg + 1]); cur_arg += 2; @@ -4880,7 +4922,7 @@ cur_arg += 1; } else { - Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'rise' and 'fall'.\n", + Alert("parsing [%s:%d] : server %s only supports options 'backup', 'cookie', 'check', 'inter', 'weight', 'rise' and 'fall'.\n", //SB file, linenum, newsrv->id); return -1; }