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: Concurrent access to trigger connection maps #2814

Merged
merged 14 commits into from
Sep 27, 2023

Conversation

gokulav137
Copy link
Contributor

@gokulav137 gokulav137 commented Sep 23, 2023

Fixes #2660

Refactor connection maps for trigger clients from map to concurrent safe map.

Checklist:

gokulav137 and others added 5 commits September 9, 2023 18:38
feat: add all sprig functions to templates (argoproj#2768)
…safe maps

    - HTTP client
    - CustomTrigger client
    - Kafka client
    - Pulsar client
    - NATS client
    - Lambda client
    - OpenWhisk client
    - Eventbus client
    - Servicebus client

Signed-off-by: gokulav137 <gokulav999@gmail.com>
Signed-off-by: gokulav137 <gokulav999@gmail.com>
Signed-off-by: gokulav137 <gokulav999@gmail.com>
@gokulav137 gokulav137 marked this pull request as ready for review September 23, 2023 10:06
@@ -51,25 +52,25 @@ type SensorContext struct {
hostname string

// httpClients holds the reference to HTTP clients for HTTP triggers.
httpClients map[string]*http.Client
Copy link
Member

Choose a reason for hiding this comment

The 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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I'll make the changes. Thanks.

- Add StringKeyedMap, Concurrent Safe String keyed map; using generics
- Replace concurrent safe connection map structs with StringKeyedMap

Signed-off-by: gokulav137 <52277763+gokulav137@users.noreply.github.com>
common/util.go Outdated
@@ -475,3 +476,35 @@ func StructToMap(obj interface{}, output map[string]interface{}) error {

return json.Unmarshal(data, &output) // Convert to a map
}

// Concurrent Safe String keyed map
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving it to a dedicated file would be better, all the rest looks good to me. Thanks again!

Copy link
Contributor Author

@gokulav137 gokulav137 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick feedback.😄Where do you think this would fit. Kinda puzzled.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking same package but, in a concurrent.go file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same package, string_keyed_map.go?

Copy link
Contributor Author

@gokulav137 gokulav137 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking along the lines any common concurrent safe implementations required moving forward could be placed in concurrent.go. If this doesn't track, I will change the filename. Let me know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is even bigger...

It is good to use straightforward file names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh big as in vague. concur_safe_type.go? Sorry if I'm dragging this naming out 😅.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its just that the other files in common/ are json.go , retry.go, string.go, time.go etc are generic names

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal opinions:

  1. The package structure in Argo Events (including file names) should be refactored;
  2. Those file names (json.go, string.go ...) are kind of simple, because they provide that particular file name related utility functions (not struct functions), and maybe there's no better names to describe it - you can imagine we could name them something like json_util.go or string_util.go, that wouldn't make too much difference. But it's for sure it does not make much sense to separate them into different files.
  3. If it's util structs (like the one you added), there is already a clear boundary (the struct itself), putting them in separated files and naming them as straightforward as possible might be better, so that ppl clearly know what they are about.

Copy link
Contributor Author

@gokulav137 gokulav137 Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes a lot of sense. Thanks for clarifying!

Will rename the file to string_keyed_map.go

Signed-off-by: gokulav137 <52277763+gokulav137@users.noreply.github.com>
Signed-off-by: gokulav137 <52277763+gokulav137@users.noreply.github.com>
…from eventhubs to eventhub

Signed-off-by: gokulav137 <52277763+gokulav137@users.noreply.github.com>
lock *sync.RWMutex
}

func NewStringKeyedMap[T any]() *StringKeyedMap[T] {
Copy link
Member

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?

Copy link
Contributor Author

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.

Signed-off-by: gokulav137 <52277763+gokulav137@users.noreply.github.com>
Signed-off-by: gokulav137 <52277763+gokulav137@users.noreply.github.com>
Copy link
Member

@whynowy whynowy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@whynowy whynowy changed the title Fix: Concurrent access to trigger connection maps fix: Concurrent access to trigger connection maps Sep 27, 2023
@whynowy whynowy merged commit 334097f into argoproj:master Sep 27, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race conditions in sensor
2 participants