With this change, you could pass URL "host" and it will translate
to "nats://host:4222". Also, if attempting to connect to a server
that requires TLS, the Secure option will be automatically turned
on and an attempt to make a secure connection will be made.
Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
When a connection is closed very quickly after being created, there
was a race condition that could lead to the connection object still
being referenced (and therefore holding resources) until the first
ping interval elapsed.
This was because the pinger was created in spinGoRoutines() which
was itself created as a go routine. When the Close() call was made,
the ping timer may still be nil (therefore no way to stop it), but
then created in the go routine (which caused the reference to the
connection object). By default, it means that the connection object
would be held for 30 seconds.
Doing connect/close in a tight loop would show the process size
growing.
This PR refactors where we create the pinger and the use of the
wait group. There had been a change in a past to attempt to fix
a WaitGroup panic. I believe this approach is more stable. We do
create the pinger and bump the wait group in processConnectInit,
under connection lock. The doReconnect() go routine waits for
this wait group on entry and then on iterations where a failure
occurs after we know we have started the go routines.
In case of failures in the reconect loop, some state has also been
properly reset (namely the connection's buffered writer).
Resolves#368
Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This is a follow up on #365. Working on another issue and running
tests, I still had a race on the original ach or a panic on send
to close channel.
Move from a channel to a linked list with dedicated lock.
Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
Doing a new release by pushing a tag will allow a test to run and
verify that the library version matches the given tag.
```
git tag -a vX.Y.Z -m "Release vX.Y.Z"
git push --tags
```
If the test fails, then we would delete the newly created tag,
fix the version string and recreate the tag.
The server pool was only growing when new servers were discovered.
Now, the client library updates its server pool based on server's
INFO protocols (true for server 1.0.6+).
The DiscoveredServersCB is still invoked only when new servers
are added (as in never seen as opposed to added back after leaving
the cluster).
The code should work ok with older servers but will take advantage
of changes in the server (https://github.com/nats-io/gnatsd/pull/626)
Once we introduce config reload support for users, it's possible for a
user to be disconnected due to a change in credentials. Before the user
is disconnected, the server sends an authorization error. We should
allow the user to handle this error in the async error callback.
When connecting to a server, the received INFO may contain an array
of URLs. The current code was adding those and then shuffeling the
entire pool (if NoRandomize is false). This is wrong since the
connect (and reconnect) loop would then potentially skip never
tried servers and try again servers that had just failed.
The tool is started with the "-ignore" flag to ignore warning SA2002
which corresponds to invoking t.Fatalf (and the like) in a go routine.
Calling t.Fatalf in a go routine may produce a race condition.
The rationale for ignoring this warning is that if a test executes
the t.Fatalf() line it is that we have a problem either with test
or code that should be fixed.
The recent change in processing of INFO protocol changed that
behavior by shuffling the entire pool. Now ensure that if Opts.Url
is set, it is first in the array.
This would be used in conjunction with server's PR #314.
The client may receive INFO protocols with a field containing
a possible array of URLs that correspond to server addresses in
the full mesh cluster that clients can connect to.
The server pool is updated with these URLs.
3 new Options and 2 new Options setters have been introduced to
manage username/password and/or tokens when dealing with these
bare URLs.
cc /derekcollison
- In sendConnect(), checks if it is an -ERR, if so, normalize error, otherwise return protocol error.
- Change ErrAuthorization text to end with 'violation' to match server
- Add test for normalizeErr
- Trim quotes and convert to lower case before processing error
- Fix parse of error test to just check parser state
- Add two tests to check for stale connection and other error
- Check expected authentication failure error message on connect
- If nc.Flush[Timeout]() was waiting for a response and the connection would close, nc.Flush() would return without error. This is not appropriate. It will now return ErrConnectionClosed.
- Adding/Updating several tests for more code coverage.
The case of the error message was changed to all lower case in the
client. The server is using mixed case. This would cause the check to
fail, resulting in connection close, instead of possibly trying to
reconnect.
Note: it seems that the parser code coverage in the client is not that
great. Since the code is common in part with the server, we know that
this code is tested, but wonder if we need to duplicate tests existing
in the server.
Note: it seems that the parser code coverage in the client is not that
great. Since the code is common in part with the server, we know that
this code is tested, but wonder if we need to duplicate tests existing
in the server.