forked from pablin87/rtb_exchange_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadserver.py
128 lines (116 loc) · 4.05 KB
/
adserver.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pyev
import os
import sys
import logging
import socket
import signal
import errno
import random
import weakref
from settings import EVENT_ENDPOINT
from utils import EphemeralConnection
class AdServer(object):
def __init__(self, loop):
self.loop = loop
self.watchers = []
ep = EVENT_ENDPOINT.split(':')
self.endpoint = (ep[0], int(ep[1]))
self.timers = []
self.stats_timer = pyev.Timer(
3.0,
3.0,
self.loop,
self.print_stats)
self.stats_timer.start()
self.reqs = 0
self.resps = 0
self.no_resps = 0
self.errors = 0
self.conn_pool = [
EphemeralConnection(
self.loop, self.endpoint, '', self.recv_http,
self.on_error, self.no_response) for i in range(900)
]
self.conn_use = []
def stop(self):
self.stats_timer.stop()
for v in self.timers:
v.stop()
logging.debug("{0}: stopped".format(self))
def print_stats(self, watcher, revents):
logging.info(
'evs=%d reps=%d errs=%d noresps=%d, endpoint=%s:%d' %
(self.reqs, self.resps, self.errors, self.no_resps,
self.endpoint[0], self.endpoint[1]))
logging.info('idle_conns=%d used_conns=%d' % (len(self.conn_pool),len(self.conn_use)))
def send_event(self, buf, timeout):
# set the timeout to call the method that will
# create the connection and send the buffer (send buffer)
logging.debug('ad.send_event creating conn')
#conn = EphemeralConnection(
# self.loop, self.endpoint, buf, self.recv_http,
# self.on_error, self.no_response)
conn = None
try :
conn = self.conn_pool.pop()
self.conn_use.append(conn)
except :
logging.error('no more connections')
return
conn.buf = buf
conn.read_buf = ''
to = pyev.Timer(
timeout,
0.0,
self.loop,
self.send_http,
conn)
to.start()
self.timers.append(to)
logging.debug('ad.send_event creating conn done %d' % conn.id)
def send_http(self, watcher, revents):
# create the ad connection, set the buff
# and recv_http callback and the connect to send it
conn_id = watcher.data.id
logging.debug('ad.send_http %d' % conn_id)
try:
logging.debug('ad.send_http connecting')
watcher.data.connect()
logging.debug('ad.send_http connecting done')
self.reqs += 1
except KeyError :
logging.error('AdServer : no conn %d found' % conn_id)
except :
logging.error('AdServer : error sending http')
self.timers.remove(watcher)
logging.debug('ad.send_http done')
def recv_http(self, buf, conn):
# receive the response, maybe pint the status code,
# close the connection and erase it form the list
logging.debug('ad.receive_response conn %d : %s' % (conn.id, buf))
self.resps += 1
try:
conn.close()
self.conn_pool.append(conn)
self.conn_use.remove(conn)
except :
logging.error('recv_http')
return ''
def on_error(self, conn):
logging.error('ad.on_error : error recived %d' % conn.id)
self.errors += 1
try:
self.conn_pool.append(conn)
self.conn_use.remove(conn)
except :
pass
def no_response(self, watcher, revents):
conn = watcher.data
logging.error('ad.on_error : error no response recived %d' % conn.id)
self.no_resps += 1
try:
conn.close()
self.conn_pool.append(conn)
self.conn_use.remove(conn)
except :
logging.error('no_response')