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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-30 14:48:50 +00:00
|
|
|
// SetMeta sets adds meta-header to resp.
|
|
|
|
func (s *Service) SetMeta(resp util.ResponseMessage) {
|
2020-10-22 11:04:46 +00:00
|
|
|
meta := new(session.ResponseMetaHeader)
|
2022-12-30 14:48:50 +00:00
|
|
|
meta.SetVersion(&s.cfg.version)
|
2022-02-07 13:34:02 +00:00
|
|
|
meta.SetTTL(1) // FIXME: #1160 TTL must be calculated
|
2022-12-30 14:48:50 +00:00
|
|
|
meta.SetEpoch(s.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
|
|
|
|
}
|
|
|
|
}
|