common

package
v0.6.9 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// DefaultEtcdRetryCount for Etcd operations
	DefaultEtcdRetryCount = 3
	// DefaultEtcdtIntervalBetweenRetries for Etcd failed operations
	DefaultEtcdtIntervalBetweenRetries = time.Second * 5
)

Variables

This section is empty.

Functions

func AddParts

func AddParts(address string) (string, int, error)

AddParts returns the parts of the address

func CheckPasswordAgainstHash

func CheckPasswordAgainstHash(password, hash string) bool

CheckPasswordAgainstHash compares the password with the hashed password

func CnvSliceStrToSliceInf

func CnvSliceStrToSliceInf(in []string) []interface{}

CnvSliceStrToSliceInf converts the given slice of string to slice of interface which be used later in logging.

func ExternalIP

func ExternalIP() (string, error)

ExternalIP returns an external ip address of the current host

func Find

func Find(a []string, x interface{}, op string) bool

Find tells whether string contains x. op - boolean operator, expected `AND` `OR` string value. x - could be string or slice of string.

func GenerateBcryptHash

func GenerateBcryptHash(password string, cost int) (string, error)

GenerateBcryptHash return the hash password

func Hash

func Hash(s string, f crypto.Hash) string

Hash generates a new slice of bytee hash from a given string using a given hash algorithms.

func HashFNV

func HashFNV(s string) string

HashFNV generates a new 64-bit number from a given string using 64-bit FNV-1a hash function.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound verifies the type of given error is NotFound or not.

func NewHTTPClient

func NewHTTPClient() *http.Client

NewHTTPClient returns the net/http.Client with a custom set of configs.

func ParseDuration

func ParseDuration(durationStr string) (time.Duration, error)

ParseDuration parses a string into a time.Duration, assuming that a year always has 365d, a week always has 7d, and a day always has 24h.

func Path

func Path(keys ...string) string

Path returns a etcd key path.

func RandToken

func RandToken() string

RandToken generates a random 16-bit token

func ReachableTCP

func ReachableTCP(addr string) error

ReachableTCP checks an address is reachable via TCP.

func ReportFailureEtcdRequestCounter

func ReportFailureEtcdRequestCounter(clusterID, action, path string)

func RetryableError

func RetryableError(err error) bool

RetryableError determines that given error is retryable or not.

func RuntimeStats

func RuntimeStats() []string

RuntimeStats is used to return various runtime information

Types

type BasicAuthTransport

type BasicAuthTransport struct {
	Username string
	Password string
	// Transport is the underlying HTTP transport to use when making requests.
	// It will default to http.DefaultTransport if nil
	Transport http.RoundTripper
}

BasicAuthTransport is an http.RoundTripper that authenticates all requests using HTTP Basic Authentication with the provided username and password

func (*BasicAuthTransport) RoundTrip

