Skip to content

Commit

Permalink
added locks for middlewares in router (ThreeDotsLabs#465)
Browse files Browse the repository at this point in the history
Co-authored-by: Lara Blyshchyk <[email protected]>
Co-authored-by: Miłosz Smółka <[email protected]>
  • Loading branch information
3 people authored and thejoeejoee committed Sep 5, 2024
1 parent 97f0308 commit 3860237
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions _examples/pubsubs/googlecloud/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 h1:vArvWooPH749rNHpBGgVl+U9B9dATjiEhJzcWGlovNs=
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
Expand Down
14 changes: 11 additions & 3 deletions message/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func NewRouter(config RouterConfig, logger watermill.LoggerAdapter) (*Router, er

handlerAdded: make(chan struct{}),

handlersLock: &sync.RWMutex{},
middlewaresLock: &sync.RWMutex{},
handlersLock: &sync.RWMutex{},

closingInProgressCh: make(chan struct{}),
closedCh: make(chan struct{}),
Expand All @@ -131,7 +132,8 @@ type middleware struct {
type Router struct {
config RouterConfig

middlewares []middleware
middlewares []middleware
middlewaresLock *sync.RWMutex

plugins []RouterPlugin

Expand Down Expand Up @@ -185,6 +187,8 @@ func (r *Router) addRouterLevelMiddleware(m ...HandlerMiddleware) {
}

func (r *Router) addHandlerLevelMiddleware(handlerName string, m ...HandlerMiddleware) {
r.middlewaresLock.Lock()
defer r.middlewaresLock.Unlock()
for _, handlerMiddleware := range m {
middleware := middleware{
Handler: handlerMiddleware,
Expand Down Expand Up @@ -435,7 +439,11 @@ func (r *Router) RunHandlers(ctx context.Context) error {
go func() {
defer cancel()

h.run(ctx, r.middlewares)
r.middlewaresLock.Lock()
middlewares := append([]middleware{}, r.middlewares...)
r.middlewaresLock.Unlock()

h.run(ctx, middlewares)

r.handlersWg.Done()
r.logger.Info("Subscriber stopped", watermill.LogFields{
Expand Down

0 comments on commit 3860237

Please sign in to comment.