|
@@ -6,12 +6,19 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+
|
|
|
+type ClientConfig struct {
|
|
|
+ MetadataRetries int
|
|
|
+ WaitForElection time.Duration
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
- id string
|
|
|
+ id string
|
|
|
+ config ClientConfig
|
|
|
brokers map[int32]*Broker
|
|
|
leaders map[string]map[int32]int32
|
|
|
lock sync.RWMutex
|
|
@@ -20,7 +27,7 @@ type Client struct {
|
|
|
|
|
|
|
|
|
|
|
|
-func NewClient(id string, host string, port int32) (client *Client, err error) {
|
|
|
+func NewClient(id string, host string, port int32, config ClientConfig) (client *Client, err error) {
|
|
|
tmp := NewBroker(host, port)
|
|
|
err = tmp.Connect()
|
|
|
if err != nil {
|
|
@@ -29,6 +36,7 @@ func NewClient(id string, host string, port int32) (client *Client, err error) {
|
|
|
|
|
|
client = new(Client)
|
|
|
client.id = id
|
|
|
+ client.config = config
|
|
|
|
|
|
client.brokers = make(map[int32]*Broker)
|
|
|
client.leaders = make(map[string]map[int32]int32)
|
|
@@ -39,7 +47,7 @@ func NewClient(id string, host string, port int32) (client *Client, err error) {
|
|
|
client.brokers[tmp.ID()] = tmp
|
|
|
|
|
|
|
|
|
- err = client.refreshTopics(make([]string, 0), 3)
|
|
|
+ err = client.refreshTopics(make([]string, 0), client.config.MetadataRetries)
|
|
|
if err != nil {
|
|
|
client.Close()
|
|
|
return nil, err
|
|
@@ -122,7 +130,7 @@ func (client *Client) refreshTopic(topic string) error {
|
|
|
tmp := make([]string, 1)
|
|
|
tmp[0] = topic
|
|
|
|
|
|
- return client.refreshTopics(tmp, 3)
|
|
|
+ return client.refreshTopics(tmp, client.config.MetadataRetries)
|
|
|
}
|
|
|
|
|
|
|
|
@@ -144,7 +152,7 @@ func (client *Client) refreshTopics(topics []string, retries int) error {
|
|
|
if retries <= 0 {
|
|
|
return LEADER_NOT_AVAILABLE
|
|
|
}
|
|
|
- time.Sleep(250 * time.Millisecond)
|
|
|
+ time.Sleep(client.config.WaitForElection)
|
|
|
return client.refreshTopics(retry, retries-1)
|
|
|
}
|
|
|
case EncodingError:
|