-
Notifications
You must be signed in to change notification settings - Fork 748
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: Concurrent access to trigger connection maps #2814
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ccfda47
Merge pull request #2 from argoproj/master
gokulav137 f2de435
Merge branch 'argoproj:master' into master
gokulav137 7e8f2ef
Refactor connection maps for trigger clients from map to concurre…
gokulav137 b78023a
Fix New func for concurrent safe map creating nil maps
gokulav137 f87c2c4
Empty Commit to retrigger test
gokulav137 e6e95bb
Refactor Concurrent Safe connection map structs using generics
gokulav137 f13eb1c
Move StringKeyedMap from util.go to dedicated concurret.go file
gokulav137 03e3998
Segregate external imports and avoid merge conflicts
gokulav137 ed62a55
Revert unintended change - azure-event-hubs-go import alias reverted …
gokulav137 a296f30
Merge branch 'master' into concurrent-safe-map
gokulav137 a011e0e
Merge branch 'master' into concurrent-safe-map
gokulav137 dd521cb
Refactor passing StringKeyedMap by reference to value
gokulav137 18cca0b
Merge branch 'concurrent-safe-map' of https://github.com/gokulav137/a…
gokulav137 c4b19b5
Rename common/concurrent.go to common/string_keyed_map.go
gokulav137 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package common | ||
|
||
import "sync" | ||
|
||
// Concurrent Safe String keyed map | ||
type StringKeyedMap[T any] struct { | ||
items map[string]T | ||
lock *sync.RWMutex | ||
} | ||
|
||
func NewStringKeyedMap[T any]() *StringKeyedMap[T] { | ||
return &StringKeyedMap[T]{ | ||
items: make(map[string]T, 0), | ||
lock: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (sm *StringKeyedMap[T]) Store(key string, item T) { | ||
sm.lock.Lock() | ||
defer sm.lock.Unlock() | ||
sm.items[key] = item | ||
} | ||
|
||
func (sm *StringKeyedMap[T]) Load(key string) (T, bool) { | ||
sm.lock.RLock() | ||
defer sm.lock.RUnlock() | ||
ok, item := sm.items[key] | ||
return ok, item | ||
} | ||
|
||
func (sm *StringKeyedMap[T]) Delete(key string) { | ||
sm.lock.Lock() | ||
defer sm.lock.Unlock() | ||
delete(sm.items, key) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import ( | |
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"github.com/argoproj/argo-events/common" | ||
sensormetrics "github.com/argoproj/argo-events/metrics" | ||
eventbusv1alpha1 "github.com/argoproj/argo-events/pkg/apis/eventbus/v1alpha1" | ||
"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1" | ||
|
@@ -51,25 +52,25 @@ type SensorContext struct { | |
hostname string | ||
|
||
// httpClients holds the reference to HTTP clients for HTTP triggers. | ||
httpClients map[string]*http.Client | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define a generic struct in the utilities like below? type StringKeyedMap[T any] struct {
items map[string]T
lock *sync.RWMutex
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I'll make the changes. Thanks. |
||
httpClients *common.StringKeyedMap[*http.Client] | ||
// customTriggerClients holds the references to the gRPC clients for the custom trigger servers | ||
customTriggerClients map[string]*grpc.ClientConn | ||
customTriggerClients *common.StringKeyedMap[*grpc.ClientConn] | ||
// http client to send slack messages. | ||
slackHTTPClient *http.Client | ||
// kafkaProducers holds references to the active kafka producers | ||
kafkaProducers map[string]sarama.AsyncProducer | ||
kafkaProducers *common.StringKeyedMap[sarama.AsyncProducer] | ||
// pulsarProducers holds references to the active pulsar producers | ||
pulsarProducers map[string]pulsar.Producer | ||
pulsarProducers *common.StringKeyedMap[pulsar.Producer] | ||
// natsConnections holds the references to the active nats connections. | ||
natsConnections map[string]*natslib.Conn | ||
natsConnections *common.StringKeyedMap[*natslib.Conn] | ||
// awsLambdaClients holds the references to active AWS Lambda clients. | ||
awsLambdaClients map[string]*lambda.Lambda | ||
awsLambdaClients *common.StringKeyedMap[*lambda.Lambda] | ||
// openwhiskClients holds the references to active OpenWhisk clients. | ||
openwhiskClients map[string]*whisk.Client | ||
openwhiskClients *common.StringKeyedMap[*whisk.Client] | ||
// azureEventHubsClients holds the references to active Azure Event Hub clients. | ||
azureEventHubsClients map[string]*eventhubs.Hub | ||
azureEventHubsClients *common.StringKeyedMap[*eventhubs.Hub] | ||
// azureServiceBusClients holds the references to active Azure Service Bus clients. | ||
azureServiceBusClients map[string]*servicebus.Sender | ||
azureServiceBusClients *common.StringKeyedMap[*servicebus.Sender] | ||
metrics *sensormetrics.Metrics | ||
} | ||
|
||
|
@@ -82,18 +83,18 @@ func NewSensorContext(kubeClient kubernetes.Interface, dynamicClient dynamic.Int | |
eventBusConfig: eventBusConfig, | ||
eventBusSubject: eventBusSubject, | ||
hostname: hostname, | ||
httpClients: make(map[string]*http.Client), | ||
customTriggerClients: make(map[string]*grpc.ClientConn), | ||
httpClients: common.NewStringKeyedMap[*http.Client](), | ||
customTriggerClients: common.NewStringKeyedMap[*grpc.ClientConn](), | ||
slackHTTPClient: &http.Client{ | ||
Timeout: time.Minute * 5, | ||
}, | ||
kafkaProducers: make(map[string]sarama.AsyncProducer), | ||
pulsarProducers: make(map[string]pulsar.Producer), | ||
natsConnections: make(map[string]*natslib.Conn), | ||
awsLambdaClients: make(map[string]*lambda.Lambda), | ||
openwhiskClients: make(map[string]*whisk.Client), | ||
azureEventHubsClients: make(map[string]*eventhubs.Hub), | ||
azureServiceBusClients: make(map[string]*servicebus.Sender), | ||
kafkaProducers: common.NewStringKeyedMap[sarama.AsyncProducer](), | ||
pulsarProducers: common.NewStringKeyedMap[pulsar.Producer](), | ||
natsConnections: common.NewStringKeyedMap[*natslib.Conn](), | ||
awsLambdaClients: common.NewStringKeyedMap[*lambda.Lambda](), | ||
openwhiskClients: common.NewStringKeyedMap[*whisk.Client](), | ||
azureEventHubsClients: common.NewStringKeyedMap[*eventhubs.Hub](), | ||
azureServiceBusClients: common.NewStringKeyedMap[*servicebus.Sender](), | ||
metrics: metrics, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to return a pointer, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I'll make it return by value.