-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.py
66 lines (48 loc) · 2.18 KB
/
resolver.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
66
import time
from sqlalchemy import *
import pylru
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass
logging.getLogger(__name__).addHandler(NullHandler())
class Resolver:
def __init__(self, config):
self.logger = logging.getLogger()
self.address = config['db.url']
self.uniq_host = config['hostname-base']
self.engine = create_engine(self.address, pool_recycle=3600, pool_size=10)
cache_size = config.get('cache-size', 100)
self.cache = pylru.lrucache(cache_size)
#add auto reconnect
self.connection = self.engine.connect()
metadata = MetaData(bind=self.connection)
self.m_table = Table('measurement', metadata, autoload=True)
def resolve(self, hostname):
if hostname in self.cache:
measurement = self.cache[hostname]
else:
results = self.connection.execute(self.m_table.select().where(self.m_table.c.hostname==hostname))
measurement_row = results.first() #implicit close
measurement = dict(measurement_row) #copy the row data to cache
dests = measurement['destination_list']
dest_list = dests.split(',')
measurement['dest_list'] = dest_list
self.cache[hostname] = measurement
timestamp = int(time.time())
dest_list = measurement['dest_list']
if measurement['measurement_start_time'] == None:
self.logger.info("First request for %s" % hostname)
update = self.connection.execute(self.m_table.update().where(self.m_table.c.id==measurement['id']), {"measurement_start_time": timestamp})
#TODO what to do here if this fails??
return dest_list[0]
else:
current_time = timestamp
first_time = measurement['measurement_start_time']
interval = measurement['measurement_interval']
difference = current_time - first_time
slot = (difference/interval) % len(dest_list)
return dest_list[slot]