Open rduque1 opened 2 years ago
I'm seeing the same issue with version 2.2.0
. I'm running using the NATS Docker image 2.9.10
for my server. I ran @rduque1's code and also my own adaptation of the demo code. I tried tweaking the consumer settings to delay redelivery beyond what my random
sleeps, but I still see several redeliveries.
import asyncio
from datetime import datetime
import nats
from nats.js.api import ConsumerConfig, RetentionPolicy
from random import random
async def main():
async def closed_cb():
print("Connection closed")
nc = await nats.connect("localhost", closed_cb=closed_cb)
# Create JetStream context.
js = nc.jetstream()
await js.add_stream(name="work-stream", subjects=["logs"], retention=RetentionPolicy.WORK_QUEUE)
for i in range(0, 100):
ack = await js.publish("logs", f"hello world: {i}".encode())
print(f"{datetime.now()} {ack}")
# Create deliver group that will be have load balanced messages.
async def qsub_a(msg):
print(f"QSUB A {msg.metadata.timestamp}: {msg.data}")
await msg.in_progress()
await asyncio.sleep(.5 + random())
# await asyncio.sleep(random())
await msg.ack_sync()
if msg.metadata.num_delivered > 1:
print(f"Delivered {msg.metadata.num_delivered} times")
async def qsub_b(msg):
print(f"QSUB B {msg.metadata.timestamp}: {msg.data}")
await msg.in_progress()
await asyncio.sleep(2)
# await asyncio.sleep(random())
await msg.ack_sync()
if msg.metadata.num_delivered > 1:
print(f"Delivered {msg.metadata.num_delivered} times")
config_a = ConsumerConfig(ack_wait=120, flow_control=True, max_ack_pending=1)
config_b = ConsumerConfig(ack_wait=120, flow_control=True, max_ack_pending=1)
await js.subscribe("logs", "workers", cb=qsub_b, manual_ack=True, config=config_b)
await js.subscribe("logs", "workers", cb=qsub_a, manual_ack=True, config=config_a)
try:
while(True):
await asyncio.sleep(2)
except KeyboardInterrupt:
pass
finally:
await nc.close()
if __name__ == '__main__':
asyncio.run(main())
I actually tried this again, yesterday, and it worked as expected. I didn't realize that the server's deliver group's configuration wasn't updated as I changed settings. Since I started a fresh server, it got the settings above and the ack_wait
setting was used.
Hi I am using
nats-py==2.1.7
and when I am trying to make a load balancing with the retention policyRetentionPolicy.WORK_QUEUE
the msgs are still delivered to both workers.I don't understand what I am doing wrong, but I can't make it work so the msgs are only delivered to one subscription. The messages are deleted from the stream, but still both workers get them. I tried with a callback set and without, but it didn't change anything.
The code is to test it is here below.