nurse/check/context.go
Peter Kurfer 0faff1d481
Some checks failed
Renovate / renovate (push) Successful in 1m8s
Go build / build (push) Failing after 2m3s
chore: cleanup, add docs, refactor some quirks and prepare release
2023-12-04 16:59:10 +01:00

40 lines
896 B
Go

package check
import (
"context"
"time"
)
var _ Context = (*checkContext)(nil)
func AttemptsContext(parent context.Context, numberOfAttempts uint, attemptTimeout time.Duration) (*checkContext, context.CancelFunc) {
finalTimeout := time.Duration(numberOfAttempts) * attemptTimeout
base, cancel := context.WithTimeout(parent, finalTimeout)
return &checkContext{
Context: base,
attemptTimeout: attemptTimeout,
numberOfAttempts: numberOfAttempts,
}, cancel
}
type checkContext struct {
attemptTimeout time.Duration
numberOfAttempts uint
context.Context
}
func (c *checkContext) AttemptCount() uint {
return c.numberOfAttempts
}
func (c *checkContext) AttemptTimeout() time.Duration {
return c.attemptTimeout
}
func (c *checkContext) WithParent(ctx context.Context) Context {
return &checkContext{
Context: ctx,
attemptTimeout: c.attemptTimeout,
}
}