any-sync/common/nodeconf/configuration.go
2022-10-20 20:14:27 +03:00

50 lines
1.1 KiB
Go

//go:generate mockgen -destination mock_nodeconf/mock_nodeconf.go github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf Service,Configuration,ConfConnector
package nodeconf
import (
"github.com/anytypeio/go-chash"
)
func New() Service {
return new(service)
}
type Configuration interface {
// Id returns current nodeconf id
Id() string
// NodeIds returns list of peerId for given spaceId
NodeIds(spaceId string) []string
// IsResponsible checks if current account responsible for given spaceId
IsResponsible(spaceId string) bool
}
type configuration struct {
id string
accountId string
chash chash.CHash
}
func (c *configuration) Id() string {
return c.id
}
func (c *configuration) NodeIds(spaceId string) []string {
members := c.chash.GetMembers(spaceId)
res := make([]string, 0, len(members))
for _, m := range members {
if m.Id() != c.accountId {
res = append(res, m.Id())
}
}
return res
}
func (c *configuration) IsResponsible(spaceId string) bool {
for _, m := range c.chash.GetMembers(spaceId) {
if m.Id() == c.accountId {
return true
}
}
return false
}