/
asynredis.py
166 lines (136 loc) · 4.17 KB
/
asynredis.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from rs4 import asynchat
from . import dbconnect
import socket
DEBUG = True
LINE_FEED = b"\r\n"
class RedisError (Exception):
pass
class AsynConnect (dbconnect.AsynDBConnect, asynchat.async_chat):
def __init__ (self, address, params = None, lock = None, logger = None):
dbconnect.AsynDBConnect.__init__ (self, address, params, lock, logger)
asynchat.async_chat.__init__ (self)
def close (self, deactive = 1):
asynchat.async_chat.close (self)
# re-init asychat
self.ac_in_buffer = b''
self.incoming = []
self.producer_fifo.clear()
dbconnect.AsynDBConnect.close (self, deactive)
self.logger ("[info] ..dbo %s:%d has been closed" % self.address)
def handle_connect (self):
self.set_event_time ()
if self.user:
self.push_command ('AUTH', self.password)
def push_command (self, *args):
self.set_event_time ()
self.last_command = args [0].upper ()
command = pack_command (self.last_command, *args [1:])
if isinstance(command, list):
command = b"".join (command)
self.push (command)
def connect (self):
self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
try:
asynchat.async_chat.connect (self, self.address)
except:
self.handle_error ()
def collect_incoming_data (self, data):
self.set_event_time ()
self.data.append (data)
def fetchall (self):
try:
res = self.response [0][0]
except IndexError:
res = None
self.response = []
return res
def add_element (self, e):
if type (e) is bytes:
e = e.decode ("utf8")
self.response [-1].append (e)
self.num_elements [-1] -= 1
while self.num_elements and self.num_elements [-1] <= 0:
self.num_elements.pop (-1)
if len (self.response) > 1:
item = self.response.pop (-1)
self.response [-1].append (item)
def raise_error (self, e):
raise RedisError (e.decode ("utf8"))
def found_terminator (self):
if self.last_command == "AUTH":
if self.data [-1] != b"+OK":
self.raise_error (self.data [-1][1:])
if self.dbname:
return self.push_command ('SELECT', self.dbname)
if self.last_command == "SELECT" and self.data [-1] != b"+OK":
self.raise_error (self.data [-1][1:])
header = self.data [-1][:1]
if self.length != -1:
self.add_element (self.data [-1][:-2])
self.data = []
self.length = -1
self.set_terminator (LINE_FEED)
elif header in b"-":
self.raise_error (self.data [-1][1:])
elif header in b"+":
self.add_element (self.data [-1][1:])
self.has_result = True
self.set_terminator (LINE_FEED)
elif header in b":":
self.add_element ((int (self.data [-1][1:]),))
self.set_terminator (LINE_FEED)
elif header == b"$":
self.length = int (self.data [-1][1:]) + 2
if self.length == 1:
self.add_element (None)
self.data = []
self.set_terminator (LINE_FEED)
self.length = -1
else:
self.set_terminator (self.length)
elif header == b"*":
num_elements = int (self.data [-1][1:])
if self.num_elements [-1] == -1:
self.add_element (None)
else:
self.response.append ([])
self.num_elements.append (num_elements)
self.data = []
self.set_terminator (LINE_FEED)
else:
raise ValueError ("Protocol Error")
if not self.num_elements:
self.has_result = True
self.close_case_with_end_tran ()
def close_case (self):
if self.request:
self.request.handle_result (None, self.expt, self.fetchall ())
self.request = None
self.set_active (False)
def end_tran (self):
if not self.backend:
self.del_channel ()
def begin_tran (self, request):
dbconnect.AsynDBConnect.begin_tran (self, request)
self.response = [[]]
self.data = []
self.length = -1
self.num_elements = [0]
self.last_command = None
def execute (self, request):
self.begin_tran (request)
# SHOULD push before adding to map, otherwise raised threading collision
self.push_command (request.method, *request.params)
self.set_terminator (LINE_FEED)
if not self.connected:
self.connect ()
elif not self.backend:
self.add_channel ()
_AsynConnect = AsynConnect # for subclassing
try:
from redis import connection as redisconn
import redis.connection
pack_command = redisconn.Connection ().pack_command
except ImportError:
from ..uninstalled import Uninstalled
AsynConnect = Uninstalled ('redis')