Files

59 lines
2.0 KiB
Go

/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package services
import "context"
// ProcessForkedContext adds a flag to the context to indicate the Teleport
// process has running forked child(ren).
func ProcessForkedContext(parent context.Context) context.Context {
return addFlagToContext[processForkedFlag](parent)
}
// HasProcessForked returns true if the Teleport process has running forked
// child(ren).
func HasProcessForked(ctx context.Context) bool {
return getFlagFromContext[processForkedFlag](ctx)
}
// ShouldDeleteServerHeartbeatsOnShutdown checks whether server heartbeats
// should be deleted based on the process shutdown context.
func ShouldDeleteServerHeartbeatsOnShutdown(ctx context.Context) bool {
switch {
// A child process can be forked to upgrade the Teleport binary. The child
// will take over the heartbeats so do NOT delete them in that case. In
// worst case scenarios if the child fails to register new heartbeats, the
// old ones will get deleted automatically upon expiry.
case HasProcessForked(ctx):
return false
default:
return true
}
}
func addFlagToContext[FlagType any](parent context.Context) context.Context {
return context.WithValue(parent, (*FlagType)(nil), (*FlagType)(nil))
}
func getFlagFromContext[FlagType any](ctx context.Context) bool {
_, ok := ctx.Value((*FlagType)(nil)).(*FlagType)
return ok
}
type processForkedFlag struct{}