util

package
v1.31.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLimitReached = errors.New("limit reached")

ErrLimitReached is the error returned by the Limiter and LimitWriter when the predefined limit has been reached

Functions

func BasicAuth added in v1.14.0

func BasicAuth(user, pass string) string

BasicAuth encodes the Authorization header value for basic auth

func Contains added in v1.29.0

func Contains[T comparable](haystack []T, needle T) bool

Contains returns true if needle is contained in haystack

func ContainsAll added in v1.29.0

func ContainsAll[T comparable](haystack []T, needles []T) bool

ContainsAll returns true if all needles are contained in haystack

func ContainsIP added in v1.29.0

func ContainsIP(haystack []netip.Prefix, needle netip.Addr) bool

ContainsIP returns true if any one of the of prefixes contains the ip.

func DetectContentType added in v1.12.0

func DetectContentType(b []byte, filename string) (mimeType string, ext string)

DetectContentType probes the byte array b and returns mime type and file extension. The filename is only used to override certain special cases.

func FileExists

func FileExists(filename string) bool

FileExists checks if a file exists, and returns true if it does

func Gzip added in v1.17.0

func Gzip(next http.Handler) http.Handler

Gzip is a HTTP middleware to transparently compress responses using gzip. Original code from https://gist.github.com/CJEnright/bc2d8b8dc0c1389a9feeddb110f822d7 (MIT)

func LastString added in v1.26.0

func LastString(s []string, def string) string

LastString returns the last string in a slice, or def if s is empty

func MaybeMarshalJSON added in v1.25.0

func MaybeMarshalJSON(v any) string

MaybeMarshalJSON returns a JSON string of the given object, or "<cannot serialize>" if serialization failed. This is useful for logging purposes where a failure doesn't matter that much.

func ParseFutureTime added in v1.6.0

func ParseFutureTime(s string, now time.Time) (time.Time, error)

ParseFutureTime parses a date/time string to a time.Time. It supports unix timestamps, durations and natural language dates

func ParsePriority added in v1.8.0

func ParsePriority(priority string) (int, error)

ParsePriority parses a priority string into its equivalent integer value

func ParseSize added in v1.12.0

func ParseSize(s string) (int64, error)

ParseSize parses a size string like 2K or 2M into bytes. If no unit is found, e.g. 123, bytes is assumed.

func PriorityString added in v1.9.0

func PriorityString(priority int) (string, error)

PriorityString converts a priority number to a string

func QuoteCommand added in v1.27.1

func QuoteCommand(command []string) string

QuoteCommand combines a command array to a string, quoting arguments that need quoting. This function is naive, and sometimes wrong. It is only meant for lo pretty-printing a command.

Warning: Never use this function with the intent to run the resulting command.

Example:

[]string{"ls", "-al", "Document Folder"} -> ls -al "Document Folder"

func RandomString

func RandomString(length int) string

RandomString returns a random string with a given length

func ReadPassword added in v1.14.0

func ReadPassword(in io.Reader) ([]byte, error)

ReadPassword will read a password from STDIN. If the terminal supports it, it will not print the input characters to the screen. If not, it'll just read using normal readline semantics (useful for testing).

func ShortTopicURL added in v1.9.0

func ShortTopicURL(s string) string

ShortTopicURL shortens the topic URL to be human-friendly, removing the http:// or https://

func SplitKV added in v1.21.0

func SplitKV(s string, sep string) (key string, value string)

SplitKV splits a string into a key/value pair using a separator, and trimming space. If the separator is not found, key is empty.

func SplitNoEmpty added in v1.8.0

func SplitNoEmpty(s string, sep string) []string

SplitNoEmpty splits a string using strings.Split, but filters out empty strings

func ValidRandomString added in v1.16.0

func ValidRandomString(s string, length int) bool

ValidRandomString returns true if the given string matches the format created by RandomString

Types

type BatchingQueue added in v1.29.1

type BatchingQueue[T any] struct {
	// contains filtered or unexported fields
}

BatchingQueue is a queue that creates batches of the enqueued elements based on a max batch size and a batch timeout.

Example:

q := NewBatchingQueue[int](2, 500 * time.Millisecond)
go func() {
  for batch := range q.Dequeue() {
    fmt.Println(batch)
  }
}()
q.Enqueue(1)
q.Enqueue(2)
q.Enqueue(3)
time.Sleep(time.Second)

This example will emit batch [1, 2] immediately (because the batch size is 2), and a batch [3] after 500ms.

func NewBatchingQueue added in v1.29.1

func NewBatchingQueue[T any](batchSize int, timeout time.Duration) *BatchingQueue[T]

NewBatchingQueue creates a new BatchingQueue

func (*BatchingQueue[T]) Dequeue added in v1.29.1

func (q *BatchingQueue[T]) Dequeue() <-chan []T

Dequeue returns a channel emitting batches of elements

func (*BatchingQueue[T]) Enqueue added in v1.29.1

func (q *BatchingQueue[T]) Enqueue(element T)

Enqueue enqueues an element to the queue. If the configured batch size is reached, the batch will be emitted immediately.

type CachingEmbedFS added in v1.5.0

type CachingEmbedFS struct {
	ModTime time.Time
	FS      embed.FS
}

CachingEmbedFS is a wrapper around embed.FS that allows setting a ModTime, so that the default static file server can send 304s back. It can be used like this:

  var (
     //go:embed docs
     docsStaticFs     embed.FS
     docsStaticCached = &util.CachingEmbedFS{ModTime: time.Now(), FS: docsStaticFs}
  )

	 http.FileServer(http.FS(docsStaticCached)).ServeHTTP(w, r)

func (CachingEmbedFS) Open added in v1.5.0

func (f CachingEmbedFS) Open(name string) (fs.File, error)

Open opens a file in the embedded filesystem and returns a fs.File with the static ModTime

type ContentTypeWriter added in v1.12.0

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

ContentTypeWriter is an implementation of http.ResponseWriter that will detect the content type and set the Content-Type and (optionally) Content-Disposition headers accordingly.

It will always set a Content-Type based on http.DetectContentType, but will never send the "text/html" content type.

func NewContentTypeWriter added in v1.12.0

func NewContentTypeWriter(w http.ResponseWriter, filename string) *ContentTypeWriter

NewContentTypeWriter creates a new ContentTypeWriter

func (*ContentTypeWriter) Write added in v1.12.0

func (w *ContentTypeWriter) Write(p []byte) (n int, err error)

type FixedLimiter added in v1.12.0

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

FixedLimiter is a helper that allows adding values up to a well-defined limit. Once the limit is reached ErrLimitReached will be returned. FixedLimiter may be used by multiple goroutines.

func NewFixedLimiter added in v1.12.0

func NewFixedLimiter(limit int64) *FixedLimiter

NewFixedLimiter creates a new Limiter

func (*FixedLimiter) Allow added in v1.12.0

func (l *FixedLimiter) Allow(n int64) error

Allow adds n to the limiters internal value, but only if the limit has not been reached. If the limit was exceeded after adding n, ErrLimitReached is returned.

type LimitWriter added in v1.12.0

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

LimitWriter implements an io.Writer that will pass through all Write calls to the underlying writer w until any of the limiter's limit is reached, at which point a Write will return ErrLimitReached. Each limiter's value is increased with every write.

func NewLimitWriter added in v1.12.0

func NewLimitWriter(w io.Writer, limiters ...Limiter) *LimitWriter

NewLimitWriter creates a new LimitWriter

func (*LimitWriter) Write added in v1.12.0

func (w *LimitWriter) Write(p []byte) (n int, err error)

Write passes through all writes to the underlying writer until any of the given limiter's limit is reached

type Limiter added in v1.1.3

type Limiter interface {
	// Allow adds n to the limiters internal value, or returns ErrLimitReached if the limit has been reached
	Allow(n int64) error
}

Limiter is an interface that implements a rate limiting mechanism, e.g. based on time or a fixed value

type PeekedReadCloser added in v1.20.0

type PeekedReadCloser struct {
	PeekedBytes  []byte
	LimitReached bool
	// contains filtered or unexported fields
}

PeekedReadCloser is a ReadCloser that allows peeking into a stream and buffering it in memory. It can be instantiated using the Peek function. After a stream has been peeked, it can still be fully read by reading the PeekedReadCloser. It first drained from the memory buffer, and then from the remaining underlying reader.

func Peek added in v1.20.0

func Peek(underlying io.ReadCloser, limit int) (*PeekedReadCloser, error)

Peek reads the underlying ReadCloser into memory up until the limit and returns a PeekedReadCloser. It does not return an error if limit is reached. Instead, LimitReached will be set to true.

func (*PeekedReadCloser) Close added in v1.20.0

func (r *PeekedReadCloser) Close() error

Close closes the underlying stream

func (*PeekedReadCloser) Read added in v1.20.0

func (r *PeekedReadCloser) Read(p []byte) (n int, err error)

Read reads from the peeked bytes and then from the underlying stream

type RateLimiter added in v1.12.0

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

RateLimiter is a Limiter that wraps a rate.Limiter, allowing a floating time-based limit.

func NewBytesLimiter added in v1.12.0

func NewBytesLimiter(bytes int, interval time.Duration) *RateLimiter

NewBytesLimiter creates a RateLimiter that is meant to be used for a bytes-per-interval limit, e.g. 250 MB per day. And example of the underlying idea can be found here: https://go.dev/play/p/0ljgzIZQ6dJ

func NewRateLimiter added in v1.12.0

func NewRateLimiter(r rate.Limit, b int) *RateLimiter

NewRateLimiter creates a new RateLimiter

func (*RateLimiter) Allow added in v1.12.0

func (l *RateLimiter) Allow(n int64) error

Allow adds n to the limiters internal value, but only if the limit has not been reached. If the limit was exceeded after adding n, ErrLimitReached is returned.

Jump to

Keyboard shortcuts

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