X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?p=living-lab-site.git;a=blobdiff_plain;f=cis%2Fcis_lb%2Fload_balancer%2Fbase.py;fp=cis%2Fcis_lb%2Fload_balancer%2Fbase.py;h=f4e9261f1baddddf307e4ef1cc26bfa128d9a3e6;hp=9586c88dcd9c14ffae157b5bd762897f4db3f797;hb=141bdb84bf143c29a6c50eef3aee18541d2fc0e4;hpb=66dc2204a919e2c29b2aa9330ea18026d58540d9 diff --git a/cis/cis_lb/load_balancer/base.py b/cis/cis_lb/load_balancer/base.py index 9586c88..f4e9261 100644 --- a/cis/cis_lb/load_balancer/base.py +++ b/cis/cis_lb/load_balancer/base.py @@ -1,6 +1,7 @@ import threading import urllib +import config import logger class LoadBalancer(threading.Thread): @@ -19,16 +20,29 @@ class LoadBalancer(threading.Thread): while True: (request, data) = self.queue.get() + urls = config.CIS_URLS[:] - cis = self.choose() - logger.log_msg('Forwarding to %s' % cis, logger.LOG_LEVEL_DEBUG) - urllib.urlopen(cis + request, data) + while len(urls) != 0: + cis = self.choose(urls) + + # Request is forwarded to the chosen CIS. + try: + urllib.urlopen(cis + request, data) + except IOError: + logger.log_msg('Failed to forward request to %s' % cis, \ + logger.LOG_LEVEL_ERROR) + continue + + logger.log_msg('Request forwarded to %s' % cis, \ + logger.LOG_LEVEL_INFO) + break self.queue.task_done() - def choose(self): + def choose(self, urls): """ Implement load balancing policy in this method for child classes which - choses a CIS from config.CIS_URLS . + choses a CIS from urls parameter. The chosen URL should be deleted from + urls list. """ pass \ No newline at end of file