Restructure modules (#1572)
* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
This commit is contained in:
118
base/utils/stablepool.go
Normal file
118
base/utils/stablepool.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package utils
|
||||
|
||||
import "sync"
|
||||
|
||||
// A StablePool is a drop-in replacement for sync.Pool that is slower, but
|
||||
// predictable.
|
||||
// A StablePool is a set of temporary objects that may be individually saved and
|
||||
// retrieved.
|
||||
//
|
||||
// In contrast to sync.Pool, items are not removed automatically. Every item
|
||||
// will be returned at some point. Items are returned in a FIFO manner in order
|
||||
// to evenly distribute usage of a set of items.
|
||||
//
|
||||
// A StablePool is safe for use by multiple goroutines simultaneously and must
|
||||
// not be copied after first use.
|
||||
type StablePool struct {
|
||||
lock sync.Mutex
|
||||
|
||||
pool []interface{}
|
||||
cnt int
|
||||
getIndex int
|
||||
putIndex int
|
||||
|
||||
// New optionally specifies a function to generate
|
||||
// a value when Get would otherwise return nil.
|
||||
// It may not be changed concurrently with calls to Get.
|
||||
New func() interface{}
|
||||
}
|
||||
|
||||
// Put adds x to the pool.
|
||||
func (p *StablePool) Put(x interface{}) {
|
||||
if x == nil {
|
||||
return
|
||||
}
|
||||
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
// check if pool is full (or unitialized)
|
||||
if p.cnt == len(p.pool) {
|
||||
p.pool = append(p.pool, x)
|
||||
p.cnt++
|
||||
p.putIndex = p.cnt
|
||||
return
|
||||
}
|
||||
|
||||
// correct putIndex
|
||||
p.putIndex %= len(p.pool)
|
||||
|
||||
// iterate the whole pool once to find a free spot
|
||||
stopAt := p.putIndex - 1
|
||||
for i := p.putIndex; i != stopAt; i = (i + 1) % len(p.pool) {
|
||||
if p.pool[i] == nil {
|
||||
p.pool[i] = x
|
||||
p.cnt++
|
||||
p.putIndex = i + 1
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the next item from the Pool, removes it from the Pool, and
|
||||
// returns it to the caller.
|
||||
// In contrast to sync.Pool, Get never ignores the pool.
|
||||
// Callers should not assume any relation between values passed to Put and
|
||||
// the values returned by Get.
|
||||
//
|
||||
// If Get would otherwise return nil and p.New is non-nil, Get returns
|
||||
// the result of calling p.New.
|
||||
func (p *StablePool) Get() interface{} {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
// check if pool is empty
|
||||
if p.cnt == 0 {
|
||||
if p.New != nil {
|
||||
return p.New()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// correct getIndex
|
||||
p.getIndex %= len(p.pool)
|
||||
|
||||
// iterate the whole pool to find an item
|
||||
stopAt := p.getIndex - 1
|
||||
for i := p.getIndex; i != stopAt; i = (i + 1) % len(p.pool) {
|
||||
if p.pool[i] != nil {
|
||||
x := p.pool[i]
|
||||
p.pool[i] = nil
|
||||
p.cnt--
|
||||
p.getIndex = i + 1
|
||||
return x
|
||||
}
|
||||
}
|
||||
|
||||
// if we ever get here, return a new item
|
||||
if p.New != nil {
|
||||
return p.New()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Size returns the amount of items the pool currently holds.
|
||||
func (p *StablePool) Size() int {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
return p.cnt
|
||||
}
|
||||
|
||||
// Max returns the amount of items the pool held at maximum.
|
||||
func (p *StablePool) Max() int {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
return len(p.pool)
|
||||
}
|
||||
Reference in New Issue
Block a user