util

package
v0.7.3-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2021 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Localhost        = "localhost"
	Success          = "success"
	Fail             = "fail"
	AlreadyCloseCode = 3
	ConfigFile       = "config.yaml"
)

Variables

View Source
var ErrWaitTimeout = errors.New("timed out waiting for the condition")

ErrWaitTimeout is returned when the condition exited without success.

View Source
var PanicHandlers = []func(interface{}){logPanic}

PanicHandlers is a list of functions which will be invoked when a panic happens.

View Source
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

func AppendStringFromSlice(slice []string, appendStr string) (s string)

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 ChangeNumber(f float64, m int) string

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 CountDown

func CountDown(count int, msg string)

count秒后执行msg

func DownloadFile

func DownloadFile(srcPath, destPath string) error

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 Get

func Get(url string, username string, password string) []byte

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

func Jitter(duration time.Duration, maxFactor float64) time.Duration

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 KConvert

func KConvert(value string) string

func OpenPortCmd

func OpenPortCmd(port int) string

func Render

func Render(tmpl *template.Template, variables map[string]interface{}) (string, error)

Render text template with given `variables` Render-context

func SliceContain

func SliceContain(s []string, element string) bool

func SubFileName

func SubFileName(s string) string

func SubSlash

func SubSlash(s string) []string

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.

func (*Backoff) Step

func (b *Backoff) Step() time.Duration

Step (1) returns an amount of time to sleep determined by the original Duration and Jitter and (2) mutates the provided Backoff to update its Steps and Duration.

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

type ConditionFunc func() (done bool, err error)

ConditionFunc returns true if the condition is satisfied, or an error if the loop should be aborted.

type Data

type Data map[string]interface{}

type Interface

type Interface interface {
	DeepCopy() interface{}
}

Interface for delegating copy process to type

type LogLevel

type LogLevel int8
const (
	Debug LogLevel = iota
	Info
	Error
)

type Timer

type Timer interface {
	C() <-chan time.Time
	Stop() bool
	Reset(d time.Duration) bool
}

Timer allows for injecting fake or real timers into code that needs to do arbitrary things based on time.

Directories

Path Synopsis
/* Copyright 2020 The KubeSphere Authors.
/* Copyright 2020 The KubeSphere Authors.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL