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.
// Set max reconnects attemptsnc, err := nats.Connect("demo.nats.io", nats.MaxReconnects(10))if err !=nil { log.Fatal(err)}defer nc.Close()// Do something with the connection
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 connectionnc.close();
nc =NATS()await nc.connect( servers=["nats://demo.nats.io:4222"], max_reconnect_attempts=10, )# Do something with the connectionawait nc.close()
// dotnet add package NATS.NetusingNATS.Net;usingNATS.Client.Core;awaitusingvar client =newNatsClient(newNatsOpts{ Url ="nats://demo.nats.io:4222", // Set the maximum number of reconnect attempts MaxReconnectRetry =10,});
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.closeend
natsConnection *conn =NULL;natsOptions *opts =NULL;natsStatus s = NATS_OK;s =natsOptions_Create(&opts);if (s == NATS_OK) s =natsOptions_SetMaxReconnect(opts,10);if (s == NATS_OK) s =natsConnection_Connect(&conn, opts);(...)// Destroy objects that were creatednatsConnection_Destroy(conn);natsOptions_Destroy(opts);