Skip to content

Commit

Permalink
chore: remove subscriber (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
achettyiitr authored Jan 31, 2024
1 parent 18b146c commit 4adc6cf
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 25 deletions.
11 changes: 5 additions & 6 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type WorkspaceConfigCache struct {
configs *modelv2.WorkspaceConfigs
updateLock sync.Mutex
subscribers []*Subscriber
subscribers []chan notifications.WorkspaceConfigNotification
subscriberLock sync.Mutex
}

Expand Down Expand Up @@ -44,7 +44,7 @@ func (c *WorkspaceConfigCache) Set(configs *modelv2.WorkspaceConfigs) {
defer c.subscriberLock.Unlock()

for _, subscriber := range c.subscribers {
subscriber.notify(notifications.WorkspaceConfigNotification{})
subscriber <- notifications.WorkspaceConfigNotification{}
}
}

Expand Down Expand Up @@ -86,13 +86,12 @@ func (c *WorkspaceConfigCache) merge(configs *modelv2.WorkspaceConfigs) {
// Subscribers are notified in the order they are subscribed.
// They can monitor for updates by reading from the notifications channel, provided by the Notifications function.
// It is expected to handle any notifications in a timely manner, otherwise it will block the cache from updating.
func (c *WorkspaceConfigCache) Subscribe() *Subscriber {
func (c *WorkspaceConfigCache) Subscribe() chan notifications.WorkspaceConfigNotification {
c.subscriberLock.Lock()
defer c.subscriberLock.Unlock()

subscriber := &Subscriber{
notifications: make(chan notifications.WorkspaceConfigNotification),
}
subscriber := make(chan notifications.WorkspaceConfigNotification)

c.subscribers = append(c.subscribers, subscriber)

return subscriber
Expand Down
2 changes: 1 addition & 1 deletion internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCacheSubscriptions(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
for range s.Notifications() {
for range s {
configs = append(configs, c.Get())
wg.Done()
if len(configs) == 2 {
Expand Down
17 changes: 0 additions & 17 deletions internal/cache/subscriber.go

This file was deleted.

2 changes: 1 addition & 1 deletion sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ type Subscriber interface {
Notifications() chan notifications.WorkspaceConfigNotification
}

func (cp *ControlPlane) Subscribe() *cache.Subscriber {
func (cp *ControlPlane) Subscribe() chan notifications.WorkspaceConfigNotification {
return cp.configsCache.Subscribe()
}

0 comments on commit 4adc6cf

Please sign in to comment.