diff --git a/minecraft-skinsystem.go b/minecraft-skinsystem.go index 5d86c59..513aa30 100644 --- a/minecraft-skinsystem.go +++ b/minecraft-skinsystem.go @@ -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.Redis.Cmd("PING") - if (resp.Err != nil) { - log.Println("Redis not pinged. Try to reconnect") - newClient, redisErr := redis.Dial("tcp", redisString) - if (redisErr != nil) { - log.Println("Cannot reconnect to redis") - } else { - services.Redis = newClient - log.Println("Reconnected") - } + resp := services.RedisPool.Cmd("PING") + if (resp.Err == nil) { + // Если редис успешно пинганулся, значит всё хорошо + continue + } + + log.Println("Redis not pinged. Try to reconnect") + newPool, redisErr := pool.New("tcp", redisString, redisPoolSize) + if (redisErr != nil) { + log.Printf("Cannot reconnect to redis, waiting %d seconds\n", period) + } else { + services.RedisPool = newPool + log.Println("Reconnected") } } - }()*/ + }() go worker.Listen()