Skip to content

Commit

Permalink
handler: Update NetworkInfo cache on GetNetworkInfo request
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <[email protected]>
  • Loading branch information
smallhive committed Oct 23, 2024
1 parent 262e100 commit 309119b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type SessionToken struct {

type networkInfoGetter interface {
NetworkInfo(ctx context.Context) (netmap.NetworkInfo, error)
StoreNetworkInfo(ni netmap.NetworkInfo)
}

const (
Expand Down
2 changes: 2 additions & 0 deletions handlers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func (a *RestAPI) GetNetworkInfo(ctx echo.Context) error {
return ctx.JSON(http.StatusBadRequest, resp)
}

a.networkInfoGetter.StoreNetworkInfo(networkInfo)

Check warning on line 21 in handlers/network.go

View check run for this annotation

Codecov / codecov/patch

handlers/network.go#L20-L21

Added lines #L20 - L21 were not covered by tests
var resp apiserver.NetworkInfoOK
resp.AuditFee = networkInfo.AuditFee()
resp.StoragePrice = networkInfo.StoragePrice()
Expand Down
12 changes: 11 additions & 1 deletion internal/cache/networkinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,19 @@ func (n *NetworkInfo) NetworkInfo(ctx context.Context) (netmap.NetworkInfo, erro
return netmap.NetworkInfo{}, fmt.Errorf("get network info: %w", err)
}

Check warning on line 44 in internal/cache/networkinfo.go

View check run for this annotation

Codecov / codecov/patch

internal/cache/networkinfo.go#L43-L44

Added lines #L43 - L44 were not covered by tests

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()

Check warning on line 60 in internal/cache/networkinfo.go

View check run for this annotation

Codecov / codecov/patch

internal/cache/networkinfo.go#L57-L60

Added lines #L57 - L60 were not covered by tests
}

0 comments on commit 309119b

Please sign in to comment.