utils

package
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const AcceptableFailCount = 10

AcceptableFailCount describes how many fails would be tolerated until the server refuses to serve any more data

View Source
const (
	// BaseEndpoint is the endpoint for all of the data
	BaseEndpoint = "https://penguin-stats.io/PenguinStats/api/v2/"
)
View Source
const DefaultTimeout = time.Second * 20

DefaultTimeout describes the default timeout for the underlying http.Client that the updater uses

Variables

View Source
var (
	// ErrCacheNotFound describes a cache that cannot found with the server provided
	ErrCacheNotFound = errors.New("CacheNotFound", "cache not found with the server provided", errors.BlameUser)
)

Functions

func NewLogger

func NewLogger(name string) *logrus.Entry

NewLogger creates a new logrus.Entry with name provided

func NewPenguinRequest

func NewPenguinRequest(resource string, params *url.Values) (*http.Request, error)

NewPenguinRequest creates a http.Request that parses resource as relative path against BaseEndpoint to produce path, and appends server to the URL that is being generated

func NewRetriedOperation

func NewRetriedOperation(retryableFunc retry.RetryableFunc) error

NewRetriedOperation executes a retry.RetryableFunc with custom default retry parameters. Returns error if all retry attempts failed

func UnmarshalFromReader

func UnmarshalFromReader(reader io.Reader, v interface{}) error

UnmarshalFromReader unmarshals all data from reader and populates it to v

func ValidServer

func ValidServer(server string) bool

ValidServer validates if server counts as a valid server

Types

type Cache

type Cache struct {

	// Updated is to represent the last updated time
	Updated *time.Time

	// FailCount is the counter for how many times does the cache failed to update (after retries)
	FailCount int

	// == instance ==
	// Name is the name of this cache instance
	Name string

	// Server is the server of this cache instance
	Server string

	// Interval is the update interval of cache
	Interval time.Duration

	// Client is the http.Client
	Client *http.Client

	// update is the function to update of underlying cache instance
	Updater Updater

	// Logger is the logger of such instance
	Logger *logrus.Entry
	// contains filtered or unexported fields
}

Cache is the thread-safe cache instance that automatically updates data every Interval with Updater

func FindServerCache

func FindServerCache(caches []*Cache, server string) (*Cache, *errors.Error)

FindServerCache finds Cache that belongs to the specified server. Returns ErrCacheNotFound if not found.

func NewCache

func NewCache(config CacheConfig) *Cache

NewCache creates a new Cache with CacheConfig provided

func (*Cache) Content

func (c *Cache) Content() interface{}

Content retrieves the cache content with proper mutex handling

func (*Cache) Ready

func (c *Cache) Ready() bool

Ready indicates whether the cache is ready to serve valid data. Notice that if the existing data have exceeded the FailCount limit, this check would also fail

func (*Cache) Update

func (c *Cache) Update() error

Update calls the underlying cache's Updater and updates cache status accordingly

type CacheConfig

type CacheConfig struct {
	// Name is the name of this cache instance
	Name string

	// Server is the server of this cache instance
	Server string

	// Interval is the update Interval of cache
	Interval time.Duration

	// Updater is the function to update of underlying cache instance
	Updater Updater
}

CacheConfig describes the configuration to provide when initializing a cache instance

type UniString

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

UniString contains only unique strings which uses a underlying map which provides a constant time complexity

func NewUniString

func NewUniString() *UniString

NewUniString creates a new UniString instance that contains only unique strings

func (*UniString) Add

func (u *UniString) Add(key string)

Add adds the string to the UniString instance only when there is no such string

func (*UniString) Slice

func (u *UniString) Slice() []string

Slice returns a string slice from UniString instance

type Updater

type Updater func(instance *Cache) (interface{}, error)

Updater describes a func type which will be used to call and get data update from

Jump to

Keyboard shortcuts

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