func (suite *HandlerSuite) SetupTest() { var err error df := func(network, addr string) (*redis.Client, error) { client, err := redis.Dial(network, addr) // fmt.Println("DIaling") if err != nil { return nil, err } err = client.Cmd("SELECT", 8).Err if err != nil { return nil, err } err = client.Cmd("FLUSHDB").Err if err != nil { return nil, err } return client, nil } redisPool, err = pool.NewCustomPool("tcp", redisURL, 1, df) if err != nil { panic(err) } c, err := redisPool.Get() errorHandler(err) defer redisPool.Put(c) SetDomain("xyz1234567890", "peterbe.com", c) }
func (c *CacheRedis) setup() error { pool, err := pool.NewCustomPool( "tcp", config.Redis.Address, config.Redis.PoolSize, dialFunc, ) if err != nil { log.Error("Error connecting to redis database") return err } c.Pool = pool log.Notice("Loaded Redis cache (address: %s, db: %v, prefix: \"%s\", pool: %v)", config.Redis.Address, config.Redis.DB, config.Redis.Prefix, config.Redis.PoolSize) return nil }
func (c *CacheRedis) setup() error { pool, err := pool.NewCustomPool( "tcp", config.Redis.Address, config.Redis.PoolSize, dialFunc, ) if err != nil { log.Error("Error connecting to redis database") return err } c.Pool = pool log.Info("Loaded Redis cache (pool: " + fmt.Sprintf("%v", config.Redis.PoolSize) + ")") return nil }
func (c *Cluster) newPool(addr string, clearThrottle bool) (*pool.Pool, error) { if clearThrottle { delete(c.poolThrottles, addr) } else if throttle, ok := c.poolThrottles[addr]; ok { select { case <-throttle: delete(c.poolThrottles, addr) default: return nil, fmt.Errorf("newPool(%s) throttled", addr) } } df := func(network, addr string) (*redis.Client, error) { return redis.DialTimeout(network, addr, c.o.Timeout) } p, err := pool.NewCustomPool("tcp", addr, c.o.PoolSize, df) if err != nil { c.poolThrottles[addr] = time.After(c.o.PoolThrottle) return nil, err } return p, err }
func (suite *Suite) SetupTest() { var err error df := func(network, addr string) (*redis.Client, error) { client, err := redis.Dial(network, addr) // fmt.Println("DIaling") if err != nil { return nil, err } err = client.Cmd("SELECT", 8).Err if err != nil { return nil, err } err = client.Cmd("FLUSHDB").Err if err != nil { return nil, err } return client, nil } redisPool, err = pool.NewCustomPool("tcp", redisURL, 1, df) if err != nil { panic(err) } }
func main() { var ( port = 3001 redisDatabase = 0 redisPoolSize = 10 clientID = "" clientSecret = "" hashKey = "randomishstringthatsi32charslong" blockKey = "randomishstringthatsi32charslong" deferPanicKey = "" ) flag.IntVar(&port, "port", port, "Port to start the server on") flag.IntVar(&procs, "procs", 1, "Number of CPU processors (0 to use max)") flag.BoolVar(&debug, "debug", false, "Debug mode") flag.StringVar( &redisURL, "redisURL", redisURL, "Redis URL to tcp connect to") flag.StringVar( &staticPrefix, "staticPrefix", staticPrefix, "Prefix in front of static assets in HTML") flag.IntVar(&redisDatabase, "redisDatabase", redisDatabase, "Redis database number to connect to") flag.StringVar( &clientID, "clientID", clientID, "OAuth Client ID") flag.StringVar( &clientSecret, "clientSecret", clientSecret, "OAuth Client Secret") flag.BoolVar(&usingHTTPS, "usingHTTPS", usingHTTPS, "Whether requests are made under HTTPS") flag.StringVar( &hashKey, "hashKey", hashKey, "HMAC hash key to use for encoding cookies") flag.StringVar( &blockKey, "blockKey", blockKey, "Block key to encrypt cookie values") flag.StringVar( &deferPanicKey, "deferPanicKey", deferPanicKey, "Auth key for deferpanic.com") flag.Parse() dfs := deferstats.NewClient(deferPanicKey) if deferPanicKey == "" { dfs.SetnoPost(true) } go dfs.CaptureStats() oauthConf.ClientID = clientID oauthConf.ClientSecret = clientSecret sCookie = securecookie.New([]byte(hashKey), []byte(blockKey)) log.Println("REDIS DATABASE:", redisDatabase) log.Println("DEBUG MODE:", debug) log.Println("STATIC PREFIX:", staticPrefix) if !debug { redisPoolSize = 100 } // Figuring out how many processors to use. maxProcs := runtime.NumCPU() if procs == 0 { procs = maxProcs } else if procs < 0 { panic("PROCS < 0") } else if procs > maxProcs { panic(fmt.Sprintf("PROCS > max (%v)", maxProcs)) } log.Println("PROCS:", procs) runtime.GOMAXPROCS(procs) renderer = render.New(render.Options{ IndentJSON: debug, IsDevelopment: debug, }) df := func(network, addr string) (*redis.Client, error) { client, err := redis.Dial(network, addr) if err != nil { return nil, err } err = client.Cmd("SELECT", redisDatabase).Err if err != nil { return nil, err } // if err = client.Cmd("AUTH", "SUPERSECRET").Err; err != nil { // client.Close() // return nil, err // } return client, nil } var err error redisPool, err = pool.NewCustomPool("tcp", redisURL, redisPoolSize, df) errorHandler(err) mux := mux.NewRouter() mux.HandleFunc("/", dfs.HTTPHandler(indexHandler)).Methods("GET", "HEAD") mux.HandleFunc("/v1/ping", dfs.HTTPHandler(pingHandler)).Methods("GET", "HEAD") mux.HandleFunc("/v1", dfs.HTTPHandler(fetchHandler)).Methods("GET", "HEAD") mux.HandleFunc("/v1", dfs.HTTPHandler(updateHandler)).Methods("POST", "PUT") mux.HandleFunc("/v1", dfs.HTTPHandler(deleteHandler)).Methods("DELETE") mux.HandleFunc("/v1/stats", dfs.HTTPHandler(privateStatsHandler)).Methods("GET") mux.HandleFunc("/v1/flush", dfs.HTTPHandler(flushHandler)).Methods("DELETE") mux.HandleFunc("/v1/bulk", dfs.HTTPHandler(bulkHandler)).Methods("POST", "PUT") mux.HandleFunc("/login", dfs.HTTPHandler(handleGitHubLogin)).Methods("GET") mux.HandleFunc("/logout", dfs.HTTPHandler(logoutHandler)).Methods("GET", "POST") mux.HandleFunc("/github_oauth_cb", dfs.HTTPHandler(handleGitHubCallback)).Methods("GET") mux.HandleFunc("/domainkeys/new", domainkeyNewHandler).Methods("POST") mux.HandleFunc("/domainkeys/delete", domainkeyDeleteHandler).Methods("POST") n := negroni.Classic() n.UseHandler(mux) n.Run(fmt.Sprintf(":%d", port)) }