From 263d0504ce2bea94e8d0042b9ea2b08ca4441a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BCl=20P=C3=A9rez?= Date: Wed, 29 Jun 2016 08:16:08 +0000 Subject: [PATCH] Do not listen on all messages, only subscribe to the needed ones --- main.go | 10 ++++++++-- nats.go | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 163b4ef..a746439 100644 --- a/main.go +++ b/main.go @@ -31,8 +31,14 @@ func main() { mux := http.NewServeMux() mux.HandleFunc("/events", authMiddleware) - // Start nats handler, subscribe to all events - n.Subscribe(">", natsHandler) + // Start nats handler, subscribe to all events related with the monitor + n.Subscribe("monitor.user", natsHandler) + n.Subscribe("service.create", natsHandler) + n.Subscribe("service.delete", natsHandler) + n.Subscribe("service.create.done", natsHandler) + n.Subscribe("service.create.error", natsHandler) + n.Subscribe("service.delete.done", natsHandler) + n.Subscribe("service.delete.error", natsHandler) // Start Listening addr := fmt.Sprintf("%s:%s", host, port) diff --git a/nats.go b/nats.go index c1af920..77635e8 100644 --- a/nats.go +++ b/nats.go @@ -5,10 +5,10 @@ package main import ( - "time" - "github.com/nats-io/nats" "github.com/r3labs/sse" + "log" + "time" ) func natsHandler(msg *nats.Msg) { @@ -25,9 +25,11 @@ func natsHandler(msg *nats.Msg) { } case "service.create", "service.delete": // Create a new stream + log.Println("Creating stream for", notification.getServiceID()) s.CreateStream(notification.getServiceID()) case "service.create.done", "service.create.error", "service.delete.done", "service.delete.error": // Remove a new stream when the build completes + log.Println("Closing stream for", notification.getServiceID()) go func(s *sse.Server) { // Notifications appear out of order, wait for all notifications to come through before closing time.Sleep(time.Second)