utils

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ExecQueue

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

ExecQueue implements a queue that executes function calls in a single thread, in the same order as they have been queued.

func NewExecQueue

func NewExecQueue(capacity int) *ExecQueue

NewExecQueue creates a new execution Queue.

func (*ExecQueue) CanQueue

func (q *ExecQueue) CanQueue() bool

CanQueue returns true if more function calls can be added to the execution Queue.

func (*ExecQueue) Clear

func (q *ExecQueue) Clear()

Clear drops all queued functions.

func (*ExecQueue) Queue

func (q *ExecQueue) Queue(f func()) bool

Queue adds a function call to the execution Queue. Returns true if successful.

func (*ExecQueue) Quit

func (q *ExecQueue) Quit()

Quit stops the exec Queue.

Quit waits for the current execution to finish before returning.

type ExpirationFactor

type ExpirationFactor struct {
	Exp    uint64
	Factor float64
}

ExpirationFactor is calculated from logOffset. 1 <= Factor < 2 and Factor*2^Exp describes the multiplier applicable for additions and the divider for readouts. If logOffset changes slowly then it saves some expensive operations to not calculate them for each addition and readout but cache this intermediate form for some time. It is also useful for structures where multiple values are expired with the same Expirer.

func ExpFactor

func ExpFactor(logOffset Fixed64) ExpirationFactor

ExpFactor calculates ExpirationFactor based on logOffset

func (ExpirationFactor) Value

func (e ExpirationFactor) Value(base float64, exp uint64) float64

Value calculates the expired value based on a floating point base and integer power-of-2 exponent. This function should be used by multi-value expired structures.

type ExpiredValue

type ExpiredValue struct {
	Base, Exp uint64 // rlp encoding works by default
}

ExpiredValue is a scalar value that is continuously expired (decreased exponentially) based on the provided logarithmic expiration offset value.

The formula for value calculation is: base*2^(exp-logOffset). In order to simplify the calculation of ExpiredValue, its value is expressed in the form of an exponent with a base of 2.

Also here is a trick to reduce a lot of calculations. In theory, when a value X decays over time and then a new value Y is added, the final result should be X*2^(exp-logOffset)+Y. However it's very hard to represent in memory. So the trick is using the idea of inflation instead of exponential decay. At this moment the temporary value becomes: X*2^exp+Y*2^logOffset_1, apply the exponential decay when we actually want to calculate the value.

e.g. t0: V = 100 t1: add 30, inflationary value is: 100 + 30/0.3, 0.3 is the decay coefficient t2: get value, decay coefficient is 0.2 now, final result is: 200*0.2 = 40

func (*ExpiredValue) Add

func (e *ExpiredValue) Add(amount int64, logOffset Fixed64) int64

add adds a signed value at the given moment

func (*ExpiredValue) AddExp

func (e *ExpiredValue) AddExp(a ExpiredValue)

addExp adds another ExpiredValue

func (*ExpiredValue) IsZero

func (e *ExpiredValue) IsZero() bool

IsZero returns true if the value is zero

func (*ExpiredValue) SubExp

func (e *ExpiredValue) SubExp(a ExpiredValue)

subExp subtracts another ExpiredValue

func (ExpiredValue) Value

func (e ExpiredValue) Value(logOffset Fixed64) uint64

value calculates the value at the given moment.

type Expirer

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

Expirer changes logOffset with a linear rate which can be changed during operation. It is not thread safe, if access by multiple goroutines is needed then it should be encapsulated into a locked structure. Note that if neither SetRate nor SetLogOffset are used during operation then LogOffset is thread safe.

func (*Expirer) LogOffset

func (e *Expirer) LogOffset(now mclock.AbsTime) Fixed64

LogOffset returns the current logarithmic offset.

func (*Expirer) SetLogOffset

func (e *Expirer) SetLogOffset(now mclock.AbsTime, logOffset Fixed64)

SetLogOffset sets logOffset instantly.

func (*Expirer) SetRate

func (e *Expirer) SetRate(now mclock.AbsTime, rate float64)

SetRate changes the expiration rate which is the inverse of the time constant in nanoseconds.

type Fixed64

type Fixed64 int64

Fixed64 implements 64-bit fixed point arithmetic functions.

func Float64ToFixed64

func Float64ToFixed64(f float64) Fixed64

