Skip to content

Commit

Permalink
style: Partitions & WriteablePartitions
Browse files Browse the repository at this point in the history
extract the same logic in Partitions and WritablePartitions

Signed-off-by: Trino <sujun.trinoooo@gmail.com>
  • Loading branch information
Trinoooo authored and dnwe committed Aug 7, 2024
1 parent be9539d commit 28f4289
Showing 1 changed file with 7 additions and 22 deletions.
29 changes: 7 additions & 22 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,34 +364,19 @@ func (client *client) MetadataTopics() ([]string, error) {
}

func (client *client) Partitions(topic string) ([]int32, error) {
if client.Closed() {
return nil, ErrClosedClient
}

partitions := client.cachedPartitions(topic, allPartitions)

if len(partitions) == 0 {
err := client.RefreshMetadata(topic)
if err != nil {
return nil, err
}
partitions = client.cachedPartitions(topic, allPartitions)
}

// no partitions found after refresh metadata
if len(partitions) == 0 {
return nil, ErrUnknownTopicOrPartition
}

return partitions, nil
return client.getPartitions(topic, allPartitions)
}

func (client *client) WritablePartitions(topic string) ([]int32, error) {
return client.getPartitions(topic, writablePartitions)
}

func (client *client) getPartitions(topic string, pt partitionType) ([]int32, error) {
if client.Closed() {
return nil, ErrClosedClient
}

partitions := client.cachedPartitions(topic, writablePartitions)
partitions := client.cachedPartitions(topic, pt)

// len==0 catches when it's nil (no such topic) and the odd case when every single
// partition is undergoing leader election simultaneously. Callers have to be able to handle
Expand All @@ -404,7 +389,7 @@ func (client *client) WritablePartitions(topic string) ([]int32, error) {
if err != nil {
return nil, err
}
partitions = client.cachedPartitions(topic, writablePartitions)
partitions = client.cachedPartitions(topic, pt)
}

if partitions == nil {
Expand Down

0 comments on commit 28f4289

Please sign in to comment.