|
@@ -2,11 +2,18 @@ package kafka
|
|
|
|
|
|
|
|
import k "sarama/protocol"
|
|
import k "sarama/protocol"
|
|
|
|
|
|
|
|
|
|
+// Client is a generic Kafka client. It manages connections to one or more Kafka brokers.
|
|
|
|
|
+// You MUST call Close() on a client to avoid leaks, it will not be garbage-collected
|
|
|
|
|
+// automatically when it passes out of scope. A single client can be safely shared by
|
|
|
|
|
+// multiple concurrent Producers and Consumers.
|
|
|
type Client struct {
|
|
type Client struct {
|
|
|
id string
|
|
id string
|
|
|
cache *metadataCache
|
|
cache *metadataCache
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// NewClient creates a new Client with the given client ID. It connects to the broker at the given
|
|
|
|
|
+// host:port address, and uses that broker to automatically fetch metadata on the rest of the kafka cluster.
|
|
|
|
|
+// If metadata cannot be retrieved (even if the connection otherwise succeeds) then the client is not created.
|
|
|
func NewClient(id string, host string, port int32) (client *Client, err error) {
|
|
func NewClient(id string, host string, port int32) (client *Client, err error) {
|
|
|
client = new(Client)
|
|
client = new(Client)
|
|
|
client.id = id
|
|
client.id = id
|
|
@@ -17,6 +24,13 @@ func NewClient(id string, host string, port int32) (client *Client, err error) {
|
|
|
return client, nil
|
|
return client, nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Close shuts down all broker connections managed by this client. It is required to call this function before
|
|
|
|
|
+// a client object passes out of scope, as it will otherwise leak memory. You must close any Producers or Consumers
|
|
|
|
|
+// using a client before you close the client.
|
|
|
|
|
+func (client *Client) Close() {
|
|
|
|
|
+ client.cache.closeAll()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func (client *Client) leader(topic string, partition_id int32) (*k.Broker, error) {
|
|
func (client *Client) leader(topic string, partition_id int32) (*k.Broker, error) {
|
|
|
leader := client.cache.leader(topic, partition_id)
|
|
leader := client.cache.leader(topic, partition_id)
|
|
|
|
|
|