Documentation
¶
Index ¶
- Constants
- Variables
- func AppendStringFromSlice(slice []string, appendStr string) (s string)
- func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan struct{})
- func ChangeNumber(f float64, m int) string
- func Copy(src interface{}) interface{}
- func CountDown(count int, msg string)
- func DownloadFile(srcPath, destPath string) error
- func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error
- func Get(url string, username string, password string) []byte
- func GetCurrPath() string
- func HandleCrash(additionalHandlers ...func(interface{}))
- func Iface(iface interface{}) interface{}
- func Jitter(duration time.Duration, maxFactor float64) time.Duration
- func KConvert(value string) string
- func OpenPortCmd(port int) string
- func Render(tmpl *template.Template, variables map[string]interface{}) (string, error)
- func SliceContain(s []string, element string) bool
- func SubFileName(s string) string
- func SubSlash(s string) []string
- type Backoff
- type BackoffManager
- type ConditionFunc
- type Data
- type Interface
- type LogLevel
- type Timer
Constants ¶
const ( Localhost = "localhost" Success = "success" Fail = "fail" AlreadyCloseCode = 3 ConfigFile = "config.yaml" )
Variables ¶
var ErrWaitTimeout = errors.New("timed out waiting for the condition")
ErrWaitTimeout is returned when the condition exited without success.
var PanicHandlers = []func(interface{}){logPanic}
PanicHandlers is a list of functions which will be invoked when a panic happens.
var ( // ReallyCrash controls the behavior of HandleCrash and now defaults // true. It's still exposed so components can optionally set to false // to restore prior behavior. ReallyCrash = true )
Functions ¶
func AppendStringFromSlice ¶
AppendStringFromSlice 拼接字符串
func BackoffUntil ¶
func BackoffUntil(f func(), backoff BackoffManager, sliding bool, stopCh <-chan struct{})
BackoffUntil loops until stop channel is closed, run f every duration given by BackoffManager.
If sliding is true, the period is computed after f runs. If it is false then period includes the runtime for f.
func ChangeNumber ¶
func Copy ¶
func Copy(src interface{}) interface{}
Copy creates a deep copy of whatever is passed to it and returns the copy in an interface{}. The returned value will need to be asserted to the correct type.
func DownloadFile ¶
func ExponentialBackoff ¶
func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error
ExponentialBackoff repeats a condition check with exponential backoff.
It repeatedly checks the condition and then sleeps, using `backoff.Step()` to determine the length of the sleep and adjust Duration and Steps. Stops and returns as soon as: 1. the condition check returns true or an error, 2. `backoff.Steps` checks of the condition have been done, or 3. a sleep truncated by the cap on duration has been completed. In case (1) the returned error is what the condition function returned. In all other cases, ErrWaitTimeout is returned.
func GetCurrPath ¶
func GetCurrPath() string
func HandleCrash ¶
func HandleCrash(additionalHandlers ...func(interface{}))
HandleCrash simply catches a crash and logs an error. Meant to be called via defer. Additional context-specific handlers can be provided, and will be called in case of panic. HandleCrash actually crashes, after calling the handlers and logging the panic message.
E.g., you can provide one or more additional handlers for something like shutting down go routines gracefully.
func Iface ¶
func Iface(iface interface{}) interface{}
Iface is an alias to Copy; this exists for backwards compatibility reasons.
func Jitter ¶
Jitter returns a time.Duration between duration and duration + maxFactor * duration.
This allows clients to avoid converging on periodic behavior. If maxFactor is 0.0, a suggested default value will be chosen.
func OpenPortCmd ¶
func SliceContain ¶
func SubFileName ¶
Types ¶
type Backoff ¶
type Backoff struct {
// The initial duration.
Duration time.Duration
// Duration is multiplied by factor each iteration, if factor is not zero
// and the limits imposed by Steps and Cap have not been reached.
// Should not be negative.
// The jitter does not contribute to the updates to the duration parameter.
Factor float64
// The sleep at each iteration is the duration plus an additional
// amount chosen uniformly at random from the interval between
// zero and `jitter*duration`.
Jitter float64
// The remaining number of iterations in which the duration
// parameter may change (but progress can be stopped earlier by
// hitting the cap). If not positive, the duration is not
// changed. Used for exponential backoff in combination with
// Factor and Cap.
Steps int
// A limit on revised values of the duration parameter. If a
// multiplication by the factor parameter would make the duration
// exceed the cap then the duration is set to the cap and the
// steps parameter is set to zero.
Cap time.Duration
}
Backoff holds parameters applied to a Backoff function.
type BackoffManager ¶
type BackoffManager interface {
Backoff() Timer
}
BackoffManager manages backoff with a particular scheme based on its underlying implementation. It provides an interface to return a timer for backoff, and caller shall backoff until Timer.C() drains. If the second Backoff() is called before the timer from the first Backoff() call finishes, the first timer will NOT be drained and result in undetermined behavior. The BackoffManager is supposed to be called in a single-threaded environment.
type ConditionFunc ¶
ConditionFunc returns true if the condition is satisfied, or an error if the loop should be aborted.