Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit f79b210

Browse files
authored
Merge pull request #3 from heetch/rog-003-retry
retry on failure to connect to Kafka
2 parents 09ea4d3 + b547705 commit f79b210

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

client.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"os"
99
"strconv"
1010
"strings"
11+
"time"
1112

1213
"github.com/Shopify/sarama"
14+
"gopkg.in/retry.v1"
1315
)
1416

1517
var ErrDisabled = fmt.Errorf("kafka tests are disabled")
@@ -35,6 +37,10 @@ var ErrDisabled = fmt.Errorf("kafka tests are disabled")
3537
// A boolean as parsed by strconv.ParseBool. If this
3638
// is true, a secure TLS connection will be used.
3739
//
40+
// - $KAFKA_TIMEOUT
41+
// The maximum duration to wait when trying to connect
42+
// to Kakfa. Defaults to "30s".
43+
//
3844
// The returned Kafka instance must be closed after use.
3945
func New() (*Kafka, error) {
4046
disabled, err := boolVar("KAFKA_DISABLE")
@@ -59,11 +65,29 @@ func New() (*Kafka, error) {
5965
saslUser: os.Getenv("KAFKA_USERNAME"),
6066
saslPassword: os.Getenv("KAFKA_PASSWORD"),
6167
}
62-
admin, err := sarama.NewClusterAdmin(addrs, client.Config())
63-
if err != nil {
64-
return nil, fmt.Errorf("cannot connect to Kafka cluster at %q: %v", addrs, err)
68+
// The cluster might not be available immediately, so try
69+
// for a while before giving up.
70+
retryLimit := 30 * time.Second
71+
if limit := os.Getenv("KAFKA_TIMEOUT"); limit != "" {
72+
retryLimit, err = time.ParseDuration(limit)
73+
if err != nil {
74+
return nil, fmt.Errorf("bad value for KAFKA_TIMEOUT: %v", err)
75+
}
76+
}
77+
retryStrategy := retry.LimitTime(retryLimit, retry.Exponential{
78+
Initial: time.Millisecond,
79+
MaxDelay: time.Second,
80+
})
81+
for a := retry.Start(retryStrategy, nil); a.Next(); {
82+
admin, err := sarama.NewClusterAdmin(addrs, client.Config())
83+
if err == nil {
84+
client.admin = admin
85+
break
86+
}
87+
if !a.Next() {
88+
return nil, fmt.Errorf("cannot connect to Kafka cluster at %q after %v: %v", addrs, retryLimit, err)
89+
}
6590
}
66-
client.admin = admin
6791
return client, nil
6892
}
6993

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ require (
66
github.com/Shopify/sarama v1.24.1
77
github.com/frankban/quicktest v1.4.1
88
github.com/klauspost/cpuid v1.2.1 // indirect
9+
gopkg.in/retry.v1 v1.0.3
910
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ github.com/eapache/queue v1.1.0 h1:YOEu7KNc61ntiQlcEeUIoDTJ2o8mQznoNvUhiigpIqc=
1313
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
1414
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
1515
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
16+
github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20=
1617
github.com/frankban/quicktest v1.4.1 h1:Wv2VwvNn73pAdFIVUQRXYDFp31lXKbqblIXo/Q5GPSg=
1718
github.com/frankban/quicktest v1.4.1/go.mod h1:36zfPVQyHxymz4cH7wlDmVwDrJuljRB60qkgn7rorfQ=
1819
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
1920
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
21+
github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
2022
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
2123
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
2224
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
@@ -38,6 +40,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3840
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3941
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a h1:9ZKAASQSHhDYGoxY8uLVpewe1GDZ2vu2Tr/vTdVAkFQ=
4042
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
43+
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM=
44+
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a/go.mod h1:4r5QyqhjIWCcK8DO4KMclc5Iknq5qVBAlbYYzAbUScQ=
4145
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4246
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
4347
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -61,3 +65,5 @@ gopkg.in/jcmturner/gokrb5.v7 v7.2.3 h1:hHMV/yKPwMnJhPuPx7pH2Uw/3Qyf+thJYlisUc440
6165
gopkg.in/jcmturner/gokrb5.v7 v7.2.3/go.mod h1:l8VISx+WGYp+Fp7KRbsiUuXTTOnxIc3Tuvyavf11/WM=
6266
gopkg.in/jcmturner/rpc.v1 v1.1.0 h1:QHIUxTX1ISuAv9dD2wJ9HWQVuWDX/Zc0PfeC2tjc4rU=
6367
gopkg.in/jcmturner/rpc.v1 v1.1.0/go.mod h1:YIdkC4XfD6GXbzje11McwsDuOlZQSb9W4vfLvuNnlv8=
68+
gopkg.in/retry.v1 v1.0.3 h1:a9CArYczAVv6Qs6VGoLMio99GEs7kY9UzSF9+LD+iGs=
69+
gopkg.in/retry.v1 v1.0.3/go.mod h1:FJkXmWiMaAo7xB+xhvDF59zhfjDWyzmyAxiT4dB688g=

0 commit comments

Comments
 (0)