From 3f46be0e7e547eac762d9ecfab81929d3945116b Mon Sep 17 00:00:00 2001 From: eleksir Date: Mon, 22 May 2023 01:27:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=20=D1=81=20redis-=D0=BE=D0=BC,=20=D0=B4=D0=BE=D0=B1=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BD=D0=B0=D1=81=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=B9=D0=BA=D0=B0=20=D1=82=D0=B0=D0=B9=D0=BC=D0=B0=D1=83?= =?UTF-8?q?=D1=82=D0=B0=20=D1=81=D0=BE=D0=B5=D0=B4=D0=B8=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aleesa-misc-lib.go | 4 ++++ data/config_sample.json | 3 +++ main.go | 44 +++++++++++++---------------------------- types.go | 1 + 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/aleesa-misc-lib.go b/aleesa-misc-lib.go index 4845d5c..9df2a6f 100644 --- a/aleesa-misc-lib.go +++ b/aleesa-misc-lib.go @@ -309,6 +309,10 @@ func readConfig() { sampleConfig.Port = 6379 } + if sampleConfig.Timeout == 0 { + sampleConfig.Timeout = 10 + } + if sampleConfig.Loglevel == "" { sampleConfig.Loglevel = "info" } diff --git a/data/config_sample.json b/data/config_sample.json index 2c8fdb9..45a44c4 100644 --- a/data/config_sample.json +++ b/data/config_sample.json @@ -5,6 +5,9 @@ # 6379 if not set "port" : 6379, + # Redis timeout, 10 seconds if not set + "timeout" : 10, + # info if not set "loglevel" : "info", diff --git a/main.go b/main.go index 6f1f9c3..e2fa104 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "context" "fmt" "os" "os/signal" @@ -39,13 +38,18 @@ func init() { default: log.SetLevel(log.InfoLevel) } + + // Иницализируем клиента Редиски + redisClient = redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%d", config.Server, config.Port), + }).WithContext(ctx).WithTimeout(time.Duration(config.Timeout) * time.Second) + + // Обозначим, что хотим после соединения подписаться на события из канала config.Channel + subscriber = redisClient.Subscribe(ctx, config.Channel) } // Основная функция программы, не добавить и не убавить func main() { - // Main context - var ctx = context.Background() - // Откроем лог и скормим его логгеру if config.Log != "" { logfile, err := os.OpenFile(config.Log, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) @@ -57,14 +61,6 @@ func main() { log.SetOutput(logfile) } - // Иницализируем клиента - redisClient = redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%d", config.Server, config.Port), - }) - - log.Debugf("Lazy connect() to redis at %s:%d", config.Server, config.Port) - subscriber = redisClient.Subscribe(ctx, config.Channel) - // Самое время поставить траппер сигналов signal.Notify(sigChan, syscall.SIGINT, @@ -73,25 +69,13 @@ func main() { go sigHandler() - // Обработчик событий от редиски - for { - if shutdown { - time.Sleep(1 * time.Second) - continue + // Начнём выгребать события из редиски (длина ковеера/буфера канала по-умолчанию - 100 сообщений) + ch := subscriber.Channel() + + for msg := range ch { + if !shutdown { + msgParser(ctx, msg.Payload) } - - msg, err := subscriber.ReceiveMessage(ctx) - - if err != nil { - if !shutdown { - log.Warnf("Unable to connect to redis at %s:%d: %s", config.Server, config.Port, err) - } - - time.Sleep(1 * time.Second) - continue - } - - go msgParser(ctx, msg.Payload) } } diff --git a/types.go b/types.go index 14401e2..3e1a76f 100644 --- a/types.go +++ b/types.go @@ -4,6 +4,7 @@ package main type myConfig struct { Server string `json:"server,omitempty"` Port int `json:"port,omitempty"` + Timeout int `json:"timeout,omitempty"` Loglevel string `json:"loglevel,omitempty"` Log string `json:"log,omitempty"` Channel string `json:"channel,omitempty"`