Skip to content

Commit

Permalink
Make Client responsible for closing its conn
Browse files Browse the repository at this point in the history
Prior to this, closing client connections was done in a separate
step outside of `client.Close()`.

However it makes sense to move this responsibility to Client, the
higher-level abstraction that already contains conn in its internal
state.

This change does not alter the actual closing sequence:
consumers/producers are closed first and then the client connection.

Closes #36.
  • Loading branch information
agis committed Aug 25, 2017
1 parent f542a0a commit 2dcf24c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 4 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func (c *Client) Producer(cfg *rdkafka.ConfigMap) (*Producer, error) {
return c.producer, nil
}

// Close closes c's underlying producers and consumers. Calling Close on
// an already closed client will result in a panic.
// Close closes producers, consumers and the connection of c.
// Calling Close on an already closed client will result in a panic.
func (c *Client) Close() {
for cid := range c.consumers {
// TODO(agis): make this blocking
Expand All @@ -143,4 +143,6 @@ func (c *Client) Close() {
if c.producer != nil {
c.producer.Close()
}

c.conn.Close()
}
3 changes: 1 addition & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ func NewServer(ctx context.Context, manager *ConsumerManager, timeout time.Durat
}

func (s *Server) handleConn(conn net.Conn) {
defer conn.Close()

c := NewClient(conn, s.manager)
defer c.Close()

s.clientByID.Store(c.id, c)
defer func() {
s.clientByID.Delete(c.id)
Expand Down

0 comments on commit 2dcf24c

Please sign in to comment.