9 class LoadBalancer(threading.Thread):
11 def __init__(self, id, queue):
13 Initialize Load Balancer,
16 threading.Thread.__init__(self, \
17 name = '%s%02d' % (self.__class__.__name__, id))
24 (request, data) = self.queue.get()
25 urls = config.CIS_URLS[:]
26 code = json.loads(data)['code']
29 cis = self.choose(urls)
31 # Request is forwarded to the chosen CIS.
33 urllib.urlopen(cis + request, data)
35 logger.log_msg('#%s: Failed to forward request to %s' \
37 logger.LOG_LEVEL_ERROR)
40 logger.log_msg('#%s: Request forwarded to %s' \
42 logger.LOG_LEVEL_INFO)
46 logger.log_msg('#%s: Failed to forward request to any CIS' \
48 logger.LOG_LEVEL_FATAL)
49 self.notify_error(code)
51 self.queue.task_done()
53 def notify_error(self, code):
54 logger.log_msg('#%s: notifying web server about the error...'\
57 if config.WS_ERROR[len(config.WS_ERROR) - 1] == '/':
58 url = config.WS_ERROR + code
60 url = config.WS_ERROR + '/' + code
61 url = url + '/' + 'unreachable'
63 f = urllib.urlopen(url)
66 def choose(self, urls):
68 Implement load balancing policy in this method for child classes which
69 choses a CIS from urls parameter. The chosen URL should be deleted from