Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changed eventing configuration mutex to rwmutex and added missing lock #220

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions pkg/service/connect_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ type ConnectServiceConfiguration struct {
}

type eventingConfiguration struct {
mu *sync.Mutex
mu *sync.RWMutex
subs map[interface{}]chan Notification
}

func (s *ConnectService) Serve(ctx context.Context, eval eval.IEvaluator) error {
s.Eval = eval
s.eventingConfiguration = &eventingConfiguration{
subs: make(map[interface{}]chan Notification),
mu: &sync.Mutex{},
mu: &sync.RWMutex{},
}
lis, err := s.setupServer()
if err != nil {
Expand Down Expand Up @@ -144,13 +144,16 @@ func (s *ConnectService) EventStream(
req *connect.Request[emptypb.Empty],
stream *connect.ServerStream[schemaV1.EventStreamResponse],
) error {
s.eventingConfiguration.subs[req] = make(chan Notification, 1)
requestNotificationChan := make(chan Notification, 1)
s.eventingConfiguration.mu.Lock()
s.eventingConfiguration.subs[req] = requestNotificationChan
s.eventingConfiguration.mu.Unlock()
defer func() {
s.eventingConfiguration.mu.Lock()
delete(s.eventingConfiguration.subs, req)
s.eventingConfiguration.mu.Unlock()
}()
s.eventingConfiguration.subs[req] <- Notification{
requestNotificationChan <- Notification{
Type: ProviderReady,
}
for {
Expand All @@ -162,7 +165,7 @@ func (s *ConnectService) EventStream(
if err != nil {
s.Logger.Error(err.Error())
}
case notification := <-s.eventingConfiguration.subs[req]:
case notification := <-requestNotificationChan:
d, err := structpb.NewStruct(notification.Data)
if err != nil {
s.Logger.Error(err.Error())
Expand All @@ -181,11 +184,11 @@ func (s *ConnectService) EventStream(
}

func (s *ConnectService) Notify(n Notification) {
s.eventingConfiguration.mu.Lock()
s.eventingConfiguration.mu.RLock()
skyerus marked this conversation as resolved.
Show resolved Hide resolved
defer s.eventingConfiguration.mu.RUnlock()
for _, send := range s.eventingConfiguration.subs {
send <- n
}
s.eventingConfiguration.mu.Unlock()
}

func (s *ConnectService) ResolveBoolean(
Expand Down