Skip to content

Commit 8dfb915

Browse files
authored
Fix go vet issues: example tests, returning a mutex copy (#783)
Example tests are renamed to fit required naming conventions. This caused them to be relocated in generated docs. Also, a problem with writer.newBatchQueue is fixed; in order to avoid an unnecessary copy of a newly created object holding a mutex, the mutex field within batchQueue is now a pointer, so that copying on return doesn't copy the mutex itself. The same treatment is needed for the cond field; the same no-copy rule applies to it.
1 parent bc25f16 commit 8dfb915

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

example_consumergroup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/segmentio/kafka-go"
99
)
1010

11-
func ExampleConsumerGroupParallelReaders() {
11+
func ExampleGeneration_Start_consumerGroupParallelReaders() {
1212
group, err := kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{
1313
ID: "my-group",
1414
Brokers: []string{"kafka:9092"},
@@ -60,7 +60,7 @@ func ExampleConsumerGroupParallelReaders() {
6060
}
6161
}
6262

63-
func ExampleConsumerGroupOverwriteOffsets() {
63+
func ExampleGeneration_CommitOffsets_overwriteOffsets() {
6464
group, err := kafka.NewConsumerGroup(kafka.ConsumerGroupConfig{
6565
ID: "my-group",
6666
Brokers: []string{"kafka:9092"},

example_groupbalancer_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"time"
1111
)
1212

13-
// ExampleAWSRackLocal shows how the RackAffinityGroupBalancer can be used to
14-
// pair up consumers with brokers in the same AWS availability zone. This code
15-
// assumes that each brokers' rack is configured to be the name of the AZ in
16-
// which it is running.
17-
func ExampleAWSRackLocal() {
13+
// ExampleNewReader_rackAffinity shows how the RackAffinityGroupBalancer can be
14+
// used to pair up consumers with brokers in the same AWS availability zone.
15+
// This code assumes that each brokers' rack is configured to be the name of the
16+
// AZ in which it is running.
17+
func ExampleNewReader_rackAffinity() {
1818
r := NewReader(ReaderConfig{
1919
Brokers: []string{"kafka:9092"},
2020
GroupID: "my-group",

writer.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,11 @@ func (w *Writer) chooseTopic(msg Message) (string, error) {
867867
type batchQueue struct {
868868
queue []*writeBatch
869869

870-
mutex sync.Mutex
871-
cond sync.Cond
870+
// Pointers are used here to make `go vet` happy, and avoid copying mutexes.
871+
// It may be better to revert these to non-pointers and avoid the copies in
872+
// a different way.
873+
mutex *sync.Mutex
874+
cond *sync.Cond
872875

873876
closed bool
874877
}
@@ -915,9 +918,11 @@ func (b *batchQueue) Close() {
915918
func newBatchQueue(initialSize int) batchQueue {
916919
bq := batchQueue{
917920
queue: make([]*writeBatch, 0, initialSize),
921+
mutex: &sync.Mutex{},
922+
cond: &sync.Cond{},
918923
}
919924

920-
bq.cond.L = &bq.mutex
925+
bq.cond.L = bq.mutex
921926

922927
return bq
923928
}

0 commit comments

Comments
 (0)