Реализовано восстановление соединения с redis

This commit is contained in:
ErickSkrauch 2016-09-16 19:35:58 +03:00
parent 3e3ba296d5
commit 2477433dc9

View File

@ -3,21 +3,21 @@ package main
import ( import (
"log" "log"
"runtime" "runtime"
//"time" "time"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/streadway/amqp"
"github.com/mediocregopher/radix.v2/pool" "github.com/mediocregopher/radix.v2/pool"
"elyby/minecraft-skinsystem/lib/routes" "elyby/minecraft-skinsystem/lib/routes"
"elyby/minecraft-skinsystem/lib/services" "elyby/minecraft-skinsystem/lib/services"
//"github.com/mediocregopher/radix.v2/redis"
"github.com/streadway/amqp"
"elyby/minecraft-skinsystem/lib/worker" "elyby/minecraft-skinsystem/lib/worker"
) )
const redisString string = "redis:6379" const redisString string = "redis:6379"
const redisPoolSize int = 10
const rabbitmqString string = "amqp://ely-skinsystem-app:ely-skinsystem-app-password@rabbitmq:5672/%2fely" const rabbitmqString string = "amqp://ely-skinsystem-app:ely-skinsystem-app-password@rabbitmq:5672/%2fely"
func main() { func main() {
@ -26,8 +26,8 @@ func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
log.Println("Connecting to redis") log.Println("Connecting to redis")
redisPool, redisErr := pool.New("tcp", redisString, 10) redisPool, redisErr := pool.New("tcp", redisString, redisPoolSize)
if redisErr != nil { if (redisErr != nil) {
log.Fatal("Redis unavailable") log.Fatal("Redis unavailable")
} }
log.Println("Connected to redis") log.Println("Connected to redis")
@ -35,12 +35,12 @@ func main() {
log.Println("Connecting to rabbitmq") log.Println("Connecting to rabbitmq")
// TODO: rabbitmq становится доступен не сразу. Нужно дождаться, пока он станет доступен, периодически повторяя запросы // TODO: rabbitmq становится доступен не сразу. Нужно дождаться, пока он станет доступен, периодически повторяя запросы
rabbitConnection, rabbitmqErr := amqp.Dial(rabbitmqString) rabbitConnection, rabbitmqErr := amqp.Dial(rabbitmqString)
if rabbitmqErr != nil { if (rabbitmqErr != nil) {
log.Fatalf("%s", rabbitmqErr) log.Fatalf("%s", rabbitmqErr)
} }
log.Println("Connected to rabbitmq. Trying to open a channel") log.Println("Connected to rabbitmq. Trying to open a channel")
rabbitChannel, rabbitmqErr := rabbitConnection.Channel() rabbitChannel, rabbitmqErr := rabbitConnection.Channel()
if rabbitmqErr != nil { if (rabbitmqErr != nil) {
log.Fatalf("%s", rabbitmqErr) log.Fatalf("%s", rabbitmqErr)
} }
log.Println("Connected to rabbitmq channel") log.Println("Connected to rabbitmq channel")
@ -62,23 +62,27 @@ func main() {
services.RedisPool = redisPool services.RedisPool = redisPool
services.RabbitMQChannel = rabbitChannel services.RabbitMQChannel = rabbitChannel
/*go func() { go func() {
period := 5
for { for {
time.Sleep(5 * time.Second) time.Sleep(time.Duration(period) * time.Second)
resp := services.RedisPool.Cmd("PING")
if (resp.Err == nil) {
// Если редис успешно пинганулся, значит всё хорошо
continue
}
resp := services.Redis.Cmd("PING")
if (resp.Err != nil) {
log.Println("Redis not pinged. Try to reconnect") log.Println("Redis not pinged. Try to reconnect")
newClient, redisErr := redis.Dial("tcp", redisString) newPool, redisErr := pool.New("tcp", redisString, redisPoolSize)
if (redisErr != nil) { if (redisErr != nil) {
log.Println("Cannot reconnect to redis") log.Printf("Cannot reconnect to redis, waiting %d seconds\n", period)
} else { } else {
services.Redis = newClient services.RedisPool = newPool
log.Println("Reconnected") log.Println("Reconnected")
} }
} }
} }()
}()*/
go worker.Listen() go worker.Listen()