Skip to content

Commit

Permalink
Add unstable envvar to disable server keepalives (#43483)
Browse files Browse the repository at this point in the history
  • Loading branch information
espadolini authored Jun 25, 2024
1 parent 313cc26 commit c7d3034
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
31 changes: 20 additions & 11 deletions lib/inventory/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ type Controller struct {
serviceCounter *serviceCounter
auth Auth
authID string
serverKeepAliveEnabled bool
serverKeepAlive time.Duration
serverTTL time.Duration
instanceTTL time.Duration
Expand All @@ -202,6 +203,18 @@ type Controller struct {
cancel context.CancelFunc
}

// serverKeepAliveDisabledEnv checks if the periodic server keepalive has been
// explicitly disabled via environment variable.
func serverKeepAliveDisabledEnv() bool {
return os.Getenv("TELEPORT_UNSTABLE_DISABLE_SERVER_KEEPALIVE") == "yes"
}

// instanceHeartbeatsDisabledEnv checks if instance heartbeats have been explicitly disabled
// via environment variable.
func instanceHeartbeatsDisabledEnv() bool {
return os.Getenv("TELEPORT_UNSTABLE_DISABLE_INSTANCE_HB") == "yes"
}

// NewController sets up a new controller instance.
func NewController(auth Auth, usageReporter usagereporter.UsageReporter, opts ...ControllerOption) *Controller {
var options controllerOptions
Expand All @@ -220,6 +233,7 @@ func NewController(auth Auth, usageReporter usagereporter.UsageReporter, opts ..
return &Controller{
store: NewStore(),
serviceCounter: &serviceCounter{},
serverKeepAliveEnabled: !serverKeepAliveDisabledEnv(),
serverKeepAlive: options.serverKeepAlive,
serverTTL: apidefaults.ServerAnnounceTTL,
instanceTTL: apidefaults.InstanceHeartbeatTTL,
Expand Down Expand Up @@ -325,9 +339,10 @@ func (c *Controller) handleControlStream(handle *upstreamHandle) {
c.testEvent(handlerClose)
}()

// keepAliveInit tracks wether or not we've initialized the server keepalive sub-interval. we do this lazily
// upon receipt of the first heartbeat since not all servers send heartbeats.
var keepAliveInit bool
// keepAliveNeedInit tracks wether or not we should initialize the server
// keepalive sub-interval upon receiving a heartbeat. We do this lazily upon
// receipt of the first heartbeat since not all servers send heartbeats.
keepAliveNeedInit := c.serverKeepAliveEnabled

for {
select {
Expand All @@ -344,15 +359,15 @@ func (c *Controller) handleControlStream(handle *upstreamHandle) {
handle.CloseWithError(err)
return
}
if !keepAliveInit {
if keepAliveNeedInit {
// this is the first heartbeat, so we need to initialize the keepalive sub-interval
handle.ticker.Push(interval.SubInterval[intervalKey]{
Key: serverKeepAliveKey,
Duration: c.serverKeepAlive,
FirstDuration: halfJitter(c.serverKeepAlive),
Jitter: seventhJitter,
})
keepAliveInit = true
keepAliveNeedInit = false
}
case proto.UpstreamInventoryPong:
c.handlePong(handle, m)
Expand Down Expand Up @@ -391,12 +406,6 @@ func (c *Controller) handleControlStream(handle *upstreamHandle) {
}
}

// instanceHeartbeatsDisabledEnv checks if instance heartbeats have been explicitly disabled
// via environment variable.
func instanceHeartbeatsDisabledEnv() bool {
return os.Getenv("TELEPORT_UNSTABLE_DISABLE_INSTANCE_HB") == "yes"
}

func (c *Controller) heartbeatInstanceState(handle *upstreamHandle, now time.Time) error {
if !c.instanceHBEnabled {
return nil
Expand Down
12 changes: 12 additions & 0 deletions lib/inventory/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@ func TestInstanceHeartbeatDisabledEnv(t *testing.T) {
require.False(t, controller.instanceHBEnabled)
}

func TestServerKeepaliveDisabledEnv(t *testing.T) {
t.Setenv("TELEPORT_UNSTABLE_DISABLE_SERVER_KEEPALIVE", "yes")

controller := NewController(
&fakeAuth{},
usagereporter.DiscardUsageReporter{},
)
defer controller.Close()

require.False(t, controller.serverKeepAliveEnabled)
}

// TestInstanceHeartbeat verifies basic expected behaviors for instance heartbeat.
func TestInstanceHeartbeat(t *testing.T) {
const serverID = "test-instance"
Expand Down

0 comments on commit c7d3034

Please sign in to comment.