func init() { log.Println("Initializing redis client") client = redis.NewTCPClient(&redis.Options{ Addr: ":6379", }) log.Println("Redis up and going") }
func (out *RedisOutputType) PublishIPs(name string, localAddrs []string) error { DEBUG("output_redis", "[%s] Publish the IPs %s", name, localAddrs) // connect to db client := redis.NewTCPClient(&redis.Options{ Addr: out.Hostname, Password: out.Password, DialTimeout: out.Timeout, }) client.Select(int64(out.DbTopology)) defer client.Close() _, err := client.HSet(name, "ipaddrs", strings.Join(localAddrs, ",")).Result() if err != nil { ERR("[%s] Fail to set the IP addresses: %s", name, err) return err } _, err = client.Expire(name, out.TopologyExpire).Result() if err != nil { ERR("[%s] Fail to set the expiration time: %s", name, err) return err } out.UpdateLocalTopologyMap(client) return nil }
func (out *RedisOutputType) Connect() error { client := redis.NewTCPClient(&redis.Options{ Addr: out.Hostname, Password: out.Password, DB: int64(out.Db), DialTimeout: out.Timeout, }) _, err := client.Ping().Result() if err != nil { ERR("Failed connection to Redis. ping returns an error: %s", err) return err } out.Client = client out.connected = true return nil }
// starts the redis pump and http server func main() { flag.Parse() // TODO: verify if this is actually beneficial runtime.GOMAXPROCS(runtime.NumCPU()) // load up config.json conf := loadConfig() // redis client client := redis.NewTCPClient(&redis.Options{ Addr: conf.RedisHost, Password: conf.RedisPass, DB: conf.RedisDB, }) if ping := client.Ping(); ping.Err() != nil { log.Fatal(ping.Err()) } // redis pubsub connection ps := client.PubSub() // prepare a socketmap sm := NewSocketMap(ps) // loop for receiving messages from Redis pubsub, and forwarding them on to relevant ws connection go redisPump(ps, sm) defer func() { ps.Close() client.Close() }() // prepare server http.Handle("/", NewGeobinServer(conf, NewRedisWrapper(client), ps, sm)) // Start up HTTP server log.Println("Starting server at", conf.Host, conf.Port) err := http.ListenAndServe(fmt.Sprintf("%v:%d", conf.Host, conf.Port), nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
func init() { rd = redis.NewTCPClient(&redis.Options{ Addr: "localhost:6379", DB: 0, }) }