From 3aa6f2f8c1f203675fbb4c203e99a1e7ecb43b74 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Sat, 9 Mar 2019 20:59:03 -0800 Subject: [PATCH] Started adding redis. --- main.go | 38 ++++++++++++++++++++++++++++++++++++++ stratum/stratum.go | 8 ++++++++ 2 files changed, 46 insertions(+) diff --git a/main.go b/main.go index d552f99..0b8691b 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "time" "./pool" @@ -15,6 +16,7 @@ import ( "github.com/goji/httpauth" "github.com/gorilla/mux" + "github.com/hashicorp/go-version" "github.com/yvasiyarov/gorelic" ) @@ -83,7 +85,43 @@ func readConfig(cfg *pool.Config) { } } +func checkRedisVersion(info string) bool { + var versionStr string + parts := strings.Split(info, "\r\n") + for _, line := range parts { + if strings.Index(line, ":") != -1 { + valParts := strings.Split(line, ":") + if valParts[0] == "redis_version" { + versionStr = valParts[1] + break + } + } + } + if versionStr == "" { + log.Printf("Could not detect redis version - must be super old or broken") + return false + } + minVersion, _ := version.NewVersion("2.6") + curVersion, err := version.NewVersion(versionStr) + if err != nil { + log.Printf("Could not check redis version: " + err.Error()) + return false + } + if curVersion.LessThan(minVersion) { + log.Printf("You're using redis version %s the minimum required version is 2.6. Follow the damn usage instructions...", versionStr) + return false + } + return true +} + func main() { + info, err := stratum.RedisClient.Info().Result() + if err != nil { + log.Fatal("Cannot start redis server.") + } + if !checkRedisVersion(info) { + os.Exit(1) + } rand.Seed(time.Now().UTC().UnixNano()) readConfig(&cfg) startNewrelic() diff --git a/stratum/stratum.go b/stratum/stratum.go index 9e49de0..257786b 100644 --- a/stratum/stratum.go +++ b/stratum/stratum.go @@ -13,11 +13,19 @@ import ( "sync/atomic" "time" + "github.com/go-redis/redis" + "../pool" "../rpc" "../util" ) +var RedisClient *redis.Client = redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", + DB: 0, +}) + type StratumServer struct { luckWindow int64 luckLargeWindow int64