Skip to content

Commit

Permalink
refactor: connect redis with TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
tangxinfa committed Jan 10, 2020
1 parent 1d0f7d9 commit 9197c1c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 37 deletions.
38 changes: 11 additions & 27 deletions src/redis/driver_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redis

import (
"crypto/tls"
"net"

stats "github.com/lyft/gostats"
"github.com/lyft/ratelimit/src/assert"
Expand Down Expand Up @@ -66,43 +67,26 @@ func (this *poolImpl) Put(c Connection) {
}
}

func NewAuthTLSPoolImpl(scope stats.Scope, auth string, url string, poolSize int) Pool {
logger.Warnf("connecting to redis on tls %s with pool size %d", url, poolSize)
func NewPoolImpl(scope stats.Scope, useTls bool, auth string, url string, poolSize int) Pool {
logger.Warnf("connecting to redis on %s with pool size %d", url, poolSize)
df := func(network, addr string) (*redis.Client, error) {
conn, err := tls.Dial("tcp", addr, &tls.Config{})
if err != nil {
return nil, err
var conn net.Conn
var err error
if useTls {
conn, err = tls.Dial("tcp", addr, &tls.Config{})
} else {
conn, err = net.Dial("tcp", addr)
}
client, err := redis.NewClient(conn)

if err != nil {
return nil, err
}
if auth != "" {
logger.Warnf("enabling authentication to redis on tls %s", url)
if err = client.Cmd("AUTH", auth).Err; err != nil {
client.Close()
return nil, err
}
}
return client, nil
}
pool, err := pool.NewCustom("tcp", url, poolSize, df)
checkError(err)
return &poolImpl{
pool: pool,
stats: newPoolStats(scope)}
}
client, err := redis.NewClient(conn)

func NewAuthPoolImpl(scope stats.Scope, auth string, url string, poolSize int) Pool {
logger.Warnf("connecting to redis on tcp %s with pool size %d", url, poolSize)
df := func(network, addr string) (*redis.Client, error) {
client, err := redis.Dial("tcp", addr)
if err != nil {
return nil, err
}
if auth != "" {
logger.Warnf("enabling authentication to redis on tcp %s", url)
logger.Warnf("enabling authentication to redis on %s", url)
if err = client.Cmd("AUTH", auth).Err; err != nil {
client.Close()
return nil, err
Expand Down
12 changes: 2 additions & 10 deletions src/service_cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@ func Run() {

var perSecondPool redis.Pool
if s.RedisPerSecond {
if s.RedisPerSecondTls {
perSecondPool = redis.NewAuthTLSPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
} else {
perSecondPool = redis.NewAuthPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
}
perSecondPool = redis.NewPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondTls, s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
}
var otherPool redis.Pool
if s.RedisTls {
otherPool = redis.NewAuthTLSPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisAuth, s.RedisUrl, s.RedisPoolSize)
} else {
otherPool = redis.NewAuthPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisAuth, s.RedisUrl, s.RedisPoolSize)
}
otherPool = redis.NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisTls, s.RedisAuth, s.RedisUrl, s.RedisPoolSize)

var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
Expand Down

0 comments on commit 9197c1c

Please sign in to comment.