func (t *BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

type Etcd

type Etcd struct {
	*etcdv3.Client

	ErrCh chan EtcdError
	// contains filtered or unexported fields
}

Etcd is the Etcd v3 client wrapper with addition context.

func NewEtcd

func NewEtcd(l log.Logger, ns string, cfg etcdv3.Config) (*Etcd, error)

NewEtcd constructs a new Etcd client

func (*Etcd) CheckKey

func (e *Etcd) CheckKey(key string) bool

CheckKey accepts a given Etcd key with format: then finds the key. Return true if one instance is found, otherwise false.

func (*Etcd) Context

func (e *Etcd) Context() (context.Context, context.CancelFunc)

Context returns a cancelable context and its cancel function.

func (*Etcd) DoDelete

func (e *Etcd) DoDelete(key string, opts ...etcdv3.OpOption) (*etcdv3.DeleteResponse, error)

DoDelete deletes a key, or optionally using WithRange(end), [key, end). More details please refer to etcd clientv3.KV interface.

func (*Etcd) DoGet

func (e *Etcd) DoGet(key string, opts ...etcdv3.OpOption) (*etcdv3.GetResponse, error)

DoGet retrieves keys. More details please refer to etcd clientv3.KV interface.

func (*Etcd) DoGrant

func (e *Etcd) DoGrant(ttl int64) (*etcdv3.LeaseGrantResponse, error)

DoGrant creates a new lease.

func (*Etcd) DoKeepAlive

func (e *Etcd) DoKeepAlive(id etcdv3.LeaseID) error

DoKeepAlive attempts to keep the given lease alive forever. If the keepalive responses posted to the channel are not consumed promptly the channel may become full. When full, the lease client will continue sending keep alive requests to the etcd server, but will drop responses until there is capacity on the channel to send more responses.

If client keep alive loop halts with an unexpected error (e.g. "etcdserver: no leader") or canceled by the caller (e.g. context.Canceled), KeepAlive returns a ErrKeepAliveHalted error containing the error reason.

The returned "LeaseKeepAliveResponse" channel closes if underlying keep alive stream is interrupted in some way the client cannot handle itself; given context "ctx" is canceled or timed out.

func (*Etcd) DoKeepAliveOnce

func (e *Etcd) DoKeepAliveOnce(id etcdv3.LeaseID) (*etcdv3.LeaseKeepAliveResponse, error)

DoKeepAliveOnce renews the lease once. The response corresponds to the first message from calling KeepAlive. If the response has a recoverable error, KeepAliveOnce will retry the RPC with a new keep alive message.

func (*Etcd) DoPut

func (e *Etcd) DoPut(key, val string, opts ...etcdv3.OpOption) (*etcdv3.PutResponse, error)

DoPut puts a key-value pair into etcd. More details please refer to etcd clientv3.KV interface.

func (*Etcd) DoRevoke

func (e *Etcd) DoRevoke(id etcdv3.LeaseID) (*etcdv3.LeaseRevokeResponse, error)

DoRevoke revokes the given lease.

func (*Etcd) DoSync

func (e *Etcd) DoSync() error

DoSync synchronizes client's endpoints with the known endpoints from etcd membership

func (*Etcd) LeaseContext

func (e *Etcd) LeaseContext() (context.Context, context.CancelFunc)

LeaseContext returns a cancelable context and its cancel function.

func (*Etcd) Run

func (e *Etcd) Run(stopc chan struct{})

Run waits for Etcd client's error.

func (*Etcd) WatchContext

func (e *Etcd) WatchContext() (context.Context, context.CancelFunc)

WatchContext wraps context with the WithRequireLeader If the context is "context.Background/TODO", returned "WatchChan" will not be closed and block until event is triggered, except when server returns a non-recoverable error (e.g. ErrCompacted). For example, when context passed with "WithRequireLeader" and the connected server has no leader (e.g. due to network partition), error "etcdserver: no leader" (ErrNoLeader) will be returned, and then "WatchChan" is closed with non-nil "Err()". In order to prevent a watch stream being stuck in a partitioned node, make sure to wrap context with "WithRequireLeader".

type EtcdError

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

EtcdError is the Etcd error wrapper

func NewEtcdErr

func NewEtcdErr(path, action string, err error) EtcdError

NewEtcdErr returns an Etcd error.

func (EtcdError) Error

func (ee EtcdError) Error() string

type FernetString added in v0.0.11

type FernetString struct {
	Token     string `json:"token,omitempty"`
	Encrypted bool   `json:"encrypted" default:"false"`
}

func (*FernetString) Decrypt added in v0.0.11

func (fs *FernetString) Decrypt() bool

func (*FernetString) Encrypt added in v0.0.11

func (fs *FernetString) Encrypt() (err error)

type Registry

type Registry struct {
	sync.RWMutex
	Items map[string]Worker
}

func (*Registry) Delete

func (r *Registry) Delete(key string)

func (*Registry) Get

func (r *Registry) Get(key string) (Worker, bool)

func (*Registry) Iter

func (r *Registry) Iter() <-chan RegistryItem

func (*Registry) Set

func (r *Registry) Set(key string, value Worker)

type RegistryItem

type RegistryItem struct {
	Name  string
	Value Worker
}

type Worker

type Worker interface {
	Stop()
}

Jump to

Keyboard shortcuts

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