utils

package
v0.0.0-...-820a931 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: AGPL-3.0 Imports: 15 Imported by: 22

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NopShutdown = NewGroupShutdown()

NopShutdown implements the Shutdowner interface but does not execute any process on shutdown.

Functions

func AbsPath

func AbsPath(inPath string) string

AbsPath returns an absolute path relative.

func CheckPreconditions

func CheckPreconditions(w http.ResponseWriter, r *http.Request, etag string) (done bool)

CheckPreconditions evaluates request preconditions based only on the Etag values.

func CleanUTF8

func CleanUTF8(s string) string

CleanUTF8 returns a string with only valid UTF-8 runes

func CloneURL

func CloneURL(u *url.URL) *url.URL

CloneURL clones the given url

func CookieDomain

func CookieDomain(domain string) string

CookieDomain removes the port and does IDNA encoding.

func DirExists

func DirExists(name string) (bool, error)

DirExists returns whether or not the directory exists on the current file system.

func DurationFuzzing

func DurationFuzzing(d time.Duration, variation float64) time.Duration

DurationFuzzing returns a duration that is near the given duration, but randomized to avoid patterns like several cache entries that expires at the same time.

func FileExists

func FileExists(name string) (bool, error)

FileExists returns whether or not the file exists on the current file system.

func IsInArray

func IsInArray(s string, a []string) bool

IsInArray returns whether or not a string is in the given array of strings.

func LimitWriter

func LimitWriter(w io.Writer, n int64) io.Writer

LimitWriter works like io.LimitReader. It writes at most n bytes to the underlying Writer. It returns io.ErrShortWrite if more than n bytes are attempted to be written.

func LimitWriterDiscard

func LimitWriterDiscard(w io.Writer, n int64) io.Writer

LimitWriterDiscard works like io.LimitReader. It writes at most n bytes to the underlying Writer. It does not return any error if more than n bytes are attempted to be written, the data is discarded.

func NewSeededRand

func NewSeededRand(seed int64) io.Reader

NewSeededRand returns a random bytes reader initialized with the given seed.

func RandomString

func RandomString(n int) string

RandomString returns a string of random alpha characters of the specified length.

func RandomStringFast

func RandomStringFast(rng *rand.Rand, n int) string

RandomStringFast returns a random string containing printable ascii characters: [0-9a-zA-Z_-]{n}. Each character encodes 6bits of entropy. To avoid wasting entropy, it is better to create a string whose length is a multiple of 10. For instance a 20 bytes string will encode 120 bits of entropy.

func ReadCloser

func ReadCloser(r io.Reader, c func() error) io.ReadCloser

ReadCloser returns an io.ReadCloser from a io.Reader and a close method.

func RetryWithExpBackoff

func RetryWithExpBackoff(count int, delay time.Duration, fn func() error) error

RetryWithExpBackoff can be used to call several times a function until it returns no error or the maximum count of calls has been reached. Between two calls, it will wait, first by the given delay, and after that, the delay will double after each failure.

func SplitTrimString

func SplitTrimString(s, sep string) []string

SplitTrimString slices s into all substrings a s separated by sep, like strings.Split. In addition it will trim all those substrings and filter out the empty ones.

func StripPort

func StripPort(domain string) string

StripPort extract the domain name from a domain:port string.

func TrimStrings

func TrimStrings(strs []string) []string

TrimStrings trim all strings and filter out the empty ones.

func UniqueStrings

func UniqueStrings(strs []string) []string

UniqueStrings returns a filtered slice without string duplicates.

func WriteCloser

func WriteCloser(w io.Writer, c func() error) io.WriteCloser

WriteCloser returns an io.WriteCloser from a io.Writer and a close method.

Types

type GroupShutdown

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

GroupShutdown allow to group multiple Shutdowner into a single one.

func NewGroupShutdown

func NewGroupShutdown(s ...Shutdowner) *GroupShutdown

NewGroupShutdown returns a new GroupShutdown

func (*GroupShutdown) Shutdown

func (g *GroupShutdown) Shutdown(ctx context.Context) error

Shutdown closes all the encapsulated Shutdowner in parallel an returns the concatenated errors.

type Shutdowner

type Shutdowner interface {
	Shutdown(ctx context.Context) error
}

Shutdowner is an interface with a Shutdown method to gracefully shutdown a running process.

Jump to

Keyboard shortcuts

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