2022-04-20 09:17:20 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-16 08:40:08 +00:00
|
|
|
"fmt"
|
2022-04-20 09:17:20 +00:00
|
|
|
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-16 08:40:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
2023-05-30 14:01:20 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2022-04-20 09:17:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetContainerID decode container id, if it's not a valid container id
|
|
|
|
// then trey to resolve name using provided resolver.
|
|
|
|
func GetContainerID(ctx context.Context, containerID string, resolver *resolver.ContainerResolver) (*cid.ID, error) {
|
|
|
|
cnrID := new(cid.ID)
|
|
|
|
err := cnrID.DecodeString(containerID)
|
|
|
|
if err != nil {
|
2022-09-08 16:00:22 +00:00
|
|
|
cnrID, err = resolver.Resolve(ctx, containerID)
|
2022-04-20 09:17:20 +00:00
|
|
|
}
|
|
|
|
return cnrID, err
|
|
|
|
}
|
2023-03-16 08:40:08 +00:00
|
|
|
|
|
|
|
type EpochDurations struct {
|
|
|
|
CurrentEpoch uint64
|
|
|
|
MsPerBlock int64
|
|
|
|
BlockPerEpoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEpochDurations(ctx context.Context, p *pool.Pool) (*EpochDurations, error) {
|
|
|
|
networkInfo, err := p.NetworkInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := &EpochDurations{
|
|
|
|
CurrentEpoch: networkInfo.CurrentEpoch(),
|
|
|
|
MsPerBlock: networkInfo.MsPerBlock(),
|
|
|
|
BlockPerEpoch: networkInfo.EpochDuration(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.BlockPerEpoch == 0 {
|
|
|
|
return nil, fmt.Errorf("EpochDuration is empty")
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
2023-05-30 14:01:20 +00:00
|
|
|
|
|
|
|
// SetContextToRequest adds new context to fasthttp request.
|
|
|
|
func SetContextToRequest(ctx context.Context, c *fasthttp.RequestCtx) {
|
|
|
|
c.SetUserValue("context", ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContextFromRequest returns main context from fasthttp request context.
|
|
|
|
func GetContextFromRequest(c *fasthttp.RequestCtx) context.Context {
|
|
|
|
return c.UserValue("context").(context.Context)
|
|
|
|
}
|