utils

package
v0.0.0-...-1f1d885 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocalIP

func GetLocalIP() string

GetLocalIP will return a local IP(non-loopback, non 0.0.0.0), if there is one

func GoWithRecovery

func GoWithRecovery(exec func(), recoverFn func(r interface{}))

GoWithRecovery wraps goroutine startup call with force recovery. it will dump current goroutine stack into log if catch any recover result.

exec:      execute logic function.
recoverFn: handler will be called after recover and before dump stack, passing `nil` means noop.

func WithRetry

func WithRetry(ctx context.Context, maxRetryTimes uint, duration time.Duration, f func(uint) bool)

WithRetry provides a general retry logic.

The given f will keep running, until: - f returns true. It means work is done. - retry times is greater than the given times - ctx is done.

Otherwise, this function will wait a time of the given duration and continue to execute f.

The argument provided for f is the retried times.

func WithRetryBackoff

func WithRetryBackoff(ctx context.Context, maxRetryTimes uint, firstDuration time.Duration, f func(uint) bool)

WithRetryBackoff provides a general retry logic.

The given f will keep running, until: - f returns true. It means work is done. - retry times is greater than the given times - ctx is done.

Otherwise, this function will wait a time of the given duration in a backoff way, and continue to execute f.

The argument provided for f is the retried times.

Types

type BytesBufferPool

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

func (*BytesBufferPool) Get

func (bbp *BytesBufferPool) Get() *bytes.Buffer

func (*BytesBufferPool) Put

func (bbp *BytesBufferPool) Put(bb *bytes.Buffer)

type HeaderPool

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

func (*HeaderPool) Get

func (hdp *HeaderPool) Get() http.Header

func (*HeaderPool) Put

func (hdp *HeaderPool) Put(hdv http.Header)

type RateLimit

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

RateLimit wraps a fix sized channel to control concurrency.

func NewRateLimit

func NewRateLimit(n int) *RateLimit

NewRateLimit creates a limit controller with capacity n.

func (*RateLimit) GetCapacity

func (r *RateLimit) GetCapacity() int

GetCapacity returns the token capacity.

func (*RateLimit) GetToken

func (r *RateLimit) GetToken(done <-chan struct{}) (exit bool)

GetToken acquires a token.

func (*RateLimit) PutToken

func (r *RateLimit) PutToken()

PutToken puts a token back.

type ResponseWriter

type ResponseWriter struct {
	Body    *bytes.Buffer
	Headers http.Header
	Code    int
}

func NewRespWriter

func NewRespWriter(body *bytes.Buffer, header http.Header) ResponseWriter

func (*ResponseWriter) Header

func (r *ResponseWriter) Header() http.Header

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(b []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(statusCode int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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