-
-
Save raecoo/d58c3b8bf60dd55e73ca60d61be53324 to your computer and use it in GitHub Desktop.
Golang redis pub/sub example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pubsub | |
import ( | |
"github.com/garyburd/redigo/redis" | |
log "github.com/sirupsen/logrus" | |
) | |
// Service service | |
type Service struct { | |
pool *redis.Pool | |
conn redis.Conn | |
} | |
// NewInput input for constructor | |
type NewInput struct { | |
RedisURL string | |
} | |
// New return new service | |
func New(input *NewInput) *Service { | |
if input == nil { | |
log.Fatal("input is required") | |
} | |
var redispool *redis.Pool | |
redispool = &redis.Pool{ | |
Dial: func() (redis.Conn, error) { | |
return redis.Dial("tcp", input.RedisURL) | |
}, | |
} | |
// Get a connection | |
conn := redispool.Get() | |
defer conn.Close() | |
// Test the connection | |
_, err := conn.Do("PING") | |
if err != nil { | |
log.Fatalf("can't connect to the redis database, got error:\n%v", err) | |
} | |
return &Service{ | |
pool: redispool, | |
conn: conn, | |
} | |
} | |
// Publish publish key value | |
func (s *Service) Publish(key string, value string) error { | |
conn := s.pool.Get() | |
conn.Do("PUBLISH", key, value) | |
return nil | |
} | |
// Subscribe subscribe | |
func (s *Service) Subscribe(key string, msg chan []byte) error { | |
rc := s.pool.Get() | |
psc := redis.PubSubConn{Conn: rc} | |
if err := psc.PSubscribe(key); err != nil { | |
return err | |
} | |
go func() { | |
for { | |
switch v := psc.Receive().(type) { | |
case redis.PMessage: | |
msg <- v.Data | |
} | |
} | |
}() | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pubsub | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"testing" | |
"time" | |
) | |
func TestPublish(t *testing.T) { | |
t.Parallel() | |
svc := New(&NewInput{ | |
RedisURL: os.Getenv("REDIS_URL"), | |
}) | |
err := svc.Publish("test/foo", "bar") | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func TestSubscribe(t *testing.T) { | |
t.Parallel() | |
svc := New(&NewInput{ | |
RedisURL: os.Getenv("REDIS_URL"), | |
}) | |
channel := fmt.Sprintf("test/%s", time.Now().Add(10*time.Second).String()) | |
val := time.Now().String() | |
reply := make(chan []byte) | |
err := svc.Subscribe(channel, reply) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = svc.Publish(channel, val) | |
if err != nil { | |
log.Fatal(err) | |
} | |
t.Run("message", func(t *testing.T) { | |
msg := <-reply | |
if string(msg) != val { | |
t.Fatal("expected correct reply message") | |
} | |
log.Printf("recieved %q", string(msg)) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment