frostfs-node/pkg/services/reputation/local/controller/controller.go
Dmitrii Stepanov 7ebbfa3358 [#212] reputationsvc: Resolve linters and rename
Resolved containedctx linters.
Renamed context structs and interfaces to more understandble names.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2023-04-07 15:35:57 +00:00

84 lines
2.2 KiB
Go

package trustcontroller
import (
"context"
"fmt"
"sync"
reputationrouter "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation/common/router"
)
// Prm groups the required parameters of the Controller's constructor.
//
// All values must comply with the requirements imposed on them.
// Passing incorrect parameter values will result in constructor
// failure (error or panic depending on the implementation).
type Prm struct {
// Iterator over the reputation values
// collected by the node locally.
//
// Must not be nil.
LocalTrustSource IteratorProvider
// Place of recording the local values of
// trust to other nodes.
//
// Must not be nil.
LocalTrustTarget *reputationrouter.Router
}
// Controller represents main handler for starting
// and interrupting the reporting local trust values.
//
// It binds the interfaces of the local value stores
// to the target storage points. Controller is abstracted
// from the internal storage device and the network location
// of the connecting components. At its core, it is a
// high-level start-stop trigger for reporting.
//
// For correct operation, the controller must be created
// using the constructor (New) based on the required parameters
// and optional components. After successful creation,
// the constructor is immediately ready to work through
// API of external control of calculations and data transfer.
type Controller struct {
prm Prm
opts *options
mtx sync.Mutex
mCtx map[uint64]context.CancelFunc
}
const invalidPrmValFmt = "invalid parameter %s (%T):%v"
func panicOnPrmValue(n string, v any) {
panic(fmt.Sprintf(invalidPrmValFmt, n, v, v))
}
// New creates a new instance of the Controller.
//
// Panics if at least one value of the parameters is invalid.
//
// The created Controller does not require additional
// initialization and is completely ready for work.
func New(prm Prm, opts ...Option) *Controller {
switch {
case prm.LocalTrustSource == nil:
panicOnPrmValue("LocalTrustSource", prm.LocalTrustSource)
case prm.LocalTrustTarget == nil:
panicOnPrmValue("LocalTrustTarget", prm.LocalTrustTarget)
}
o := defaultOpts()
for _, opt := range opts {
opt(o)
}
return &Controller{
prm: prm,
opts: o,
mCtx: make(map[uint64]context.CancelFunc),
}
}