common

package module
v0.0.0-...-8323f46 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2023 License: AGPL-3.0 Imports: 13 Imported by: 18

Documentation

Index

Constants

View Source
const MAX_RETRIES float64 = 4

Variables

View Source
var (
	ContextKeyTraceID          = ContextKey("request id")
	ContextKeyRequestStartTime = ContextKey("request start time")
	ContextKeyRemoteAddress    = ContextKey("request remote address")
	ContextKeyUser             = ContextKey("user")
)
View Source
var (
	ErrNotFound            = errors.New("not found")
	ErrValidation          = errors.New("validation")
	ErrRetryable           = errors.New("retryable")
	ErrNoNewBlocks         = errors.New("no new blocks")
	ErrNotDistributionTime = errors.New("not distribution time")
)

Functions

func Decode

func Decode[T any](reader io.Reader) (*T, error)

func FunctionRetrier

func FunctionRetrier[T any](ctx context.Context, fn func() (T, error)) (result T, err error)

Invoke a function with exponential backoff when a retryable error is encountered. If fn returns an err that is wrapped in common.ErrRetyable, fn will be retried with exponential backoff. This was originally implemented to solve getting rate limited by the alchemy APIs

See: https://docs.alchemy.com/alchemy/documentation/rate-limits#retries

func Unmarshal

func Unmarshal[T any](data []byte) (*T, error)

func ValidateField

func ValidateField(f interface{}, tag string) error

func ValidateStruct

func ValidateStruct(s interface{}) error

Types

type ContextKey

type ContextKey string

func (ContextKey) String

func (c ContextKey) String() string
type Header struct {
	Key   string
	Value string
}

type HttpClient

type HttpClient interface {
	Do(ctx context.Context, method string, url string, body io.Reader, options *HttpOptions) (*http.Response, error)
}

func NewHttpClient

func NewHttpClient(logger Logger) HttpClient

type HttpOptions

type HttpOptions struct {
	Headers []Header
}

type LogEvent

type LogEvent interface {
	Send()
	Msg(msg string)
	Msgf(msg string, v ...any)
	Err(err error) LogEvent
	Strs(strs []struct {
		Key   string
		Value string
	}) LogEvent
}

type Logger

type Logger interface {
	Debug(ctx context.Context) LogEvent
	Info(ctx context.Context) LogEvent
	Warn(ctx context.Context) LogEvent
	Error(ctx context.Context) LogEvent
}

func NewLogger

func NewLogger(settings Settings) Logger

type Settings

type Settings interface {
	Appname() string
	Hostname() string
	Env() string
	IsDev() bool
}

These are intended to be generic settings that every app is expected to implement. packages in common can safely expect to have access to these settings in their constructors.

func NewSettings

func NewSettings() Settings

type SigninMessage

type SigninMessage struct {
	Address string `json:"address"`
}

type Stream

type Stream = string
const (
	SigninStream Stream = "signin"
	VoteStream   Stream = "vote"
)

type VoteMessage

type VoteMessage struct {
	Id      int64     `json:"id"`
	Address string    `json:"address"`
	Type    VoteType  `json:"type"`
	Value   VoteValue `json:"value"`
	// the timestamp at which the vote was accepted
	// this is used to discard old votes that may be processed out of order
	UpdatedAt int64 `json:"updated_at"`
}

type VoteType

type VoteType string
const (
	ThreadVote  VoteType = "thread"
	CommentVote VoteType = "comment"
)

type VoteValue

type VoteValue string
const (
	Upvote   VoteValue = "upvote"
	Downvote VoteValue = "downvote"
	Unvote   VoteValue = "unvote"
)

Jump to

Keyboard shortcuts

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