-
Notifications
You must be signed in to change notification settings - Fork 70
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(topic): performance for read and creation #1393
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,3 +201,11 @@ func DeleteTopicFromCache(projectName, serviceName, topicName string) { | |
} | ||
t.Unlock() | ||
} | ||
|
||
// GetFullQueue retrieves a topics queue | ||
func (t *kafkaTopicCache) GetFullQueue(projectName, serviceName string) []string { | ||
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. ensure the method name accurately represents its purpose; If the function retrieves a specific type of queue or has a unique behavior, be more explicit in the name |
||
t.RLock() | ||
defer t.RUnlock() | ||
|
||
return t.inQueue[projectName+serviceName] | ||
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. consider handling a scenario where the key might not exist in the |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package kafkatopic | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
|
||
"github.com/aiven/aiven-go-client/v2" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
var onceForService sync.Map | ||
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. consider renaming the variable to be more descriptive, like |
||
|
||
// isTopicExists checks if topic exists | ||
func isTopicExists(ctx context.Context, client *aiven.Client, project, serviceName, topic string) bool { | ||
c := getTopicCache() | ||
|
||
var err error | ||
once, _ := onceForService.LoadOrStore(project+"/"+serviceName, new(sync.Once)) | ||
once.(*sync.Once).Do(func() { | ||
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. add comments for better clarity, especially on the significance of using |
||
var list []*aiven.KafkaListTopic | ||
list, err = client.KafkaTopics.List(ctx, project, serviceName) | ||
if err != nil { | ||
return | ||
} | ||
|
||
c.SetV1List(project, serviceName, list) | ||
}) | ||
|
||
if err != nil { | ||
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. this approach can be confusing: if possible, consider handling the error within the |
||
log.Printf("[ERROR] cannot check kafka topic existence: %s", err) | ||
return false | ||
} | ||
|
||
if slices.Contains(c.GetV1List(project, serviceName), topic) || slices.Contains(c.GetFullQueue(project, serviceName), topic) { | ||
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. for better performance, if the topic is found in the first |
||
return true | ||
} | ||
|
||
return false | ||
} |
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.
consider adding a comment above this block to briefly explain its purpose for clarity