-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(topic): performance for read and creation
- Loading branch information
1 parent
dffd94c
commit 0448497
Showing
5 changed files
with
60 additions
and
19 deletions.
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
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
42 changes: 42 additions & 0 deletions
42
internal/sdkprovider/service/kafkatopic/kafka_topic_exists.go
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,42 @@ | ||
package kafkatopic | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"sync" | ||
|
||
"github.com/aiven/aiven-go-client/v2" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
var onceCheckTopicForService sync.Map | ||
|
||
// isTopicExists checks if topic exists | ||
func isTopicExists(ctx context.Context, client *aiven.Client, project, serviceName, topic string) bool { | ||
c := getTopicCache() | ||
|
||
var err error | ||
|
||
// Warming up of the v1 cache should happen only once per service | ||
once, _ := onceCheckTopicForService.LoadOrStore(project+"/"+serviceName, new(sync.Once)) | ||
once.(*sync.Once).Do(func() { | ||
var list []*aiven.KafkaListTopic | ||
list, err = client.KafkaTopics.List(ctx, project, serviceName) | ||
if err != nil { | ||
return | ||
} | ||
|
||
c.SetV1List(project, serviceName, list) | ||
}) | ||
|
||
if err != nil { | ||
log.Printf("[ERROR] cannot check kafka topic existence: %s", err) | ||
return false | ||
} | ||
|
||
if slices.Contains(c.GetV1List(project, serviceName), topic) { | ||
return true | ||
} | ||
|
||
return slices.Contains(c.GetQueue(project, serviceName), topic) | ||
} |
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