mirror of
https://github.com/elyby/chrly.git
synced 2025-01-03 18:51:49 +05:30
Реализовано восстановление соединения с redis
This commit is contained in:
parent
3e3ba296d5
commit
2477433dc9
@ -3,21 +3,21 @@ package main
|
||||
import (
|
||||
"log"
|
||||
"runtime"
|
||||
//"time"
|
||||
"time"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/streadway/amqp"
|
||||
"github.com/mediocregopher/radix.v2/pool"
|
||||
|
||||
"elyby/minecraft-skinsystem/lib/routes"
|
||||
"elyby/minecraft-skinsystem/lib/services"
|
||||
//"github.com/mediocregopher/radix.v2/redis"
|
||||
|
||||
"github.com/streadway/amqp"
|
||||
"elyby/minecraft-skinsystem/lib/worker"
|
||||
)
|
||||
|
||||
const redisString string = "redis:6379"
|
||||
const redisPoolSize int = 10
|
||||
|
||||
const rabbitmqString string = "amqp://ely-skinsystem-app:ely-skinsystem-app-password@rabbitmq:5672/%2fely"
|
||||
|
||||
func main() {
|
||||
@ -26,8 +26,8 @@ func main() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
||||
log.Println("Connecting to redis")
|
||||
redisPool, redisErr := pool.New("tcp", redisString, 10)
|
||||
if redisErr != nil {
|
||||
redisPool, redisErr := pool.New("tcp", redisString, redisPoolSize)
|
||||
if (redisErr != nil) {
|
||||
log.Fatal("Redis unavailable")
|
||||
}
|
||||
log.Println("Connected to redis")
|
||||
@ -35,12 +35,12 @@ func main() {
|
||||
log.Println("Connecting to rabbitmq")
|
||||
// TODO: rabbitmq становится доступен не сразу. Нужно дождаться, пока он станет доступен, периодически повторяя запросы
|
||||
rabbitConnection, rabbitmqErr := amqp.Dial(rabbitmqString)
|
||||
if rabbitmqErr != nil {
|
||||
if (rabbitmqErr != nil) {
|
||||
log.Fatalf("%s", rabbitmqErr)
|
||||
}
|
||||
log.Println("Connected to rabbitmq. Trying to open a channel")
|
||||
rabbitChannel, rabbitmqErr := rabbitConnection.Channel()
|
||||
if rabbitmqErr != nil {
|
||||
if (rabbitmqErr != nil) {
|
||||
log.Fatalf("%s", rabbitmqErr)
|
||||
}
|
||||
log.Println("Connected to rabbitmq channel")
|
||||
@ -62,23 +62,27 @@ func main() {
|
||||
services.RedisPool = redisPool
|
||||
services.RabbitMQChannel = rabbitChannel
|
||||
|
||||
/*go func() {
|
||||
go func() {
|
||||
period := 5
|
||||
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")
|
||||
newClient, redisErr := redis.Dial("tcp", redisString)
|
||||
newPool, redisErr := pool.New("tcp", redisString, redisPoolSize)
|
||||
if (redisErr != nil) {
|
||||
log.Println("Cannot reconnect to redis")
|
||||
log.Printf("Cannot reconnect to redis, waiting %d seconds\n", period)
|
||||
} else {
|
||||
services.Redis = newClient
|
||||
services.RedisPool = newPool
|
||||
log.Println("Reconnected")
|
||||
}
|
||||
}
|
||||
}
|
||||
}()*/
|
||||
}()
|
||||
|
||||
go worker.Listen()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user