float64ToFixed64 converts float64 to Fixed64 format.

func Uint64ToFixed64

func Uint64ToFixed64(f uint64) Fixed64

Uint64ToFixed64 converts uint64 integer to Fixed64 format.

func (Fixed64) Fraction

func (f64 Fixed64) Fraction() Fixed64

fraction returns the fractional part of a Fixed64 value.

func (Fixed64) Pow2

func (f64 Fixed64) Pow2() float64

pow2Fixed returns the base 2 power of the fixed point value.

func (Fixed64) ToUint64

func (f64 Fixed64) ToUint64() uint64

toUint64 converts Fixed64 format to uint64.

type Limiter

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

Limiter protects a network request serving mechanism from denial-of-service attacks. It limits the total amount of resources used for serving requests while ensuring that the most valuable connections always have a reasonable chance of being served.

func NewLimiter

func NewLimiter(sumCostLimit uint) *Limiter

NewLimiter creates a new Limiter

func (*Limiter) Add

func (l *Limiter) Add(id enode.ID, address string, value float64, reqCost uint) chan chan struct{}

Add adds a new request to the node queue belonging to the given id. Value belongs to the requesting node. A higher value gives the request a higher chance of being served quickly in case of heavy load or a DDoS attack. Cost is a rough estimate of the serving cost of the request. A lower cost also gives the request a better chance.

func (*Limiter) Stop

func (l *Limiter) Stop()

Stop stops the processing loop. All queued and future requests are rejected.

type LinearExpiredValue

type LinearExpiredValue struct {
	Offset uint64         // The latest time offset
	Val    uint64         // The remaining value, can never be negative
	Rate   mclock.AbsTime `rlp:"-"` // Expiration rate(by nanosecond), will ignored by RLP
}

LinearExpiredValue is very similar with the expiredValue which the value will continuously expired. But the different part is it's expired linearly.

func (*LinearExpiredValue) Add

func (e *LinearExpiredValue) Add(amount int64, now mclock.AbsTime) uint64

add adds a signed value at the given moment. This function always has the assumption that the given timestamp shouldn't less than the recorded one.

func (LinearExpiredValue) Value

func (e LinearExpiredValue) Value(now mclock.AbsTime) uint64

value calculates the value at the given moment. This function always has the assumption that the given timestamp shouldn't less than the recorded one.

type UpdateTimer

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

func NewUpdateTimer

func NewUpdateTimer(clock mclock.Clock, threshold time.Duration) *UpdateTimer

func (*UpdateTimer) Update

func (t *UpdateTimer) Update(callback func(diff time.Duration) bool) bool

func (*UpdateTimer) UpdateAt

func (t *UpdateTimer) UpdateAt(at mclock.AbsTime, callback func(diff time.Duration) bool) bool

type ValueExpirer

type ValueExpirer interface {
	SetRate(now mclock.AbsTime, rate float64)
	SetLogOffset(now mclock.AbsTime, logOffset Fixed64)
	LogOffset(now mclock.AbsTime) Fixed64
}

ValueExpirer controls value expiration rate

type WeightFn

type WeightFn func(interface{}) uint64

type WeightedRandomSelect

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

WeightedRandomSelect is capable of weighted random selection from a set of items

func NewWeightedRandomSelect

func NewWeightedRandomSelect(wfn WeightFn) *WeightedRandomSelect

NewWeightedRandomSelect returns a new WeightedRandomSelect structure

func (*WeightedRandomSelect) Choose

func (w *WeightedRandomSelect) Choose() WrsItem

Choose randomly selects an item from the set, with a chance proportional to its current weight. If the weight of the chosen element has been decreased since the last stored value, returns it with a newWeight/oldWeight chance, otherwise just updates its weight and selects another one

func (*WeightedRandomSelect) IsEmpty

func (w *WeightedRandomSelect) IsEmpty() bool

IsEmpty returns true if the set is empty

func (*WeightedRandomSelect) Remove

func (w *WeightedRandomSelect) Remove(item WrsItem)

Remove removes an item from the set

func (*WeightedRandomSelect) Update

func (w *WeightedRandomSelect) Update(item WrsItem)

Update updates an item's weight, adds it if it was non-existent or removes it if the new weight is zero. Note that explicitly updating decreasing weights is not necessary.

type WrsItem

type WrsItem interface{}

Jump to

Keyboard shortcuts

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