Skip to content

Commit

Permalink
kgo: add NodeName for easy user formatting of internal seed node IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
twmb committed Mar 23, 2023
1 parent 1a59c2d commit 8e14928
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/kgo/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,22 @@ type promisedResp struct {
readEnqueue time.Time
}

// NodeName returns the name of a node, given the kgo internal node ID.
//
// Internally, seed brokers are stored with very negative node IDs, and these
// node IDs are visible in the BrokerMetadata struct. You can use NodeName to
// convert the negative node ID into "seed_#". Brokers discovered through
// metadata responses have standard non-negative numbers and this function just
// returns the number as a string.
func NodeName(nodeID int32) string {
return logID(nodeID)
}

func logID(id int32) string {
if id >= -10 {
return strconv.FormatInt(int64(id), 10)
}
return "seed " + strconv.FormatInt(int64(id)-math.MinInt32, 10)
return "seed_" + strconv.FormatInt(int64(id)-math.MinInt32, 10)
}

// BrokerMetadata is metadata for a broker.
Expand All @@ -96,7 +107,8 @@ type BrokerMetadata struct {
// NodeID is the broker node ID.
//
// Seed brokers will have very negative IDs; kgo does not try to map
// seed brokers to loaded brokers.
// seed brokers to loaded brokers. You can use NodeName to convert
// the seed node ID into a formatted string.
NodeID int32

// Port is the port of the broker.
Expand Down

0 comments on commit 8e14928

Please sign in to comment.