diff --git a/hub/hub.go b/hub/hub.go index 75bbea9..4091923 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -21,7 +21,7 @@ func New(st *storage.Storage) *Hub { subscribers: []*Subscription{}, } - go h.cleanupPeriodically() + go h.cleanupEverySecond() return h } @@ -57,7 +57,7 @@ func (h *Hub) Sub(s *Subscription) { h.subscribers = append(h.subscribers, s) } -func (h *Hub) cleanupPeriodically() { +func (h *Hub) cleanupEverySecond() { t := time.NewTicker(1 * time.Second) for { diff --git a/main.go b/main.go index 8adb7f1..167d3a4 100644 --- a/main.go +++ b/main.go @@ -17,9 +17,8 @@ const ( ) var ( - theStorage *storage.Storage - theHub *hub.Hub - config struct { + theHub *hub.Hub + config struct { storage string port int } @@ -30,18 +29,16 @@ func main() { flag.IntVar(&config.port, "port", 4401, "Server HTTP port") flag.Parse() - theStorage, err := storage.New(config.storage) + store, err := storage.New(config.storage) if err != nil { panic(err) } - theHub = hub.New(theStorage) - ch := make(chan os.Signal) - signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT) - + shutdown := make(chan os.Signal) + signal.Notify(shutdown, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGINT) go func() { - <-ch - theStorage.Close() + <-shutdown + store.Close() os.Exit(0) }() @@ -50,5 +47,7 @@ func main() { fmt.Println("Storage path: %s", config.storage) fmt.Println("Server is running at http://127.0.0.1:%d", config.port) + theHub = hub.New(store) + startServer() }