From 850045d6a69756aafe383c7bf1067f449c9c84e3 Mon Sep 17 00:00:00 2001 From: Sergey Shcherbina Date: Thu, 29 Sep 2016 15:12:35 +0500 Subject: [PATCH] Use appendIfNotExists instead of append. Fixed bug when connection loss to server leads to exponential grow of channels and patterns array in PubSub in every reconnect --- pubsub.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/pubsub.go b/pubsub.go index 1d1fc939..de8d35c4 100644 --- a/pubsub.go +++ b/pubsub.go @@ -43,7 +43,7 @@ func (c *PubSub) subscribe(redisCmd string, channels ...string) error { func (c *PubSub) Subscribe(channels ...string) error { err := c.subscribe("SUBSCRIBE", channels...) if err == nil { - c.channels = append(c.channels, channels...) + c.channels = appendIfNotExists(c.channels, channels...) c.nsub += len(channels) } return err @@ -53,7 +53,7 @@ func (c *PubSub) Subscribe(channels ...string) error { func (c *PubSub) PSubscribe(patterns ...string) error { err := c.subscribe("PSUBSCRIBE", patterns...) if err == nil { - c.patterns = append(c.patterns, patterns...) + c.patterns = appendIfNotExists(c.patterns, patterns...) c.nsub += len(patterns) } return err @@ -74,6 +74,23 @@ func remove(ss []string, es ...string) []string { return ss } +func appendIfNotExists(ss []string, es ...string) []string { + for _, e := range es { + found := false + for _, s := range ss { + if s == e { + found = true + break + } + } + + if !found { + ss = append(ss, e) + } + } + return ss +} + // Unsubscribes the client from the given channels, or from all of // them if none is given. func (c *PubSub) Unsubscribe(channels ...string) error {