Skip to content

Commit

Permalink
Clean up client listeners (#2)
Browse files Browse the repository at this point in the history
- fixes memory leak
  • Loading branch information
beatgammit authored Oct 31, 2016
1 parent 90945ea commit 20397bd
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ func (c *Client) registerListener(id ID) {
c.listeners[id] = wait
}

func (c *Client) unregisterListener(id ID) {
log.Println("unregister listener:", id)
delete(c.listeners, id)
}

func (c *Client) waitOnListener(id ID) (msg Message, err error) {
log.Println("wait on listener:", id)
c.lock.RLock()
Expand All @@ -341,6 +346,9 @@ type EventHandler func(args []interface{}, kwargs map[string]interface{})
func (c *Client) Subscribe(topic string, fn EventHandler) error {
id := NewID()
c.registerListener(id)
// TODO: figure out where to clean this up
// defer c.unregisterListener(id)

sub := &Subscribe{
Request: id,
Options: make(map[string]interface{}),
Expand Down Expand Up @@ -386,6 +394,8 @@ func (c *Client) Unsubscribe(topic string) error {

id := NewID()
c.registerListener(id)
defer c.unregisterListener(id)

sub := &Unsubscribe{
Request: id,
Subscription: subscriptionID,
Expand Down Expand Up @@ -418,6 +428,9 @@ type MethodHandler func(
func (c *Client) Register(procedure string, fn MethodHandler, options map[string]interface{}) error {
id := NewID()
c.registerListener(id)
// TODO: figure out where to clean this up
// defer c.unregisterListener(id)

register := &Register{
Request: id,
Options: options,
Expand Down Expand Up @@ -475,6 +488,8 @@ func (c *Client) Unregister(procedure string) error {
}
id := NewID()
c.registerListener(id)
defer c.unregisterListener(id)

unregister := &Unregister{
Request: id,
Registration: procedureID,
Expand Down Expand Up @@ -523,6 +538,7 @@ func (rpc RPCError) Error() string {
func (c *Client) Call(procedure string, args []interface{}, kwargs map[string]interface{}) (*Result, error) {
id := NewID()
c.registerListener(id)
defer c.unregisterListener(id)

call := &Call{
Request: id,
Expand Down

0 comments on commit 20397bd

Please sign in to comment.