Merge pull request #379 from Hepri/master

PubSub. Use appendIfNotExists instead of append for channels and patterns
This commit is contained in:
Vladimir Mihailenco 2016-09-29 14:33:17 +03:00 committed by GitHub
commit 833b0c68df
1 changed files with 19 additions and 2 deletions

View File

@ -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 {