From 309119bcbd1b666078c80b1ff663d9f9c82f87f6 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Wed, 23 Oct 2024 08:33:24 +0400 Subject: [PATCH] handler: Update NetworkInfo cache on GetNetworkInfo request Signed-off-by: Evgenii Baidakov --- handlers/api.go | 1 + handlers/network.go | 2 ++ internal/cache/networkinfo.go | 12 +++++++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/handlers/api.go b/handlers/api.go index 55b915f..dea19ea 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -49,6 +49,7 @@ type SessionToken struct { type networkInfoGetter interface { NetworkInfo(ctx context.Context) (netmap.NetworkInfo, error) + StoreNetworkInfo(ni netmap.NetworkInfo) } const ( diff --git a/handlers/network.go b/handlers/network.go index 2c57a64..e3c26fd 100644 --- a/handlers/network.go +++ b/handlers/network.go @@ -17,6 +17,8 @@ func (a *RestAPI) GetNetworkInfo(ctx echo.Context) error { return ctx.JSON(http.StatusBadRequest, resp) } + a.networkInfoGetter.StoreNetworkInfo(networkInfo) + var resp apiserver.NetworkInfoOK resp.AuditFee = networkInfo.AuditFee() resp.StoragePrice = networkInfo.StoragePrice() diff --git a/internal/cache/networkinfo.go b/internal/cache/networkinfo.go index daa9161..b367ac3 100644 --- a/internal/cache/networkinfo.go +++ b/internal/cache/networkinfo.go @@ -43,9 +43,19 @@ func (n *NetworkInfo) NetworkInfo(ctx context.Context) (netmap.NetworkInfo, erro return netmap.NetworkInfo{}, fmt.Errorf("get network info: %w", err) } + n.update(ni) + + return ni, nil +} + +func (n *NetworkInfo) update(ni netmap.NetworkInfo) { n.ttl = time.Duration(int64(ni.EpochDuration())/2*ni.MsPerBlock()) * time.Millisecond n.validUntil = time.Now().Add(n.ttl) n.ni = ni +} - return ni, nil +func (n *NetworkInfo) StoreNetworkInfo(ni netmap.NetworkInfo) { + n.mu.Lock() + n.update(ni) + n.mu.Unlock() }