util

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2024 License: Apache-2.0, BSD-2-Clause, MIT, + 1 more Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlwaysRetry

func AlwaysRetry(_ error) bool

AlwaysRetry ignores the error and always returns true. This can be used as a CanRetryPredicate when using Retry if a retry is required irrespective of any error during the invocation of the function.

func CloseResponseBody

func CloseResponseBody(response *http.Response)

CloseResponseBody closes the response body if the response is not nil. As per https://pkg.go.dev/net/http - The http Client and Transport guarantee that Body is always non-nil, even on responses without a body or responses with a zero-length body. It is the caller's responsibility to close Body. The default HTTP client's Transport may not reuse HTTP/1.x "keep-alive" TCP connections if the Body is not read to completion and closed.

func ConstructBaseAddress

func ConstructBaseAddress(tlsEnabled bool, hostPort string) string

ConstructBaseAddress creates a base address selecting a scheme based on tlsEnabled and using hostPort.

func CreateCACertPool

func CreateCACertPool(caCertBundlePath string) (*x509.CertPool, error)

CreateCACertPool creates a CA cert pool gives a CA cert bundle

func CreateTLSConfig

func CreateTLSConfig(tlsEnabledFn IsTLSEnabledFn, serverName, caCertPath string, keyPair *KeyPair) (*tls.Config, error)

CreateTLSConfig creates a TLS Config to be used for TLS communication.

func ResponseHasOKCode

func ResponseHasOKCode(response *http.Response) bool

ResponseHasOKCode checks if the response is one of the accepted OK responses.

Types

type CanRetryPredicate

type CanRetryPredicate func(err error) bool

CanRetryPredicate provides a predicate whose return value decides if an error can be ignored for the next retry.

type IsTLSEnabledFn

type IsTLSEnabledFn func() bool

IsTLSEnabledFn returns true if TLS is enabled and false otherwise.

type KeyPair

type KeyPair struct {
	// CertPath is the path to the certificate
	CertPath string
	// KeyPath is the path to the private key
	KeyPath string
}

KeyPair is a collection of paths one for the certificate and another for the key. This is used to configure certificate-key pair when configuring TLS config.

type Result

type Result[T any] struct {
	Value T
	Err   error
}

Result encapsulates the result of retrying a function via Retry.

func Retry

func Retry[T any](ctx context.Context, logger *zap.Logger, operation string, fn RetriableFunc[T], numAttempts int, backOff time.Duration, canRetryFn CanRetryPredicate) Result[T]

Retry will retry invoking the given function `fn` a max of `numAttempts` with a back off between successive attempts. If an invocation of the function returns an error then it will check it can proceed with the retry by evaluating via `canRetryFn`. A Result containing the return value if function invocation was successful or an error if the function was not successful will be returned to the caller. The caller can check if the Result is an error by invoking Result.IsErr function.

func (Result[T]) IsErr

func (r Result[T]) IsErr() bool

IsErr checks if the Result has error set. If error is not nil then it will return true else it will return false.

type RetriableFunc

type RetriableFunc[T any] func() (T, error)

RetriableFunc is any function that should be retried. It returns any T and an error.

Jump to

Keyboard shortcuts

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