-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Evgenii Baidakov <[email protected]>
- Loading branch information
Showing
7 changed files
with
162 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package neofs | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/nspcc-dev/neofs-sdk-go/client" | ||
"github.com/nspcc-dev/neofs-sdk-go/netmap" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type ( | ||
// NetworkInfoGetter represents provider to get actual [netmap.NetworkInfo]. | ||
NetworkInfoGetter interface { | ||
NetworkInfo(ctx context.Context, prm client.PrmNetworkInfo) (netmap.NetworkInfo, error) | ||
} | ||
|
||
// EpochGetter represents provider to get actual NeoFS epoch. | ||
EpochGetter interface { | ||
CurrentEpoch() uint64 | ||
} | ||
|
||
// PeriodicGetter implements [EpochGetter]. | ||
PeriodicGetter struct { | ||
logger *zap.Logger | ||
netGetter NetworkInfoGetter | ||
epoch atomic.Uint64 | ||
timeOut time.Duration | ||
} | ||
) | ||
|
||
// NewPeriodicGetter is a constructor to [PeriodicGetter]. | ||
func NewPeriodicGetter(ctx context.Context, initialEpoch uint64, timeOut time.Duration, netGetter NetworkInfoGetter, logger *zap.Logger) *PeriodicGetter { | ||
getter := &PeriodicGetter{ | ||
timeOut: timeOut, | ||
netGetter: netGetter, | ||
logger: logger, | ||
} | ||
|
||
getter.epoch.Store(initialEpoch) | ||
|
||
go getter.update(ctx) | ||
|
||
return getter | ||
} | ||
|
||
// CurrentEpoch returns actual epoch. | ||
// | ||
// CurrentEpoch implements [EpochGetter]. | ||
func (g *PeriodicGetter) CurrentEpoch() uint64 { | ||
return g.epoch.Load() | ||
} | ||
|
||
func (g *PeriodicGetter) update(ctx context.Context) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-time.After(g.timeOut): | ||
ni, err := g.netGetter.NetworkInfo(ctx, client.PrmNetworkInfo{}) | ||
if err != nil { | ||
g.logger.Error("periodicGetter: networkInfo", zap.Error(err)) | ||
continue | ||
} | ||
|
||
g.logger.Info("periodicGetter", zap.Uint64("epoch", ni.CurrentEpoch())) | ||
g.epoch.Store(ni.CurrentEpoch()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters