[IMPROVED] Added close reason in the connection close log statement

This gives the close reason directly in the log without having to
get that information from the monitoring endpoint. Here is an
example of a route closed due to the remote side not replying to
PINGs:

```
[INF] 127.0.0.1:53839 - rid:2 - Router connection closed: Stale Connection
```

Without this change, the log statement would have been:
```
[INF] 127.0.0.1:53839 - rid:2 - Router connection closed
```

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
This commit is contained in:
Ivan Kozlovic
2020-04-15 14:12:31 -06:00
parent f7db844c79
commit be00ea96cf
2 changed files with 56 additions and 7 deletions

View File

@@ -1196,6 +1196,14 @@ func (c *client) markConnAsClosed(reason ClosedState, skipFlush bool) bool {
if skipFlush {
c.flags.set(skipFlushOnClose)
}
// Be consistent with the creation: for routes and gateways,
// we use Noticef on create, so use that too for delete.
if c.kind == ROUTER || c.kind == GATEWAY {
c.Noticef("%s connection closed: %v", c.typeString(), reason.String())
} else { // Client and Leaf Node connections.
c.Debugf("%s connection closed: %v", c.typeString(), reason.String())
}
// Save off the connection if its a client or leafnode.
if c.kind == CLIENT || c.kind == LEAF {
if nc := c.nc; nc != nil && c.srv != nil {
@@ -3517,13 +3525,6 @@ func (c *client) closeConnection(reason ClosedState) {
// been started.
func (c *client) teardownConn() {
c.mu.Lock()
// Be consistent with the creation: for routes and gateways,
// we use Noticef on create, so use that too for delete.
if c.kind == ROUTER || c.kind == GATEWAY {
c.Noticef("%s connection closed", c.typeString())
} else { // Client and Leaf Node connections.
c.Debugf("%s connection closed", c.typeString())
}
c.clearAuthTimer()
c.clearPingTimer()

View File

@@ -1990,3 +1990,51 @@ func TestFlushOutboundNoSliceReuseIfPartial(t *testing.T) {
t.Fatalf("Expected\n%q\ngot\n%q", expected.String(), fakeConn.buf.String())
}
}
type captureNoticeLogger struct {
DummyLogger
notices []string
}
func (l *captureNoticeLogger) Noticef(format string, v ...interface{}) {
l.Lock()
l.notices = append(l.notices, fmt.Sprintf(format, v...))
l.Unlock()
}
func TestCloseConnectionLogsReason(t *testing.T) {
o1 := DefaultOptions()
s1 := RunServer(o1)
defer s1.Shutdown()
l := &captureNoticeLogger{}
s1.SetLogger(l, true, true)
o2 := DefaultOptions()
o2.Routes = RoutesFromStr(fmt.Sprintf("nats://127.0.0.1:%d", o1.Cluster.Port))
s2 := RunServer(o2)
defer s2.Shutdown()
checkClusterFormed(t, s1, s2)
s2.Shutdown()
checkFor(t, time.Second, 15*time.Millisecond, func() error {
if s1.NumRoutes() != 0 {
return fmt.Errorf("route still connected")
}
return nil
})
// Now check that s1 has logged that the connection is closed and that the reason is included.
ok := false
l.Lock()
for _, n := range l.notices {
if strings.Contains(n, "connection closed: "+ClientClosed.String()) {
ok = true
break
}
}
l.Unlock()
if !ok {
t.Fatal("Log does not contain closed reason")
}
}