2022-08-07 10:22:21 +00:00
|
|
|
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{
|
2023-12-04 15:59:10 +00:00
|
|
|
Context: base,
|
|
|
|
attemptTimeout: attemptTimeout,
|
|
|
|
numberOfAttempts: numberOfAttempts,
|
2022-08-07 10:22:21 +00:00
|
|
|
}, cancel
|
|
|
|
}
|
|
|
|
|
|
|
|
type checkContext struct {
|
2023-12-04 15:59:10 +00:00
|
|
|
attemptTimeout time.Duration
|
|
|
|
numberOfAttempts uint
|
2022-08-07 10:22:21 +00:00
|
|
|
context.Context
|
|
|
|
}
|
|
|
|
|
2023-12-04 15:59:10 +00:00
|
|
|
func (c *checkContext) AttemptCount() uint {
|
|
|
|
return c.numberOfAttempts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checkContext) AttemptTimeout() time.Duration {
|
|
|
|
return c.attemptTimeout
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
func (c *checkContext) WithParent(ctx context.Context) Context {
|
|
|
|
return &checkContext{
|
|
|
|
Context: ctx,
|
|
|
|
attemptTimeout: c.attemptTimeout,
|
|
|
|
}
|
|
|
|
}
|