Skip to content

Commit

Permalink
fix(client): refactor duplicated partition logic
Browse files Browse the repository at this point in the history
extract the same logic in Partitions and WritablePartitions

Signed-off-by: Trino <[email protected]>
  • Loading branch information
Trinoooo committed Jun 12, 2024
1 parent 57b9397 commit 496e760
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 @@ -363,34 +363,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 @@ -403,7 +388,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 496e760

Please sign in to comment.