From 6fd732492886181944075c6f05c912f6456aa071 Mon Sep 17 00:00:00 2001 From: Matthias Hanel Date: Wed, 5 Feb 2020 19:30:30 -0500 Subject: [PATCH] Fixing/adding samples, slight reword Signed-off-by: Matthias Hanel --- developing-with-nats/events/events.md | 19 ++++++++++++++++--- developing-with-nats/events/slow.md | 8 ++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/developing-with-nats/events/events.md b/developing-with-nats/events/events.md index 0ffc499..7de5352 100644 --- a/developing-with-nats/events/events.md +++ b/developing-with-nats/events/events.md @@ -11,6 +11,20 @@ Connection events may include the connection being closed, disconnected or recon ```go // There is not a single listener for connection events in the NATS Go Client. // Instead, you can set individual event handlers using: +nc, err := nats.Connect("demo.nats.io", + nats.DisconnectErrHandler(func(_ *nats.Conn, err error) { + log.Printf("client disconnected: %v", err) + }), + nats.ReconnectHandler(func(_ *nats.Conn) { + log.Printf("client reconnected") + }), + nats.ClosedHandler(func(_ *nats.Conn) { + log.Printf("client closed") + })) +if err != nil { + log.Fatal(err) +} +defer nc.Close() DisconnectHandler(cb ConnHandler) ReconnectHandler(cb ConnHandler) @@ -114,7 +128,7 @@ await nc.connect(**options) {% tab title="Ruby" %} ```ruby -r# There is not a single listener for connection events in the Ruby NATS Client. +# There is not a single listener for connection events in the Ruby NATS Client. # Instead, you can set individual event handlers using: NATS.on_disconnect do @@ -229,7 +243,7 @@ nc.on('serversDiscovered', (urls) => { {% tab title="Ruby" %} ```ruby -r# The Ruby NATS client does not support discovered servers handler right now +# The Ruby NATS client does not support discovered servers handler right now ``` {% endtab %} @@ -354,4 +368,3 @@ nc.on('error', (err) => { ``` {% endtab %} {% endtabs %} - diff --git a/developing-with-nats/events/slow.md b/developing-with-nats/events/slow.md index 2c87d48..6b327db 100644 --- a/developing-with-nats/events/slow.md +++ b/developing-with-nats/events/slow.md @@ -1,8 +1,8 @@ # Slow Consumers -NATS is designed to move messages through the server quickly. As a result, NATS depends on the applications to consider and respond to changing message rates. The server will do a bit of impedance matching, but if a client is too slow the server will eventually cut them off. These cut off connections are called _slow consumers_. +NATS is designed to move messages through the server quickly. As a result, NATS depends on the applications to consider and respond to changing message rates. The server will do a bit of impedance matching, but if a client is too slow the server will eventually cut them off by closing the connection. These cut off connections are called _slow consumers_. -One way some of the libraries deal with bursty message traffic is to cache incoming messages for a subscription. So if an application can handle 10 messages per second and sometimes receives 20 messages per second, the library may hold the extra 10 to give the application time to catch up. To the server, the application will appear to be handling the messages and consider the connection healthy. It is up to the client library to decide what to do when the cache is too big, but most client libraries will drop incoming messages. +One way some of the libraries deal with bursty message traffic is to buffer incoming messages for a subscription. So if an application can handle 10 messages per second and sometimes receives 20 messages per second, the library may hold the extra 10 to give the application time to catch up. To the server, the application will appear to be handling the messages and consider the connection healthy. It is up to the client library to decide what to do when the cache is too big, but most client libraries will drop incoming messages. Receiving and dropping messages from the server keeps the connection to the server healthy, but creates an application requirement. There are several common patterns: @@ -101,7 +101,7 @@ await nc.subscribe("updates", cb=cb, pending_bytes_limit=5*1024*1024, pending_ms {% tab title="Ruby" %} ```ruby -# The Ruby NATS client currently does not have option to customize slow consumer limits per sub. +# The Ruby NATS client currently does not have option to customize specify a subscribers pending limits. ``` {% endtab %} @@ -200,7 +200,7 @@ public class SlowConsumerListener { if len(msgs) == 3: # Head of line blocking on other messages caused - # by single message proccesing taking long... + # by single message proccesing taking long... await asyncio.sleep(1) await nc.subscribe("updates", cb=cb, pending_msgs_limit=5)