This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
cbRedirector.py
65 lines (53 loc) · 1.56 KB
/
cbRedirector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import sys
from pycb.cbException import cbException
import pycb
import stat
import urllib
import glob
import errno
import logging
import threading
import tempfile
import hashlib
import traceback
import time
import pycb
class cbRedirectorIface(object):
# return new host direction or None
def new_connection(self, request):
return None
# called when a connection is closed
def end_connection(self, request):
pass
class cbBasicRedirector(object):
def __init__(self, parser):
self.max = max_connections
self.connection_count = 0
self.host_file = parser.get("load_balanced", "hostfile")
self.max = int(parser.get("load_balanced", "max"))
def new_connection(self, request):
h = None
self.connection_count = self.connection_count + 1
if g_connection_count >= self.max:
h = self.get_next_host()
return h
def end_connection(self, request):
self.connection_count = self.connection_count - 1
def get_next_host():
try:
hosts = []
f = open(self.host_file, "r")
for l in f.readlines():
hosts.append(l.strip())
f.close()
my_host = "%s:%d" % (pycb.config.hostname, pycb.config.port)
for i in range(0, 10):
ndx = random.randint(0, len(hosts)-1)
h = hosts[ndx]
if h != my_host:
return h
return h
except Exception, ex:
log(logging.ERROR, "get next host error %s" % (str(ex)))
return None