mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
Reworded to describe behaviour of non java clients and merge examples.
Specifically removed the reference to flush which is not influenced by this setting. Merged the examples as both values together make more sense. Signed-off-by: Matthias Hanel <mh@synadia.com>
This commit is contained in:
parent
92e62d1282
commit
bf9b8a8247
@ -1,18 +1,16 @@
|
|||||||
# Ping/Pong Protocol
|
# Ping/Pong Protocol
|
||||||
|
|
||||||
The client and server use a simple PING/PONG protocol to check that they are both still connected. The client will ping the server on a regular, configured interval so that the server usually doesn't have to initiate the PING/PONG interaction.
|
The client and server use a simple PING/PONG protocol to check that either of them are still connected to the other. On a regular interval he client will ping the server, which responds with a pong. Once a configurable maximum of outstanding pings without a single pong reply is hit, the connection is closed as stale. Together these two values define a timeout for the connection. In the pressence of traffic, such as messages or client side pings, the server will not initiate the PING/PONG interaction.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Set the Ping Interval
|
If you have a connection that is going to be open a long time with few messages traveling on it, setting the PING interval and/or limit how many are outstanding, can control how quickly the client will be notified of a problem. However on connections with a lot of traffic, the client will often figure out there is a problem between PINGS, and as a result the default PING interval is often on the order of minutes. To set the interval to 20s and limit outstanding pings to 5, thus force a closed connection after 100s of inactivity:
|
||||||
|
|
||||||
If you have a connection that is going to be open a long time with few messages traveling on it, setting this PING interval can control how quickly the client will be notified of a problem. However on connections with a lot of traffic, the client will often figure out there is a problem between PINGS, and as a result the default PING interval is often on the order of minutes. To set the interval to 20s:
|
|
||||||
|
|
||||||
{% tabs %}
|
{% tabs %}
|
||||||
{% tab title="Go" %}
|
{% tab title="Go" %}
|
||||||
```go
|
```go
|
||||||
// Set Ping Interval to 20 seconds
|
// Set Ping Interval to 20 seconds
|
||||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API Ping Example"), nats.PingInterval(20*time.Second))
|
nc, err := nats.Connect("demo.nats.io", nats.Name("API Ping Example"), nats.PingInterval(20*time.Second), nats.MaxPingsOutstanding(5))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -27,93 +25,6 @@ defer nc.Close()
|
|||||||
Options options = new Options.Builder().
|
Options options = new Options.Builder().
|
||||||
server("nats://demo.nats.io:4222").
|
server("nats://demo.nats.io:4222").
|
||||||
pingInterval(Duration.ofSeconds(20)). // Set Ping Interval
|
pingInterval(Duration.ofSeconds(20)). // Set Ping Interval
|
||||||
build();
|
|
||||||
Connection nc = Nats.connect(options);
|
|
||||||
|
|
||||||
// Do something with the connection
|
|
||||||
|
|
||||||
nc.close();
|
|
||||||
```
|
|
||||||
{% endtab %}
|
|
||||||
|
|
||||||
{% tab title="JavaScript" %}
|
|
||||||
```javascript
|
|
||||||
let nc = NATS.connect({
|
|
||||||
pingInterval: 20*1000, //20s
|
|
||||||
url: "nats://demo.nats.io:4222"
|
|
||||||
});
|
|
||||||
```
|
|
||||||
{% endtab %}
|
|
||||||
|
|
||||||
{% tab title="Python" %}
|
|
||||||
```python
|
|
||||||
nc = NATS()
|
|
||||||
|
|
||||||
await nc.connect(
|
|
||||||
servers=["nats://demo.nats.io:4222"],
|
|
||||||
# Set Ping Interval to 20 seconds
|
|
||||||
ping_interval=20,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Do something with the connection.
|
|
||||||
```
|
|
||||||
{% endtab %}
|
|
||||||
|
|
||||||
{% tab title="Ruby" %}
|
|
||||||
```ruby
|
|
||||||
require 'nats/client'
|
|
||||||
|
|
||||||
NATS.start(ping_interval: 20) do |nc|
|
|
||||||
nc.on_reconnect do
|
|
||||||
puts "Got reconnected to #{nc.connected_server}"
|
|
||||||
end
|
|
||||||
|
|
||||||
nc.on_disconnect do |reason|
|
|
||||||
puts "Got disconnected! #{reason}"
|
|
||||||
end
|
|
||||||
|
|
||||||
# Do something with the connection
|
|
||||||
end
|
|
||||||
```
|
|
||||||
{% endtab %}
|
|
||||||
|
|
||||||
{% tab title="TypeScript" %}
|
|
||||||
```typescript
|
|
||||||
// will throw an exception if connection fails
|
|
||||||
let nc = await connect({
|
|
||||||
pingInterval: 20*1000, //20s
|
|
||||||
url: "nats://demo.nats.io:4222"
|
|
||||||
});
|
|
||||||
nc.close();
|
|
||||||
```
|
|
||||||
{% endtab %}
|
|
||||||
{% endtabs %}
|
|
||||||
|
|
||||||
## Limit Outgoing Pings
|
|
||||||
|
|
||||||
The PING/PONG interaction is also used by most of the clients as a way to flush the connection to the server. Clients that cache outgoing messages provide a flush call that will run a PING/PONG. The flush will wait for the PONG to return, telling it that all cached messages have been processed, including the PING. The number of cached PING requests can be limited in most clients to insure that traffic problems are identified early. This configuration for _max outgoing pings_ or similar will usually default to a small number and should only be increased if you are worried about fast flush traffic, perhaps in multiple threads.
|
|
||||||
|
|
||||||
For example, to set the maximum number of outgoing pings to 5:
|
|
||||||
|
|
||||||
{% tabs %}
|
|
||||||
{% tab title="Go" %}
|
|
||||||
```go
|
|
||||||
// Set maximum number of PINGs out without getting a PONG back
|
|
||||||
// before the connection will be disconnected as a stale connection.
|
|
||||||
nc, err := nats.Connect("demo.nats.io", nats.Name("API MaxPing Example"), nats.MaxPingsOutstanding(5))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer nc.Close()
|
|
||||||
|
|
||||||
// Do something with the connection
|
|
||||||
```
|
|
||||||
{% endtab %}
|
|
||||||
|
|
||||||
{% tab title="Java" %}
|
|
||||||
```java
|
|
||||||
Options options = new Options.Builder().
|
|
||||||
server("nats://demo.nats.io:4222").
|
|
||||||
maxPingsOut(5). // Set max pings in flight
|
maxPingsOut(5). // Set max pings in flight
|
||||||
build();
|
build();
|
||||||
Connection nc = Nats.connect(options);
|
Connection nc = Nats.connect(options);
|
||||||
@ -127,6 +38,7 @@ nc.close();
|
|||||||
{% tab title="JavaScript" %}
|
{% tab title="JavaScript" %}
|
||||||
```javascript
|
```javascript
|
||||||
let nc = NATS.connect({
|
let nc = NATS.connect({
|
||||||
|
pingInterval: 20*1000, //20s
|
||||||
maxPingOut: 5,
|
maxPingOut: 5,
|
||||||
url: "nats://demo.nats.io:4222"
|
url: "nats://demo.nats.io:4222"
|
||||||
});
|
});
|
||||||
@ -139,10 +51,9 @@ nc = NATS()
|
|||||||
|
|
||||||
await nc.connect(
|
await nc.connect(
|
||||||
servers=["nats://demo.nats.io:4222"],
|
servers=["nats://demo.nats.io:4222"],
|
||||||
# Set maximum number of PINGs out without getting a PONG back
|
# Set Ping Interval to 20 seconds
|
||||||
# before the connection will be disconnected as a stale connection.
|
ping_interval=20,
|
||||||
max_outstanding_pings=5,
|
max_outstanding_pings=5,
|
||||||
ping_interval=1,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Do something with the connection.
|
# Do something with the connection.
|
||||||
@ -153,7 +64,7 @@ await nc.connect(
|
|||||||
```ruby
|
```ruby
|
||||||
require 'nats/client'
|
require 'nats/client'
|
||||||
|
|
||||||
NATS.start(max_outstanding_pings: 5) do |nc|
|
NATS.start(ping_interval: 20, max_outstanding_pings: 5) do |nc|
|
||||||
nc.on_reconnect do
|
nc.on_reconnect do
|
||||||
puts "Got reconnected to #{nc.connected_server}"
|
puts "Got reconnected to #{nc.connected_server}"
|
||||||
end
|
end
|
||||||
@ -171,6 +82,7 @@ end
|
|||||||
```typescript
|
```typescript
|
||||||
// will throw an exception if connection fails
|
// will throw an exception if connection fails
|
||||||
let nc = await connect({
|
let nc = await connect({
|
||||||
|
pingInterval: 20*1000, //20s
|
||||||
maxPingOut: 5,
|
maxPingOut: 5,
|
||||||
url: "nats://demo.nats.io:4222"
|
url: "nats://demo.nats.io:4222"
|
||||||
});
|
});
|
||||||
@ -178,4 +90,3 @@ nc.close();
|
|||||||
```
|
```
|
||||||
{% endtab %}
|
{% endtab %}
|
||||||
{% endtabs %}
|
{% endtabs %}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user