-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo.py
117 lines (90 loc) · 2.52 KB
/
fifo.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
import threading
from collections import deque
class await_fifo:
# this class has purpose handling a streaming like response
# as single response object not as multiple bytes stream
# it is questionable about efficience
# but it makes possible optimization response contents - compressing, http2 framing etc - delivery but just bytes relaying
def __init__ (self):
self.l = deque ()
self.has_None = False
def working (self):
return (self.has_None or self.l) and 1 or 0
def __len__ (self):
if not self.l:
if self.has_None:
# for return None
self.l.append (None)
self.has_None = False
return 1
return 0
for i in range (len (self.l)):
try:
readyfunc = getattr (self.l [0], 'ready')
except AttributeError:
return 1
if readyfunc ():
return 1
self.l.rotate (-1)
return 0
def __getitem__(self, index):
return self.l [index]
def __setitem__(self, index, item):
self.l [index] = item
def __delitem__ (self, index):
try:
del self.l [index]
except IndexError:
pass
def append (self, item):
self.insert (-1, item)
def appendleft (self, item):
self.insert (0, item)
def insert (self, index, item):
if item is None:
self.has_None = True
return
if self.has_None and index != 0:
return # deny adding
if not self.l:
return self.l.append (item)
if index == -1:
return self.l.append (item)
try:
readyfunc = getattr (item, 'ready')
except AttributeError:
pass
else:
if not readyfunc ():
return self.l.append (item)
return self.l.appendleft (item)
def clear (self):
self.l.clear ()
self.has_None = False
class await_ts_fifo (await_fifo):
# HTTP/1.x needn't this class, because one channel handles only one request
# this will be used for handling multiple requests like HTTP/2
def __init__ (self):
await_fifo.__init__ (self)
self._lock = threading.Lock ()
def working (self):
with self._lock:
return await_fifo.working (self)
def __len__ (self):
with self._lock:
return await_fifo.__len__ (self)
def __getitem__(self, index):
with self._lock:
return await_fifo.__getitem__ (self, index)
def __setitem__(self, index, item):
with self._lock:
await_fifo.__setitem__ (self, index, item)
def __delitem__ (self, index):
with self._lock:
await_fifo.__delitem__ (self, index)
def clear (self):
with self._lock:
await_fifo.clear (self)
def insert (self, index, item):
with self._lock:
await_fifo.insert (self, index, item)