{"modulePath":"github.com/Evolution404/simcore","version":"v1.0.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package utils\n\n## Types\n\n```go\ntype ExecQueue struct {\n\t// contains filtered or unexported fields\n}\n```\nExecQueue implements a queue that executes function calls in a single thread, in the same order as they have been queued.\n\n```go\nfunc NewExecQueue(capacity int) *ExecQueue\n```\nNewExecQueue creates a new execution Queue.\n\n```go\nfunc (q *ExecQueue) CanQueue() bool\n```\nCanQueue returns true if more function calls can be added to the execution Queue.\n\n```go\nfunc (q *ExecQueue) Clear()\n```\nClear drops all queued functions.\n\n```go\nfunc (q *ExecQueue) Queue(f func()) bool\n```\nQueue adds a function call to the execution Queue. Returns true if successful.\n\n```go\nfunc (q *ExecQueue) Quit()\n```\nQuit stops the exec Queue.\n\nQuit waits for the current execution to finish before returning.\n\n```go\ntype ExpirationFactor struct {\n\tExp    uint64\n\tFactor float64\n}\n```\nExpirationFactor is calculated from logOffset. 1 \\\u003c= Factor \\\u003c 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.\n\n```go\nfunc ExpFactor(logOffset Fixed64) ExpirationFactor\n```\nExpFactor calculates ExpirationFactor based on logOffset\n\n```go\nfunc (e ExpirationFactor) Value(base float64, exp uint64) float64\n```\nValue 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.\n\n```go\ntype ExpiredValue struct {\n\tBase, Exp uint64 // rlp encoding works by default\n}\n```\nExpiredValue is a scalar value that is continuously expired (decreased exponentially) based on the provided logarithmic expiration offset value.\n\nThe 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.\n\nAlso 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.\n\ne.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\n\n```go\nfunc (e *ExpiredValue) Add(amount int64, logOffset Fixed64) int64\n```\nadd adds a signed value at the given moment\n\n```go\nfunc (e *ExpiredValue) AddExp(a ExpiredValue)\n```\naddExp adds another ExpiredValue\n\n```go\nfunc (e *ExpiredValue) IsZero() bool\n```\nIsZero returns true if the value is zero\n\n```go\nfunc (e *ExpiredValue) SubExp(a ExpiredValue)\n```\nsubExp subtracts another ExpiredValue\n\n```go\nfunc (e ExpiredValue) Value(logOffset Fixed64) uint64\n```\nvalue calculates the value at the given moment.\n\n```go\ntype Expirer struct {\n\t// contains filtered or unexported fields\n}\n```\nExpirer 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.\n\n```go\nfunc (e *Expirer) LogOffset(now mclock.AbsTime) Fixed64\n```\nLogOffset returns the current logarithmic offset.\n\n```go\nfunc (e *Expirer) SetLogOffset(now mclock.AbsTime, logOffset Fixed64)\n```\nSetLogOffset sets logOffset instantly.\n\n```go\nfunc (e *Expirer) SetRate(now mclock.AbsTime, rate float64)\n```\nSetRate changes the expiration rate which is the inverse of the time constant in nanoseconds.\n\n```go\ntype Fixed64 int64\n```\nFixed64 implements 64-bit fixed point arithmetic functions.\n\n```go\nfunc Float64ToFixed64(f float64) Fixed64\n```\nfloat64ToFixed64 converts float64 to Fixed64 format.\n\n```go\nfunc Uint64ToFixed64(f uint64) Fixed64\n```\nUint64ToFixed64 converts uint64 integer to Fixed64 format.\n\n```go\nfunc (f64 Fixed64) Fraction() Fixed64\n```\nfraction returns the fractional part of a Fixed64 value.\n\n```go\nfunc (f64 Fixed64) Pow2() float64\n```\npow2Fixed returns the base 2 power of the fixed point value.\n\n```go\nfunc (f64 Fixed64) ToUint64() uint64\n```\ntoUint64 converts Fixed64 format to uint64.\n\n```go\ntype Limiter struct {\n\t// contains filtered or unexported fields\n}\n```\nLimiter 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.\n\n```go\nfunc NewLimiter(sumCostLimit uint) *Limiter\n```\nNewLimiter creates a new Limiter\n\n```go\nfunc (l *Limiter) Add(id enode.ID, address string, value float64, reqCost uint) chan chan struct{}\n```\nAdd 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.\n\n```go\nfunc (l *Limiter) Stop()\n```\nStop stops the processing loop. All queued and future requests are rejected.\n\n```go\ntype LinearExpiredValue struct {\n\tOffset uint64         // The latest time offset\n\tVal    uint64         // The remaining value, can never be negative\n\tRate   mclock.AbsTime `rlp:\"-\"` // Expiration rate(by nanosecond), will ignored by RLP\n}\n```\nLinearExpiredValue is very similar with the expiredValue which the value will continuously expired. But the different part is it's expired linearly.\n\n```go\nfunc (e *LinearExpiredValue) Add(amount int64, now mclock.AbsTime) uint64\n```\nadd 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.\n\n```go\nfunc (e LinearExpiredValue) Value(now mclock.AbsTime) uint64\n```\nvalue calculates the value at the given moment. This function always has the assumption that the given timestamp shouldn't less than the recorded one.\n\n```go\ntype UpdateTimer struct {\n\t// contains filtered or unexported fields\n}\n```\n\n```go\nfunc NewUpdateTimer(clock mclock.Clock, threshold time.Duration) *UpdateTimer\n```\n\n```go\nfunc (t *UpdateTimer) Update(callback func(diff time.Duration) bool) bool\n```\n\n```go\nfunc (t *UpdateTimer) UpdateAt(at mclock.AbsTime, callback func(diff time.Duration) bool) bool\n```\n\n```go\ntype ValueExpirer interface {\n\tSetRate(now mclock.AbsTime, rate float64)\n\tSetLogOffset(now mclock.AbsTime, logOffset Fixed64)\n\tLogOffset(now mclock.AbsTime) Fixed64\n}\n```\nValueExpirer controls value expiration rate\n\n```go\ntype WeightFn func(interface{}) uint64\n```\n\n```go\ntype WeightedRandomSelect struct {\n\t// contains filtered or unexported fields\n}\n```\nWeightedRandomSelect is capable of weighted random selection from a set of items\n\n```go\nfunc NewWeightedRandomSelect(wfn WeightFn) *WeightedRandomSelect\n```\nNewWeightedRandomSelect returns a new WeightedRandomSelect structure\n\n```go\nfunc (w *WeightedRandomSelect) Choose() WrsItem\n```\nChoose 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\n\n```go\nfunc (w *WeightedRandomSelect) IsEmpty() bool\n```\nIsEmpty returns true if the set is empty\n\n```go\nfunc (w *WeightedRandomSelect) Remove(item WrsItem)\n```\nRemove removes an item from the set\n\n```go\nfunc (w *WeightedRandomSelect) Update(item WrsItem)\n```\nUpdate 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.\n\n```go\ntype WrsItem interface{}\n```\n\n","path":"github.com/Evolution404/simcore/les/utils","name":"utils","synopsis":"","isRedistributable":true}
