-
Notifications
You must be signed in to change notification settings - Fork 1.8k
chore: apply modernize fixes from gopls #3297
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
Changes from all commits
bccb0fe
c85f6fb
7ce5897
7b353e9
9cba592
30d5d02
5648bd9
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 |
|---|---|---|
|
|
@@ -4,7 +4,9 @@ import ( | |
| "container/heap" | ||
| "errors" | ||
| "fmt" | ||
| "maps" | ||
| "math" | ||
| "slices" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
@@ -233,7 +235,7 @@ func (s *stickyBalanceStrategy) Plan(members map[string]ConsumerGroupMemberMetad | |
| delete(unvisitedPartitions, partition) | ||
| currentPartitionConsumers[partition] = memberID | ||
|
|
||
| if !strsContains(members[memberID].Topics, partition.Topic) { | ||
| if !slices.Contains(members[memberID].Topics, partition.Topic) { | ||
| unassignedPartitions = append(unassignedPartitions, partition) | ||
| continue | ||
| } | ||
|
|
@@ -279,15 +281,6 @@ func (s *stickyBalanceStrategy) AssignmentData(memberID string, topics map[strin | |
| }, nil) | ||
| } | ||
|
|
||
| func strsContains(s []string, value string) bool { | ||
| for _, entry := range s { | ||
| if entry == value { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // Balance assignments across consumers for maximum fairness and stickiness. | ||
| func (s *stickyBalanceStrategy) balance(currentAssignment map[string][]topicPartitionAssignment, prevAssignment map[topicPartitionAssignment]consumerGenerationPair, sortedPartitions []topicPartitionAssignment, unassignedPartitions []topicPartitionAssignment, sortedCurrentSubscriptions []string, consumer2AllPotentialPartitions map[string][]topicPartitionAssignment, partition2AllPotentialConsumers map[topicPartitionAssignment][]string, currentPartitionConsumer map[topicPartitionAssignment]string) { | ||
| initializing := len(sortedCurrentSubscriptions) == 0 || len(currentAssignment[sortedCurrentSubscriptions[0]]) == 0 | ||
|
|
@@ -321,9 +314,7 @@ func (s *stickyBalanceStrategy) balance(currentAssignment map[string][]topicPart | |
| // create a deep copy of the current assignment so we can revert to it if we do not get a more balanced assignment later | ||
| preBalanceAssignment := deepCopyAssignment(currentAssignment) | ||
| preBalancePartitionConsumers := make(map[topicPartitionAssignment]string, len(currentPartitionConsumer)) | ||
| for k, v := range currentPartitionConsumer { | ||
| preBalancePartitionConsumers[k] = v | ||
| } | ||
| maps.Copy(preBalancePartitionConsumers, currentPartitionConsumer) | ||
|
Collaborator
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 is also a good candidate for |
||
|
|
||
| reassignmentPerformed := s.performReassignments(sortedPartitions, currentAssignment, prevAssignment, sortedCurrentSubscriptions, consumer2AllPotentialPartitions, partition2AllPotentialConsumers, currentPartitionConsumer) | ||
|
|
||
|
|
@@ -332,15 +323,11 @@ func (s *stickyBalanceStrategy) balance(currentAssignment map[string][]topicPart | |
| if !initializing && reassignmentPerformed && getBalanceScore(currentAssignment) >= getBalanceScore(preBalanceAssignment) { | ||
| currentAssignment = deepCopyAssignment(preBalanceAssignment) | ||
| currentPartitionConsumer = make(map[topicPartitionAssignment]string, len(preBalancePartitionConsumers)) | ||
| for k, v := range preBalancePartitionConsumers { | ||
| currentPartitionConsumer[k] = v | ||
| } | ||
| maps.Copy(currentPartitionConsumer, preBalancePartitionConsumers) | ||
|
Collaborator
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 the fixed assignments (those that could not change) back | ||
| for consumer, assignments := range fixedAssignments { | ||
| currentAssignment[consumer] = assignments | ||
| } | ||
| maps.Copy(currentAssignment, fixedAssignments) | ||
| } | ||
|
|
||
| // NewBalanceStrategyRoundRobin returns a round-robin balance strategy, | ||
|
|
@@ -659,12 +646,7 @@ func removeTopicPartitionFromMemberAssignments(assignments []topicPartitionAssig | |
| } | ||
|
|
||
| func memberAssignmentsIncludeTopicPartition(assignments []topicPartitionAssignment, topic topicPartitionAssignment) bool { | ||
| for _, assignment := range assignments { | ||
| if assignment == topic { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| return slices.Contains(assignments, topic) | ||
|
Collaborator
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. We might want to replace the actual function calls to this unexported function to |
||
| } | ||
|
|
||
| func sortPartitions(currentAssignment map[string][]topicPartitionAssignment, partitionsWithADifferentPreviousAssignment map[topicPartitionAssignment]consumerGenerationPair, isFreshAssignment bool, partition2AllPotentialConsumers map[topicPartitionAssignment][]string, consumer2AllPotentialPartitions map[string][]topicPartitionAssignment) []topicPartitionAssignment { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "math" | ||
| "math/rand" | ||
| "reflect" | ||
| "slices" | ||
| "sort" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -2123,9 +2124,8 @@ func BenchmarkStickAssignmentWithLargeNumberOfConsumersAndTopics(b *testing.B) { | |
| } | ||
| topics[fmt.Sprintf("topic%d", i)] = partitions | ||
| } | ||
| b.ResetTimer() | ||
|
|
||
| for n := 0; n < b.N; n++ { | ||
| for b.Loop() { | ||
| if _, err := s.Plan(members, topics); err != nil { | ||
| b.Errorf("Error building plan in benchmark: %v", err) | ||
| } | ||
|
|
@@ -2163,9 +2163,8 @@ func BenchmarkStickAssignmentWithLargeNumberOfConsumersAndTopicsAndExistingAssig | |
| for i := 0; i < 1; i++ { | ||
| delete(members, fmt.Sprintf("consumer%d", i)) | ||
| } | ||
| b.ResetTimer() | ||
|
|
||
| for n := 0; n < b.N; n++ { | ||
| for b.Loop() { | ||
| if _, err := s.Plan(members, topics); err != nil { | ||
| b.Errorf("Error building plan in benchmark: %v", err) | ||
| } | ||
|
|
@@ -2203,13 +2202,7 @@ func verifyValidityAndBalance(t *testing.T, consumers map[string]ConsumerGroupMe | |
|
|
||
| for i, memberID := range members { | ||
| for assignedTopic := range plan[memberID] { | ||
| found := false | ||
| for _, assignableTopic := range consumers[memberID].Topics { | ||
| if assignableTopic == assignedTopic { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| found := slices.Contains(consumers[memberID].Topics, assignedTopic) | ||
| if !found { | ||
|
Comment on lines
+2205
to
2206
Collaborator
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. No need to go through a temporary local variable anymore, we could just do the |
||
| t.Errorf("Consumer %s had assigned topic %q that wasn't in the list of assignable topics %v", memberID, assignedTopic, consumers[memberID].Topics) | ||
| t.FailNow() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1670,7 +1670,7 @@ func buildClientFirstMessage(token *AccessToken) ([]byte, error) { | |
| ext = "\x01" + mapToString(token.Extensions, "=", "\x01") | ||
| } | ||
|
|
||
| resp := []byte(fmt.Sprintf("n,,\x01auth=Bearer %s%s\x01\x01", token.Token, ext)) | ||
| resp := fmt.Appendf(nil, "n,,\x01auth=Bearer %s%s\x01\x01", token.Token, ext) | ||
|
|
||
| return resp, nil | ||
|
Comment on lines
+1673
to
1675
Collaborator
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 doesn’t seem to be necessary to pass through a temporary local variable, but the composed |
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "maps" | ||
| "net" | ||
| "reflect" | ||
| "strconv" | ||
|
|
@@ -86,9 +87,7 @@ func (b *MockBroker) SetLatency(latency time.Duration) { | |
| // If the request type is not found in the map then nothing is sent. | ||
| func (b *MockBroker) SetHandlerByMap(handlerMap map[string]MockResponse) { | ||
| fnMap := make(map[string]MockResponse) | ||
| for k, v := range handlerMap { | ||
| fnMap[k] = v | ||
| } | ||
| maps.Copy(fnMap, handlerMap) | ||
|
Collaborator
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.
|
||
| b.setHandler(func(req *request) (res encoderWithHeader) { | ||
| reqTypeName := reflect.TypeOf(req.body).Elem().Name() | ||
| mockResponse := fnMap[reqTypeName] | ||
|
|
@@ -104,9 +103,7 @@ func (b *MockBroker) SetHandlerByMap(handlerMap map[string]MockResponse) { | |
| // and invoke the found RequestHandlerFunc instance to generate an appropriate reply. | ||
| func (b *MockBroker) SetHandlerFuncByMap(handlerMap map[string]requestHandlerFunc) { | ||
| fnMap := make(map[string]requestHandlerFunc) | ||
| for k, v := range handlerMap { | ||
| fnMap[k] = v | ||
| } | ||
| maps.Copy(fnMap, handlerMap) | ||
|
Collaborator
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.
|
||
| b.setHandler(func(req *request) (res encoderWithHeader) { | ||
| reqTypeName := reflect.TypeOf(req.body).Elem().Name() | ||
| return fnMap[reqTypeName](req) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,8 +103,8 @@ func main() { | |
|
|
||
| if *headers != "" { | ||
| var hdrs []sarama.RecordHeader | ||
| arrHdrs := strings.Split(*headers, ",") | ||
| for _, h := range arrHdrs { | ||
| arrHdrs := strings.SplitSeq(*headers, ",") | ||
| for h := range arrHdrs { | ||
|
Comment on lines
+106
to
+107
Collaborator
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. No need to pass this through a temporary local variable. |
||
| if header := strings.Split(h, ":"); len(header) != 2 { | ||
| printUsageErrorAndExit("-header should be key:value. Example: -headers=foo:bar,bar:foo") | ||
| } else { | ||
|
|
||
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.
Since we’re also making the map on line 1034, we can replace this with
groups := maps.Clone(response.Groups)