mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
80 lines
2.1 KiB
Markdown
80 lines
2.1 KiB
Markdown
# Set the Number of Reconnect Attempts
|
|
|
|
Applications can set the maximum reconnect attempts per server. This includes server provided to the clients connect call, as well as server the client discovered through other server. Once re-connect to a server failed the specified amount of times in a row, it will be removed from the connect list. After a successful re-connect to a server, the client will reset this servers failed reconnect attempt count. If a server was removed from the connect list, it can be re-discovered on connect. This effectively resets the connect attempt count as well. If the client runs out of servers to re-connect, it will close the connection and [raise an error](events.md).
|
|
|
|
{% tabs %}
|
|
{% tab title="Go" %}
|
|
```go
|
|
// Set max reconnects attempts
|
|
nc, err := nats.Connect("demo.nats.io", nats.MaxReconnects(10))
|
|
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").
|
|
maxReconnects(10). // Set max reconnect attempts
|
|
build();
|
|
Connection nc = Nats.connect(options);
|
|
|
|
// Do something with the connection
|
|
|
|
nc.close();
|
|
```
|
|
{% endtab %}
|
|
|
|
{% tab title="JavaScript" %}
|
|
```javascript
|
|
let nc = NATS.connect({
|
|
maxReconnectAttempts: 10,
|
|
servers: ["nats://demo.nats.io:4222"]
|
|
});
|
|
```
|
|
{% endtab %}
|
|
|
|
{% tab title="Python" %}
|
|
```python
|
|
nc = NATS()
|
|
await nc.connect(
|
|
servers=["nats://demo.nats.io:4222"],
|
|
max_reconnect_attempts=10,
|
|
)
|
|
|
|
# Do something with the connection
|
|
|
|
await nc.close()
|
|
```
|
|
{% endtab %}
|
|
|
|
{% tab title="Ruby" %}
|
|
```ruby
|
|
require 'nats/client'
|
|
|
|
NATS.start(servers: ["nats://127.0.0.1:1222", "nats://127.0.0.1:1223", "nats://127.0.0.1:1224"], max_reconnect_attempts: 10) do |nc|
|
|
# Do something with the connection
|
|
|
|
# Close the connection
|
|
nc.close
|
|
end
|
|
```
|
|
{% endtab %}
|
|
|
|
{% tab title="TypeScript" %}
|
|
```typescript
|
|
// will throw an exception if connection fails
|
|
let nc = await connect({
|
|
maxReconnectAttempts: 10,
|
|
});
|
|
nc.close();
|
|
```
|
|
{% endtab %}
|
|
{% endtabs %}
|
|
|