Powered By GitBook
Set the Number of Reconnect Attempts
Applications can set the maximum reconnect attempts per server. This includes the server provided to the client's connect call, as well as the server the client discovered through another server. Once reconnect to a server fails the specified amount of times in a row, it will be removed from the connect list. After a successful reconnect to a server, the client will reset that server's failed reconnect attempt count. If a server was removed from the connect list, it can be rediscovered on connect. This effectively resets the connect attempt count as well. If the client runs out of servers to reconnect, it will close the connection and raise an error.
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
// Set max reconnects attempts
2
nc, err := nats.Connect("demo.nats.io", nats.MaxReconnects(10))
3
if err != nil {
4
log.Fatal(err)
5
}
6
defer nc.Close()
7
8
// Do something with the connection
Copied!
1
Options options = new Options.Builder().
2
server("nats://demo.nats.io:4222").
3
maxReconnects(10). // Set max reconnect attempts
4
build();
5
Connection nc = Nats.connect(options);
6
7
// Do something with the connection
8
9
nc.close();
Copied!
1
let nc = NATS.connect({
2
maxReconnectAttempts: 10,
3
servers: ["nats://demo.nats.io:4222"]
4
});
Copied!
1
nc = NATS()
2
await nc.connect(
3
servers=["nats://demo.nats.io:4222"],
4
max_reconnect_attempts=10,
5
)
6
7
# Do something with the connection
8
9
await nc.close()
Copied!
1
require 'nats/client'
2
3
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|
4
# Do something with the connection
5
6
# Close the connection
7
nc.close
8
end
Copied!
1
// will throw an exception if connection fails
2
let nc = await connect({
3
maxReconnectAttempts: 10,
4
});
5
nc.close();
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
s = natsOptions_SetMaxReconnect(opts, 10);
8
if (s == NATS_OK)
9
s = natsConnection_Connect(&conn, opts);
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
15
natsOptions_Destroy(opts);
Copied!
Last modified 10mo ago
Export as PDF
Copy link