2
2
from . import consts
3
3
import time
4
4
from .log import logger
5
- from .utils import retry_iterator
5
+ from .utils import retry_iterator , RdyControl
6
6
from .connection import create_connection
7
- from .consts import TOUCH , REQ , FIN , RDY , CLS , MPUB , PUB , SUB , AUTH
7
+ from .consts import TOUCH , REQ , FIN , RDY , CLS , MPUB , PUB , SUB , AUTH , DPUB
8
8
9
9
10
10
async def create_nsq (host = '127.0.0.1' , port = 4150 , loop = None , queue = None ,
11
11
heartbeat_interval = 30000 , feature_negotiation = True ,
12
12
tls_v1 = False , snappy = False , deflate = False , deflate_level = 6 ,
13
- sample_rate = 0 ):
13
+ consumer = False , sample_rate = 0 ):
14
+ """"
15
+ param: host: host addr with no protocol. 127.0.0.1
16
+ param: port: host port
17
+ param: queue: queue where all the msg been put from the nsq
18
+ param: heartbeat_interval: heartbeat interval with nsq, set -1 to disable nsq heartbeat check
19
+ params: snappy: snappy compress
20
+ params: deflate: deflate compress can't set True both with snappy
21
+ """
14
22
# TODO: add parameters type and value validation
23
+ loop = loop or asyncio .get_event_loop ()
15
24
queue = queue or asyncio .Queue (loop = loop )
16
25
conn = Nsq (host = host , port = port , queue = queue ,
17
26
heartbeat_interval = heartbeat_interval ,
18
27
feature_negotiation = feature_negotiation ,
19
28
tls_v1 = tls_v1 , snappy = snappy , deflate = deflate ,
20
29
deflate_level = deflate_level ,
21
- sample_rate = sample_rate , loop = loop )
30
+ sample_rate = sample_rate , consumer = consumer , loop = loop )
22
31
await conn .connect ()
23
32
return conn
24
33
@@ -28,7 +37,7 @@ class Nsq:
28
37
def __init__ (self , host = '127.0.0.1' , port = 4150 , loop = None , queue = None ,
29
38
heartbeat_interval = 30000 , feature_negotiation = True ,
30
39
tls_v1 = False , snappy = False , deflate = False , deflate_level = 6 ,
31
- sample_rate = 0 ):
40
+ sample_rate = 0 , consumer = False , max_in_flight = 42 ):
32
41
# TODO: add parameters type and value validation
33
42
self ._config = {
34
43
"deflate" : deflate ,
@@ -53,6 +62,13 @@ def __init__(self, host='127.0.0.1', port=4150, loop=None, queue=None,
53
62
54
63
self ._on_rdy_changed_cb = None
55
64
self ._last_rdy = 0
65
+ self .consumer = consumer
66
+ if self .consumer :
67
+ self ._idle_timeout = 10
68
+ self ._max_in_flight = max_in_flight
69
+ self ._rdy_control = RdyControl (idle_timeout = self ._idle_timeout ,
70
+ max_in_flight = self ._max_in_flight ,
71
+ loop = self ._loop )
56
72
57
73
async def connect (self ):
58
74
self ._conn = await create_connection (self ._host , self ._port ,
@@ -61,6 +77,8 @@ async def connect(self):
61
77
self ._conn ._on_message = self ._on_message
62
78
await self ._conn .identify (** self ._config )
63
79
self ._status = consts .CONNECTED
80
+ if self .consumer :
81
+ self ._rdy_control .add_connection (self ._conn )
64
82
65
83
def _on_message (self , msg ):
66
84
# should not be coroutine
@@ -102,7 +120,7 @@ async def reconnect(self):
102
120
await asyncio .sleep (t , loop = self ._loop )
103
121
104
122
async def execute (self , command , * args , data = None ):
105
- if self ._state <= consts .CONNECTED and self ._reconnect :
123
+ if self ._status <= consts .CONNECTED and self ._reconnect :
106
124
await self .reconnect ()
107
125
108
126
response = self ._conn .execute (command , * args , data = data )
@@ -113,8 +131,10 @@ def id(self):
113
131
return self ._conn .endpoint
114
132
115
133
def wait_messages (self ):
134
+ # print('wait_messages')
116
135
while True :
117
136
future = asyncio .ensure_future (self ._queue .get (), loop = self ._loop )
137
+ # print(future)
118
138
yield future
119
139
120
140
async def auth (self , secret ):
@@ -123,7 +143,7 @@ async def auth(self, secret):
123
143
:param secret:
124
144
:return:
125
145
"""
126
- return ( await self ._conn .execute (AUTH , data = secret ) )
146
+ return await self ._conn .execute (AUTH , data = secret )
127
147
128
148
async def sub (self , topic , channel ):
129
149
"""
@@ -132,7 +152,7 @@ async def sub(self, topic, channel):
132
152
:param channel:
133
153
:return:
134
154
"""
135
- return ( await self ._conn .execute (SUB , topic , channel ) )
155
+ return await self ._conn .execute (SUB , topic , channel )
136
156
137
157
async def pub (self , topic , message ):
138
158
"""
@@ -141,7 +161,19 @@ async def pub(self, topic, message):
141
161
:param message:
142
162
:return:
143
163
"""
144
- return (await self ._conn .execute (PUB , topic , data = message ))
164
+ return await self ._conn .execute (PUB , topic , data = message )
165
+
166
+ async def dpub (self , topic , delay_time , message ):
167
+ """
168
+
169
+ :param topic:
170
+ :param message:
171
+ :param delay_time: delayed time in millisecond
172
+ :return:
173
+ """
174
+ if not delay_time or delay_time is None :
175
+ delay_time = 0
176
+ return await self ._conn .execute (DPUB , topic , delay_time , data = message )
145
177
146
178
async def mpub (self , topic , message , * messages ):
147
179
"""
@@ -152,7 +184,7 @@ async def mpub(self, topic, message, *messages):
152
184
:return:
153
185
"""
154
186
msgs = [message ] + list (messages )
155
- return ( await self ._conn .execute (MPUB , topic , data = msgs ) )
187
+ return await self ._conn .execute (MPUB , topic , data = msgs )
156
188
157
189
async def rdy (self , count ):
158
190
"""
@@ -165,15 +197,15 @@ async def rdy(self, count):
165
197
166
198
self ._last_rdy = count
167
199
self .rdy_state = count
168
- return ( await self ._conn .execute (RDY , count ) )
200
+ return await self ._conn .execute (RDY , count )
169
201
170
202
async def fin (self , message_id ):
171
203
"""
172
204
173
205
:param message_id:
174
206
:return:
175
207
"""
176
- return ( await self ._conn .execute (FIN , message_id ) )
208
+ return await self ._conn .execute (FIN , message_id )
177
209
178
210
async def req (self , message_id , timeout ):
179
211
"""
@@ -182,15 +214,15 @@ async def req(self, message_id, timeout):
182
214
:param timeout:
183
215
:return:
184
216
"""
185
- return ( await self ._conn .execute (REQ , message_id , timeout ) )
217
+ return await self ._conn .execute (REQ , message_id , timeout )
186
218
187
219
async def touch (self , message_id ):
188
220
"""
189
221
190
222
:param message_id:
191
223
:return:
192
224
"""
193
- return ( await self ._conn .execute (TOUCH , message_id ) )
225
+ return await self ._conn .execute (TOUCH , message_id )
194
226
195
227
async def cls (self ):
196
228
"""
0 commit comments