132 lines
4.4 KiB
Go
132 lines
4.4 KiB
Go
package zoptions
|
|
|
|
import "time"
|
|
|
|
type NotifyParameters struct {
|
|
// AlwaysSend makes sure this message
|
|
// is always sent no matter the condition.
|
|
//
|
|
// It will cause `zen` to ignore every other
|
|
// parameters and just send the message.
|
|
AlwaysSend bool
|
|
|
|
// InitialBackoff is the starting backoff.
|
|
//
|
|
// If not specified, the default duration is time.Minute.
|
|
InitialBackoff time.Duration
|
|
// MaxBackoff is the maximum backoff. If unset, the default value
|
|
// is time.Hour * 24.
|
|
MaxBackoff time.Duration
|
|
|
|
// BackoffFormula is a string that represents the formula to calculate the backoff time
|
|
// when multiple message with the same key is fired off repeatedly.
|
|
//
|
|
// It uses CEL (Common Expression Language) syntax. See: https://github.com/google/cel-go
|
|
// for the language spec.
|
|
//
|
|
// Expression must return a duration type.
|
|
//
|
|
// If invalid cel expression, empty string, or wrong return value, default formula will be used.
|
|
//
|
|
// Available variables:
|
|
//
|
|
// - `repeat`: the number of times the notification has been sent. type: int.
|
|
// - `last`: the last time the notification was sent. type: timestamp.
|
|
// - `prev_backoff`: the backoff value from previous evaluation. 0 if first seen. Type: duration.
|
|
// - `initial_backoff`: the initial_backoff value. Type: duration
|
|
// - `max_backoff`: the maximum backoff value. Type: duration
|
|
//
|
|
// Available Non-Standard functions:
|
|
//
|
|
// // pow multiplies the base with the exponent
|
|
// pow(base: double, exp: double) -> double
|
|
//
|
|
// // mult_dur_double multiplies the duration with the double value
|
|
// mult_dur_double(dur: duration, mult: double) -> duration
|
|
//
|
|
//
|
|
// Example (and also default formula):
|
|
//
|
|
// mult_dur_double(initial_backoff, pow(1.5, double(repeat))) > max_backoff
|
|
// ? max_backoff
|
|
// : mult_dur_double(initial_backoff, pow(1.5, double(repeat)))
|
|
BackoffFormula string
|
|
}
|
|
|
|
type NotifyOption interface {
|
|
Apply(parameters *NotifyParameters)
|
|
}
|
|
|
|
type NotifyOptionFunc func(parameters *NotifyParameters)
|
|
|
|
func (f NotifyOptionFunc) Apply(parameters *NotifyParameters) {
|
|
f(parameters)
|
|
}
|
|
|
|
type NotifiyOptionBuilder []NotifyOption
|
|
|
|
func (no NotifiyOptionBuilder) Apply(parameters *NotifyParameters) {
|
|
for _, opt := range no {
|
|
opt.Apply(parameters)
|
|
}
|
|
}
|
|
|
|
func (no NotifiyOptionBuilder) AlwaysSend(alwaysSend bool) NotifiyOptionBuilder {
|
|
return append(no, NotifyOptionFunc(func(parameters *NotifyParameters) {
|
|
parameters.AlwaysSend = alwaysSend
|
|
}))
|
|
}
|
|
|
|
func (no NotifiyOptionBuilder) InitialBackoff(backoff time.Duration) NotifiyOptionBuilder {
|
|
return append(no, NotifyOptionFunc(func(parameters *NotifyParameters) {
|
|
parameters.InitialBackoff = backoff
|
|
}))
|
|
}
|
|
|
|
func (no NotifiyOptionBuilder) MaxBackoff(maxBackoff time.Duration) NotifiyOptionBuilder {
|
|
return append(no, NotifyOptionFunc(func(parameters *NotifyParameters) {
|
|
parameters.MaxBackoff = maxBackoff
|
|
}))
|
|
}
|
|
|
|
// BackoffFormula is a string that represents the formula to calculate the backoff time
|
|
// when multiple message with the same key is fired off repeatedly.
|
|
//
|
|
// It uses CEL (Common Expression Language) syntax. See: https://github.com/google/cel-go
|
|
// for the language spec.
|
|
//
|
|
// Expression must return a duration type.
|
|
//
|
|
// If invalid cel expression, empty string, or wrong return value, default formula will be used.
|
|
//
|
|
// Available variables:
|
|
//
|
|
// - `repeat`: the number of times the notification has been sent. type: int.
|
|
// - `last`: the last time the notification was sent. type: timestamp.
|
|
// - `prev_backoff`: the backoff value from previous evaluation. 0 if this message is first seen. Type: duration.
|
|
// - `initial_backoff`: the initial_backoff value. Type: duration
|
|
// - `max_backoff`: the maximum backoff value. Type: duration
|
|
//
|
|
// Available Non-Standard functions:
|
|
//
|
|
// // pow multiplies the base with the exponent
|
|
// pow(base: double, exp: double) -> double
|
|
//
|
|
// // mult_dur_double multiplies the duration with the double value
|
|
// mult_dur_double(dur: duration, mult: double) -> duration
|
|
//
|
|
// Example (and also default formula):
|
|
//
|
|
// mult_dur_double(initial_backoff, pow(1.5, double(repeat))) > max_backoff
|
|
// ? max_backoff
|
|
// : mult_dur_double(initial_backoff, pow(1.5, double(repeat)))
|
|
func (no NotifiyOptionBuilder) BackoffFormula(formula string) NotifiyOptionBuilder {
|
|
return append(no, NotifyOptionFunc(func(parameters *NotifyParameters) {
|
|
parameters.BackoffFormula = formula
|
|
}))
|
|
}
|
|
|
|
func Notify() NotifiyOptionBuilder {
|
|
return NotifiyOptionBuilder{}
|
|
}
|