Skip to content

Commit

Permalink
Merge pull request #180 from mmontes11/redis-basic-auth
Browse files Browse the repository at this point in the history
Adapted cache to support redis authentication
  • Loading branch information
stefanprodan authored Mar 29, 2022
2 parents 59dc738 + 065a18c commit 9febc66
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pkg/api/cache.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package api

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"

"github.com/gomodule/redigo/redis"
Expand Down Expand Up @@ -119,16 +121,31 @@ func (s *Server) cacheReadHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(data))
}

func (s *Server) getCacheConn() (redis.Conn, error) {
redisUrl, err := url.Parse(s.config.CacheServer)
if err != nil {
return nil, fmt.Errorf("failed to parse redis url: %v", err)
}

var opts []redis.DialOption
if user := redisUrl.User; user != nil {
opts = append(opts, redis.DialUsername(user.Username()))
if password, ok := user.Password(); ok {
opts = append(opts, redis.DialPassword(password))
}
}

return redis.Dial("tcp", redisUrl.Host, opts...)
}

func (s *Server) startCachePool(ticker *time.Ticker, stopCh <-chan struct{}) {
if s.config.CacheServer == "" {
return
}
s.pool = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", s.config.CacheServer)
},
Dial: s.getCacheConn,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
Expand Down

0 comments on commit 9febc66

Please sign in to comment.