2020-10-22 11:04:46 +00:00
|
|
|
package response
|
|
|
|
|
|
|
|
import (
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/services/util"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/version"
|
2020-10-22 11:04:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service represents universal v2 service
|
|
|
|
// that sets response meta header values.
|
|
|
|
type Service struct {
|
|
|
|
cfg *cfg
|
|
|
|
}
|
|
|
|
|
|
|
|
// Option is an option of Service constructor.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
|
|
|
type cfg struct {
|
2022-05-11 14:58:52 +00:00
|
|
|
version refs.Version
|
2020-10-22 11:04:46 +00:00
|
|
|
|
2020-10-22 11:22:23 +00:00
|
|
|
state netmap.State
|
2020-10-22 11:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
2022-05-11 14:58:52 +00:00
|
|
|
var c cfg
|
|
|
|
|
|
|
|
version.Current().WriteToV2(&c.version)
|
|
|
|
|
|
|
|
return &c
|
2020-10-22 11:04:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates, initializes and returns Service instance.
|
|
|
|
func NewService(opts ...Option) *Service {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
cfg: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setMeta(resp util.ResponseMessage, cfg *cfg) {
|
|
|
|
meta := new(session.ResponseMetaHeader)
|
2022-05-11 14:58:52 +00:00
|
|
|
meta.SetVersion(&cfg.version)
|
2022-02-07 13:34:02 +00:00
|
|
|
meta.SetTTL(1) // FIXME: #1160 TTL must be calculated
|
2020-10-22 11:22:23 +00:00
|
|
|
meta.SetEpoch(cfg.state.CurrentEpoch())
|
2020-10-22 11:04:46 +00:00
|
|
|
|
|
|
|
if origin := resp.GetMetaHeader(); origin != nil {
|
2022-02-07 13:34:02 +00:00
|
|
|
// FIXME: #1160 what if origin is set by local server?
|
2020-10-22 11:04:46 +00:00
|
|
|
meta.SetOrigin(origin)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.SetMetaHeader(meta)
|
|
|
|
}
|
2020-10-22 11:22:23 +00:00
|
|
|
|
|
|
|
// WithNetworkState returns option to set network state of Service.
|
|
|
|
func WithNetworkState(v netmap.State) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.state = v
|
|
|
|
}
|
|
|
|
}
|