0

Подправлена работа с redis-ом, добавлена настройка таймаута соединения

This commit is contained in:
eleksir
2023-05-22 01:27:17 +03:00
parent 7ccffb7095
commit 3f46be0e7e
4 changed files with 22 additions and 30 deletions

View File

@@ -309,6 +309,10 @@ func readConfig() {
sampleConfig.Port = 6379
}
if sampleConfig.Timeout == 0 {
sampleConfig.Timeout = 10
}
if sampleConfig.Loglevel == "" {
sampleConfig.Loglevel = "info"
}

View File

@@ -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",

44
main.go
View File

@@ -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)
}
}

View File

@@ -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"`