util

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2021 License: MIT Imports: 15 Imported by: 36

Documentation

Index

Constants

View Source
const (
	InfiniteRetry = 0
)
View Source
const MaxDigits = 18 // MAX_INT64 ~ 9 * 10^18

Variables

View Source
var Pow10Table = [MaxDigits + 1]int64{
	1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18,
}

Functions

func BeginningOfTheDay added in v1.17.0

func BeginningOfTheDay(t time.Time) time.Time

func FormatFloat

func FormatFloat(val float64, prec int) string

func GetEnvVarBool added in v1.17.0

func GetEnvVarBool(n string) (bool, bool)

func GetEnvVarDuration added in v1.17.0

func GetEnvVarDuration(n string) (time.Duration, bool)

func GetEnvVarInt added in v1.17.0

func GetEnvVarInt(n string) (int, bool)

func MillisecondsJitter added in v1.17.0

func MillisecondsJitter(d time.Duration, jitterInMilliseconds int) time.Duration

func MustParseFloat

func MustParseFloat(s string) float64

func NewValidLimiter

func NewValidLimiter(r rate.Limit, b int) (*rate.Limiter, error)

func NotZero

func NotZero(v float64) bool

func Over24Hours added in v1.18.0

func Over24Hours(since time.Time) bool

func ParseFloat

func ParseFloat(s string) (float64, error)

func Pow10

func Pow10(n int64) int64

func Render

func Render(tpl string, args interface{}) string

func Retry

func Retry(ctx context.Context, attempts int, duration time.Duration, fnToRetry func() error, errHandler func(error), predicators ...RetryPredicator) (err error)

Retry retrys the passed function for "attempts" times, if passed function return error. Setting attempts to zero means keep retrying.

func SetEnvVarBool added in v1.18.0

func SetEnvVarBool(n string, v *bool) bool

func ShouldDelay

func ShouldDelay(l *rate.Limiter, minInterval time.Duration) time.Duration

func StringSliceContains added in v1.11.1

func StringSliceContains(slice []string, needle string) bool

func Zero

func Zero(v float64) bool

Types

type Response added in v1.11.0

type Response struct {
	*http.Response

	// Body overrides the composited Body field.
	Body []byte
}

Response is wrapper for standard http.Response and provides more methods.

func NewResponse added in v1.11.0

func NewResponse(r *http.Response) (response *Response, err error)

NewResponse is a wrapper of the http.Response instance, it reads the response body and close the file.

func (*Response) DecodeJSON added in v1.11.0

func (r *Response) DecodeJSON(o interface{}) error

func (*Response) IsError added in v1.11.0

func (r *Response) IsError() bool

func (*Response) IsHTML added in v1.11.0

func (r *Response) IsHTML() bool

func (*Response) IsJSON added in v1.11.0

func (r *Response) IsJSON() bool

func (*Response) String added in v1.11.0

func (r *Response) String() string

String converts response body to string. An empty string will be returned if error.

type RetryPredicator

type RetryPredicator func(e error) bool

type TimeProfile added in v1.17.0

type TimeProfile struct {
	StartTime, EndTime time.Time
	Duration           time.Duration
}

func StartTimeProfile added in v1.17.0

func StartTimeProfile() TimeProfile

func (*TimeProfile) Stop added in v1.17.0

func (p *TimeProfile) Stop() time.Duration

func (*TimeProfile) TilNow added in v1.17.0

func (p *TimeProfile) TilNow() time.Duration

type VolatileMemory

type VolatileMemory struct {
	// contains filtered or unexported fields
}

func NewDetectorCache

func NewDetectorCache() *VolatileMemory

func (*VolatileMemory) IsObjectFresh

func (i *VolatileMemory) IsObjectFresh(obj interface{}, ttl time.Duration) bool

func (*VolatileMemory) IsTextFresh

func (i *VolatileMemory) IsTextFresh(text string, ttl time.Duration) bool

Jump to

Keyboard shortcuts

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