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']
30 cis = self.choose(urls)
32 # Request is forwarded to the chosen CIS.
34 urllib.urlopen(cis + request, data)
36 logger.log_msg('#%s: Failed to forward request to %s' \
38 logger.LOG_LEVEL_ERROR)
42 logger.log_msg('#%s: Request forwarded to %s' \
44 logger.LOG_LEVEL_INFO)
47 if len(urls) == 0 and not success:
48 logger.log_msg('#%s: Failed to forward request to any CIS' \
50 logger.LOG_LEVEL_FATAL)
51 self.notify_error(code)
53 self.queue.task_done()
55 def notify_error(self, code):
56 logger.log_msg('#%s: notifying web server about the error...'\
59 if config.WS_ERROR[len(config.WS_ERROR) - 1] == '/':
60 url = config.WS_ERROR + code
62 url = config.WS_ERROR + '/' + code
63 url = url + '/' + 'unreachable'
65 f = urllib.urlopen(url)
68 def choose(self, urls):
70 Implement load balancing policy in this method for child classes which
71 choses a CIS from urls parameter. The chosen URL should be deleted from