[#217] containersvc: Resolve containedctx linter

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-04-05 16:28:09 +03:00 committed by Gitea
parent 279261ace3
commit 206458c841
4 changed files with 43 additions and 47 deletions

View file

@ -39,7 +39,7 @@ const (
stopEstimationNotifyEvent = "StopEstimation" stopEstimationNotifyEvent = "StopEstimation"
) )
func initContainerService(c *cfg) { func initContainerService(ctx context.Context, c *cfg) {
// container wrapper that tries to invoke notary // container wrapper that tries to invoke notary
// requests if chain is configured so // requests if chain is configured so
wrap, err := cntClient.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0, cntClient.TryNotary()) wrap, err := cntClient.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0, cntClient.TryNotary())
@ -77,7 +77,7 @@ func initContainerService(c *cfg) {
loadroute.WithLogger(c.log), loadroute.WithLogger(c.log),
) )
setLoadController(c, loadRouter, loadAccumulator) setLoadController(ctx, c, loadRouter, loadAccumulator)
server := containerTransportGRPC.New( server := containerTransportGRPC.New(
containerService.NewSignService( containerService.NewSignService(
@ -180,7 +180,7 @@ func configureEACLAndContainerSources(c *cfg, client *cntClient.Client, cnrSrc c
return cnrRdr, cnrWrt return cnrRdr, cnrWrt
} }
func setLoadController(c *cfg, loadRouter *loadroute.Router, loadAccumulator *loadstorage.Storage) { func setLoadController(ctx context.Context, c *cfg, loadRouter *loadroute.Router, loadAccumulator *loadstorage.Storage) {
pubKey := c.key.PublicKey().Bytes() pubKey := c.key.PublicKey().Bytes()
// container wrapper that always sends non-notary // container wrapper that always sends non-notary
@ -211,14 +211,14 @@ func setLoadController(c *cfg, loadRouter *loadroute.Router, loadAccumulator *lo
setContainerNotificationParser(c, startEstimationNotifyEvent, containerEvent.ParseStartEstimation) setContainerNotificationParser(c, startEstimationNotifyEvent, containerEvent.ParseStartEstimation)
addContainerAsyncNotificationHandler(c, startEstimationNotifyEvent, func(ev event.Event) { addContainerAsyncNotificationHandler(c, startEstimationNotifyEvent, func(ev event.Event) {
ctrl.Start(loadcontroller.StartPrm{ ctrl.Start(ctx, loadcontroller.StartPrm{
Epoch: ev.(containerEvent.StartEstimation).Epoch(), Epoch: ev.(containerEvent.StartEstimation).Epoch(),
}) })
}) })
setContainerNotificationParser(c, stopEstimationNotifyEvent, containerEvent.ParseStopEstimation) setContainerNotificationParser(c, stopEstimationNotifyEvent, containerEvent.ParseStopEstimation)
addContainerAsyncNotificationHandler(c, stopEstimationNotifyEvent, func(ev event.Event) { addContainerAsyncNotificationHandler(c, stopEstimationNotifyEvent, func(ev event.Event) {
ctrl.Stop(loadcontroller.StopPrm{ ctrl.Stop(ctx, loadcontroller.StopPrm{
Epoch: ev.(containerEvent.StopEstimation).Epoch(), Epoch: ev.(containerEvent.StopEstimation).Epoch(),
}) })
}) })

View file

@ -97,7 +97,7 @@ func initApp(ctx context.Context, c *cfg) {
initAndLog(c, "gRPC", initGRPC) initAndLog(c, "gRPC", initGRPC)
initAndLog(c, "netmap", initNetmapService) initAndLog(c, "netmap", initNetmapService)
initAndLog(c, "accounting", initAccountingService) initAndLog(c, "accounting", initAccountingService)
initAndLog(c, "container", initContainerService) initAndLog(c, "container", func(c *cfg) { initContainerService(ctx, c) })
initAndLog(c, "session", initSessionService) initAndLog(c, "session", initSessionService)
initAndLog(c, "reputation", initReputationService) initAndLog(c, "reputation", initReputationService)
initAndLog(c, "notification", initNotifications) initAndLog(c, "notification", initNotifications)

View file

@ -15,18 +15,15 @@ type StartPrm struct {
Epoch uint64 Epoch uint64
} }
// nolint: containedctx
type commonContext struct { type commonContext struct {
epoch uint64 epoch uint64
ctrl *Controller ctrl *Controller
log *logger.Logger log *logger.Logger
ctx context.Context
} }
type announceContext struct { type announcer struct {
commonContext commonContext
} }
@ -39,21 +36,22 @@ type announceContext struct {
// //
// Each call acquires an announcement context for an Epoch parameter. // Each call acquires an announcement context for an Epoch parameter.
// At the very end of the operation, the context is released. // At the very end of the operation, the context is released.
func (c *Controller) Start(prm StartPrm) { func (c *Controller) Start(ctx context.Context, prm StartPrm) {
var announcer *announcer
// acquire announcement // acquire announcement
execCtx := c.acquireAnnouncement(prm) ctx, announcer = c.acquireAnnouncement(ctx, prm)
if execCtx == nil { if announcer == nil {
return return
} }
// finally stop and free the announcement // finally stop and free the announcement
defer execCtx.freeAnnouncement() defer announcer.freeAnnouncement()
// announce local values // announce local values
execCtx.announce() announcer.announce(ctx)
} }
func (c *announceContext) announce() { func (c *announcer) announce(ctx context.Context) {
c.log.Debug("starting to announce the values of the metrics") c.log.Debug("starting to announce the values of the metrics")
var ( var (
@ -100,7 +98,7 @@ func (c *announceContext) announce() {
} }
// finish writing // finish writing
err = targetWriter.Close(c.ctx) err = targetWriter.Close(ctx)
if err != nil { if err != nil {
c.log.Debug("could not finish writing local announcements", c.log.Debug("could not finish writing local announcements",
zap.String("error", err.Error()), zap.String("error", err.Error()),
@ -112,35 +110,32 @@ func (c *announceContext) announce() {
c.log.Debug("trust announcement successfully finished") c.log.Debug("trust announcement successfully finished")
} }
func (c *Controller) acquireAnnouncement(prm StartPrm) *announceContext { func (c *Controller) acquireAnnouncement(ctx context.Context, prm StartPrm) (context.Context, *announcer) {
var ctx context.Context started := true
c.announceMtx.Lock() c.announceMtx.Lock()
{ {
if cancel := c.mAnnounceCtx[prm.Epoch]; cancel == nil { if cancel := c.mAnnounceCtx[prm.Epoch]; cancel == nil {
ctx, cancel = context.WithCancel(context.Background()) ctx, cancel = context.WithCancel(ctx)
c.mAnnounceCtx[prm.Epoch] = cancel c.mAnnounceCtx[prm.Epoch] = cancel
started = false
} }
} }
c.announceMtx.Unlock() c.announceMtx.Unlock()
log := &logger.Logger{Logger: c.opts.log.With( log := &logger.Logger{Logger: c.opts.log.With(
zap.Uint64("epoch", prm.Epoch), zap.Uint64("epoch", prm.Epoch),
)} )}
if ctx == nil { if started {
log.Debug("announcement is already started") log.Debug("announcement is already started")
return nil return ctx, nil
} }
return &announceContext{ return ctx, &announcer{
commonContext: commonContext{ commonContext: commonContext{
epoch: prm.Epoch, epoch: prm.Epoch,
ctrl: c, ctrl: c,
log: log, log: log,
ctx: ctx,
}, },
} }
} }
@ -176,7 +171,7 @@ type StopPrm struct {
Epoch uint64 Epoch uint64
} }
type stopContext struct { type reporter struct {
commonContext commonContext
} }
@ -188,31 +183,32 @@ type stopContext struct {
// //
// Each call acquires a report context for an Epoch parameter. // Each call acquires a report context for an Epoch parameter.
// At the very end of the operation, the context is released. // At the very end of the operation, the context is released.
func (c *Controller) Stop(prm StopPrm) { func (c *Controller) Stop(ctx context.Context, prm StopPrm) {
execCtx := c.acquireReport(prm) var reporter *reporter
if execCtx == nil { ctx, reporter = c.acquireReport(ctx, prm)
if reporter == nil {
return return
} }
// finally stop and free reporting // finally stop and free reporting
defer execCtx.freeReport() defer reporter.freeReport()
// interrupt announcement // interrupt announcement
execCtx.freeAnnouncement() reporter.freeAnnouncement()
// report the estimations // report the estimations
execCtx.report() reporter.report(ctx)
} }
func (c *Controller) acquireReport(prm StopPrm) *stopContext { func (c *Controller) acquireReport(ctx context.Context, prm StopPrm) (context.Context, *reporter) {
var ctx context.Context started := true
c.reportMtx.Lock() c.reportMtx.Lock()
{ {
if cancel := c.mReportCtx[prm.Epoch]; cancel == nil { if cancel := c.mReportCtx[prm.Epoch]; cancel == nil {
ctx, cancel = context.WithCancel(context.Background()) ctx, cancel = context.WithCancel(ctx)
c.mReportCtx[prm.Epoch] = cancel c.mReportCtx[prm.Epoch] = cancel
started = false
} }
} }
@ -222,12 +218,12 @@ func (c *Controller) acquireReport(prm StopPrm) *stopContext {
zap.Uint64("epoch", prm.Epoch), zap.Uint64("epoch", prm.Epoch),
)} )}
if ctx == nil { if started {
log.Debug("report is already started") log.Debug("report is already started")
return nil return ctx, nil
} }
return &stopContext{ return ctx, &reporter{
commonContext: commonContext{ commonContext: commonContext{
epoch: prm.Epoch, epoch: prm.Epoch,
ctrl: c, ctrl: c,
@ -261,7 +257,7 @@ func (c *commonContext) freeReport() {
} }
} }
func (c *stopContext) report() { func (c *reporter) report(ctx context.Context) {
var ( var (
localIterator Iterator localIterator Iterator
err error err error
@ -301,7 +297,7 @@ func (c *stopContext) report() {
} }
// finish writing // finish writing
err = resultWriter.Close(c.ctx) err = resultWriter.Close(ctx)
if err != nil { if err != nil {
c.log.Debug("could not finish writing load estimations", c.log.Debug("could not finish writing load estimations",
zap.String("error", err.Error()), zap.String("error", err.Error()),

View file

@ -143,12 +143,12 @@ func TestSimpleScenario(t *testing.T) {
// start both controllers // start both controllers
go func() { go func() {
ctrlN1.Start(startPrm) ctrlN1.Start(context.Background(), startPrm)
wg.Done() wg.Done()
}() }()
go func() { go func() {
ctrlN2.Start(startPrm) ctrlN2.Start(context.Background(), startPrm)
wg.Done() wg.Done()
}() }()
@ -161,12 +161,12 @@ func TestSimpleScenario(t *testing.T) {
// stop both controllers // stop both controllers
go func() { go func() {
ctrlN1.Stop(stopPrm) ctrlN1.Stop(context.Background(), stopPrm)
wg.Done() wg.Done()
}() }()
go func() { go func() {
ctrlN2.Stop(stopPrm) ctrlN2.Stop(context.Background(), stopPrm)
wg.Done() wg.Done()
}() }()