Skip to content

Commit

Permalink
fix: update sync_map.go
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Aug 7, 2024
1 parent 7f5ba6d commit 2ee9464
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 86 deletions.
20 changes: 10 additions & 10 deletions app/sephirah/internal/model/modelsupervisor/modelsupervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ type PorterGroup struct {
}

type ServerFeatureSummaryMap struct {
AccountPlatforms *libtype.SyncMap[[]string]
AppInfoSources *libtype.SyncMap[[]string]
FeedSources *libtype.SyncMap[[]string]
NotifyDestinations *libtype.SyncMap[[]string]
FeedItemActions *libtype.SyncMap[[]string]
AccountPlatforms *libtype.SyncMap[string, []string]
AppInfoSources *libtype.SyncMap[string, []string]
FeedSources *libtype.SyncMap[string, []string]
NotifyDestinations *libtype.SyncMap[string, []string]
FeedItemActions *libtype.SyncMap[string, []string]
}

func NewServerFeatureSummaryMap() *ServerFeatureSummaryMap {
return &ServerFeatureSummaryMap{
AccountPlatforms: libtype.NewSyncMap[[]string](),
AppInfoSources: libtype.NewSyncMap[[]string](),
FeedSources: libtype.NewSyncMap[[]string](),
NotifyDestinations: libtype.NewSyncMap[[]string](),
FeedItemActions: libtype.NewSyncMap[[]string](),
AccountPlatforms: libtype.NewSyncMap[string, []string](),
AppInfoSources: libtype.NewSyncMap[string, []string](),
FeedSources: libtype.NewSyncMap[string, []string](),
NotifyDestinations: libtype.NewSyncMap[string, []string](),
FeedItemActions: libtype.NewSyncMap[string, []string](),
}
}
20 changes: 10 additions & 10 deletions app/sephirah/internal/supervisor/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (s *Supervisor) HasAccountPlatform(platform string) bool {
func (s *Supervisor) WithAccountPlatform(ctx context.Context, platform string) context.Context {
s.featureSummaryRWMu.RLock()
defer s.featureSummaryRWMu.RUnlock()
if platforms := s.featureSummaryMap.AccountPlatforms.Load(platform); platforms != nil {
return client.WithPorterAddress(ctx, *platforms)
if platforms, ok := s.featureSummaryMap.AccountPlatforms.Load(platform); ok {
return client.WithPorterAddress(ctx, platforms)
}
return client.WithPorterFastFail(ctx)
}
Expand All @@ -41,8 +41,8 @@ func (s *Supervisor) HasAppInfoSource(source string) bool {
func (s *Supervisor) WithAppInfoSource(ctx context.Context, source string) context.Context {
s.featureSummaryRWMu.RLock()
defer s.featureSummaryRWMu.RUnlock()
if sources := s.featureSummaryMap.AppInfoSources.Load(source); sources != nil {
return client.WithPorterAddress(ctx, *sources)
if sources, ok := s.featureSummaryMap.AppInfoSources.Load(source); ok {
return client.WithPorterAddress(ctx, sources)
}
return client.WithPorterFastFail(ctx)
}
Expand All @@ -64,8 +64,8 @@ func (s *Supervisor) HasFeedSource(source *modelsupervisor.FeatureRequest) bool
func (s *Supervisor) WithFeedSource(ctx context.Context, source *modelsupervisor.FeatureRequest) context.Context {
s.featureSummaryRWMu.RLock()
defer s.featureSummaryRWMu.RUnlock()
if sources := s.featureSummaryMap.FeedSources.Load(source.ID); sources != nil {
return client.WithPorterAddress(ctx, *sources)
if sources, ok := s.featureSummaryMap.FeedSources.Load(source.ID); ok {
return client.WithPorterAddress(ctx, sources)
}
return client.WithPorterFastFail(ctx)
}
Expand All @@ -87,8 +87,8 @@ func (s *Supervisor) HasNotifyDestination(destination *modelsupervisor.FeatureRe
func (s *Supervisor) WithNotifyDestination(ctx context.Context, destination *modelsupervisor.FeatureRequest) context.Context {
s.featureSummaryRWMu.RLock()
defer s.featureSummaryRWMu.RUnlock()
if destinations := s.featureSummaryMap.NotifyDestinations.Load(destination.ID); destinations != nil {
return client.WithPorterAddress(ctx, *destinations)
if destinations, ok := s.featureSummaryMap.NotifyDestinations.Load(destination.ID); ok {
return client.WithPorterAddress(ctx, destinations)
}
return client.WithPorterFastFail(ctx)
}
Expand All @@ -107,8 +107,8 @@ func (s *Supervisor) HasFeedItemAction(request *modelsupervisor.FeatureRequest)
func (s *Supervisor) WithFeedItemAction(ctx context.Context, request *modelsupervisor.FeatureRequest) context.Context {
s.featureSummaryRWMu.RLock()
defer s.featureSummaryRWMu.RUnlock()
if actions := s.featureSummaryMap.FeedItemActions.Load(request.ID); actions != nil {
return client.WithPorterAddress(ctx, *actions)
if actions, ok := s.featureSummaryMap.FeedItemActions.Load(request.ID); ok {
return client.WithPorterAddress(ctx, actions)
}
return client.WithPorterFastFail(ctx)
}
32 changes: 16 additions & 16 deletions app/sephirah/internal/supervisor/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,51 @@ func summarize( //nolint:gocognit // how?
continue
}
for _, account := range ins.FeatureSummary.AccountPlatforms {
a := resMap.AccountPlatforms.Load(ins.Address)
a, _ := resMap.AccountPlatforms.Load(ins.Address)
if a == nil {
a = &[]string{}
a = []string{}
}
*a = append(*a, account.ID)
resMap.AccountPlatforms.Store(ins.Address, *a)
a = append(a, account.ID)
resMap.AccountPlatforms.Store(ins.Address, a)
if supportedAccountPlatforms[account.ID] {
continue
}
res.AccountPlatforms = append(res.AccountPlatforms, account)
supportedAccountPlatforms[account.ID] = true
}
for _, appSource := range ins.FeatureSummary.AppInfoSources {
a := resMap.AppInfoSources.Load(ins.Address)
a, _ := resMap.AppInfoSources.Load(ins.Address)
if a == nil {
a = &[]string{}
a = []string{}
}
*a = append(*a, appSource.ID)
resMap.AppInfoSources.Store(ins.Address, *a)
a = append(a, appSource.ID)
resMap.AppInfoSources.Store(ins.Address, a)
if supportedAppSources[appSource.ID] {
continue
}
res.AppInfoSources = append(res.AppInfoSources, appSource)
supportedAppSources[appSource.ID] = true
}
for _, feedSource := range ins.FeatureSummary.FeedSources {
a := resMap.FeedSources.Load(ins.Address)
a, _ := resMap.FeedSources.Load(ins.Address)
if a == nil {
a = &[]string{}
a = []string{}
}
*a = append(*a, feedSource.ID)
resMap.FeedSources.Store(ins.Address, *a)
a = append(a, feedSource.ID)
resMap.FeedSources.Store(ins.Address, a)
if supportedFeedSources[feedSource.ID] {
continue
}
res.FeedSources = append(res.FeedSources, feedSource)
supportedFeedSources[feedSource.ID] = true
}
for _, notifyDestination := range ins.FeatureSummary.NotifyDestinations {
a := resMap.NotifyDestinations.Load(ins.Address)
a, _ := resMap.NotifyDestinations.Load(ins.Address)
if a == nil {
a = &[]string{}
a = []string{}
}
*a = append(*a, notifyDestination.ID)
resMap.NotifyDestinations.Store(ins.Address, *a)
a = append(a, notifyDestination.ID)
resMap.NotifyDestinations.Store(ins.Address, a)
if supportedNotifyDestinations[notifyDestination.ID] {
continue
}
Expand Down
13 changes: 8 additions & 5 deletions app/sephirah/internal/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Supervisor struct {

refreshMu sync.Mutex
trustedAddresses []string
instanceController *libtype.SyncMap[modelsupervisor.PorterInstanceController]
instanceController *libtype.SyncMap[string, modelsupervisor.PorterInstanceController]
instanceCache *libcache.Map[string, modelsupervisor.PorterInstance]

featureSummary *modelsupervisor.ServerFeatureSummary
Expand All @@ -63,7 +63,7 @@ func NewSupervisor(
UUID: int64(uuid.New().ID()),
porter: porter,
auth: auth,
instanceController: libtype.NewSyncMap[modelsupervisor.PorterInstanceController](),
instanceController: libtype.NewSyncMap[string, modelsupervisor.PorterInstanceController](),
instanceCache: instanceCache,
refreshMu: sync.Mutex{},
featureSummary: new(modelsupervisor.ServerFeatureSummary),
Expand All @@ -82,7 +82,10 @@ func (s *Supervisor) GetInstanceController(
ctx context.Context,
address string,
) *modelsupervisor.PorterInstanceController {
return s.instanceController.Load(address)
if c, ok := s.instanceController.Load(address); ok {
return &c
}
return nil
}

func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit,funlen // TODO
Expand Down Expand Up @@ -114,7 +117,7 @@ func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit,funlen // TODO

// Discover new instances and Refresh disconnected instances
for _, address := range discoveredAddresses {
if ic := s.instanceController.Load(address); ic != nil &&
if ic, ok := s.instanceController.Load(address); ok &&
ic.ConnectionStatus != modelsupervisor.PorterConnectionStatusDisconnected {
continue
}
Expand All @@ -131,7 +134,7 @@ func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit,funlen // TODO
return
}

if ic := s.instanceController.Load(address); ic == nil ||
if ic, ok := s.instanceController.Load(address); ok ||
(ic.GlobalName != ins.GlobalName || ic.BinarySummary.BuildVersion != ins.BinarySummary.Version) {
newInstancesMu.Lock()
newInstances = append(newInstances, ins)
Expand Down
68 changes: 23 additions & 45 deletions internal/lib/libtype/sync_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,60 @@ package libtype

import "sync"

type SyncMap[T any] struct {
type SyncMap[K comparable, V any] struct {
m sync.Map
}

func NewSyncMap[T any]() *SyncMap[T] {
return &SyncMap[T]{
func NewSyncMap[K comparable, V any]() *SyncMap[K, V] {
return &SyncMap[K, V]{
m: sync.Map{},
}
}

func (m *SyncMap[T]) Load(key string) *T {
func (m *SyncMap[K, V]) Load(key K) (V, bool) {
var value V
v, ok := m.m.Load(key)
if !ok {
return nil
return value, ok
}
value, ok := v.(T)
if !ok {
return nil
}
return &value
return v.(V), ok
}

func (m *SyncMap[T]) LoadOrStore(key string, value T) *T {
v, loaded := m.m.LoadOrStore(key, value)
if !loaded {
return nil
}
actual, ok := v.(T)
if !ok {
return nil
}
return &actual
func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (V, bool) {
a, loaded := m.m.LoadOrStore(key, value)
return a.(V), loaded
}

func (m *SyncMap[T]) LoadAndDelete(key string) *T {
func (m *SyncMap[K, V]) LoadAndDelete(key K) (V, bool) {
var value V
v, loaded := m.m.LoadAndDelete(key)
if !loaded {
return nil
return value, loaded
}
value, ok := v.(T)
if !ok {
return nil
}
return &value
return v.(V), loaded
}

func (m *SyncMap[T]) Store(key string, value T) {
func (m *SyncMap[K, V]) Store(key K, value V) {
m.m.Store(key, value)
}

func (m *SyncMap[T]) Range(f func(key string, value T) bool) {
m.m.Range(func(key, value interface{}) bool {
return f(key.(string), value.(T))
})
func (m *SyncMap[K, V]) Range(f func(key K, value V) bool) {
m.m.Range(func(key, value any) bool { return f(key.(K), value.(V)) })
}

func (m *SyncMap[T]) Swap(key string, value T) *T {
v, loaded := m.m.Swap(key, value)
if !loaded {
return nil
}
previous, ok := v.(T)
if !ok {
return nil
}
return &previous
func (m *SyncMap[K, V]) Swap(key K, value V) (V, bool) {
p, loaded := m.m.Swap(key, value)
return p.(V), loaded
}

func (m *SyncMap[T]) CompareAndSwap(key string, oldValue, newValue T) bool {
func (m *SyncMap[K, V]) CompareAndSwap(key K, oldValue, newValue V) bool {
return m.m.CompareAndSwap(key, oldValue, newValue)
}

func (m *SyncMap[T]) Delete(key string) {
func (m *SyncMap[K, V]) Delete(key K) {
m.m.Delete(key)
}

func (m *SyncMap[T]) CompareAndDelete(key string, value T) bool {
func (m *SyncMap[K, V]) CompareAndDelete(key string, value V) bool {
return m.m.CompareAndDelete(key, value)
}

0 comments on commit 2ee9464

Please sign in to comment.