Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHANGED] Gateway connections now always send PINGs #1692

Merged
merged 1 commit into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 29 additions & 5 deletions server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3952,18 +3952,30 @@ func (c *client) processPingTimer() {

c.Debugf("%s Ping Timer", c.typeString())

var sendPing bool

// If we have had activity within the PingInterval then
// there is no need to send a ping. This can be client data
// or if we received a ping from the other side.
pingInterval := c.srv.getOpts().PingInterval
if c.kind == GATEWAY {
pingInterval = adjustPingIntervalForGateway(pingInterval)
sendPing = true
}
now := time.Now()
needRTT := c.rtt == 0 || now.Sub(c.rttStart) > DEFAULT_RTT_MEASUREMENT_INTERVAL

if delta := now.Sub(c.last); delta < pingInterval && !needRTT {
c.Debugf("Delaying PING due to client activity %v ago", delta.Round(time.Second))
} else if delta := now.Sub(c.ping.last); delta < pingInterval && !needRTT {
c.Debugf("Delaying PING due to remote ping %v ago", delta.Round(time.Second))
} else {
// Do not delay PINGs for GATEWAY connections.
if c.kind != GATEWAY {
if delta := now.Sub(c.last); delta < pingInterval && !needRTT {
c.Debugf("Delaying PING due to client activity %v ago", delta.Round(time.Second))
} else if delta := now.Sub(c.ping.last); delta < pingInterval && !needRTT {
c.Debugf("Delaying PING due to remote ping %v ago", delta.Round(time.Second))
} else {
sendPing = true
}
}
if sendPing {
// Check for violation
if c.ping.out+1 > c.srv.getOpts().MaxPingsOut {
c.Debugf("Stale Client Connection - Closing")
Expand All @@ -3981,12 +3993,24 @@ func (c *client) processPingTimer() {
c.mu.Unlock()
}

// Returns the smallest value between the given `d` and `gatewayMaxPingInterval` durations.
// Invoked for connections known to be of GATEWAY type.
func adjustPingIntervalForGateway(d time.Duration) time.Duration {
if d > gatewayMaxPingInterval {
return gatewayMaxPingInterval
}
return d
}

// Lock should be held
func (c *client) setPingTimer() {
if c.srv == nil {
return
}
d := c.srv.getOpts().PingInterval
if c.kind == GATEWAY {
d = adjustPingIntervalForGateway(d)
}
c.ping.tmr = time.AfterFunc(d, c.processPingTimer)
}

Expand Down
2 changes: 1 addition & 1 deletion server/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (

const (
// VERSION is the current version for the server.
VERSION = "2.2.0-beta.28"
VERSION = "2.2.0-beta.29"

// PROTO is the currently supported protocol.
// 0 was the original
Expand Down
5 changes: 5 additions & 0 deletions server/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ const (
gwClusterOffset = gwReplyPrefixLen
gwServerOffset = gwClusterOffset + gwHashLen + 1
gwSubjectOffset = gwServerOffset + gwHashLen + 1

// Gateway connections send PINGs regardless of traffic. The interval is
// either Options.PingInterval or this value, whichever is the smallest.
gwMaxPingInterval = 15 * time.Second
)

var (
gatewayConnectDelay = defaultGatewayConnectDelay
gatewayReconnectDelay = defaultGatewayReconnectDelay
gatewayMaxRUnsubBeforeSwitch = defaultGatewayMaxRUnsubBeforeSwitch
gatewaySolicitDelay = int64(defaultSolicitGatewaysDelay)
gatewayMaxPingInterval = gwMaxPingInterval
)

// Warning when user configures gateway TLS insecure
Expand Down
47 changes: 47 additions & 0 deletions server/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6201,3 +6201,50 @@ func TestGatewayUpdateURLsFromRemoteCluster(t *testing.T) {
expected[fmt.Sprintf("127.0.0.1:%d", ob3.Gateway.Port)] = "B3"
checkURLs(expected)
}

type capturePingConn struct {
net.Conn
ch chan struct{}
}

func (c *capturePingConn) Write(b []byte) (int, error) {
if bytes.Contains(b, []byte(pingProto)) {
select {
case c.ch <- struct{}{}:
default:
}
}
return c.Conn.Write(b)
}

func TestGatewayPings(t *testing.T) {
gatewayMaxPingInterval = 50 * time.Millisecond
defer func() { gatewayMaxPingInterval = gwMaxPingInterval }()

ob := testDefaultOptionsForGateway("B")
sb := RunServer(ob)
defer sb.Shutdown()

oa := testGatewayOptionsFromToWithServers(t, "A", "B", sb)
sa := RunServer(oa)
defer sa.Shutdown()

waitForInboundGateways(t, sa, 1, 2*time.Second)
waitForOutboundGateways(t, sa, 1, 2*time.Second)
waitForInboundGateways(t, sb, 1, 2*time.Second)
waitForOutboundGateways(t, sb, 1, 2*time.Second)

c := sa.getOutboundGatewayConnection("B")
ch := make(chan struct{}, 1)
c.mu.Lock()
c.nc = &capturePingConn{c.nc, ch}
c.mu.Unlock()

for i := 0; i < 5; i++ {
select {
case <-ch:
case <-time.After(250 * time.Millisecond):
t.Fatalf("Did not send PING")
}
}
}
3 changes: 3 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,9 @@ func (s *Server) setFirstPingTimer(c *client) {
if d > firstPingInterval {
d = firstPingInterval
}
if c.kind == GATEWAY {
d = adjustPingIntervalForGateway(d)
}
} else if d > firstClientPingInterval {
d = firstClientPingInterval
}
Expand Down