mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-02-14 18:29:52 +00:00
Merge pull request #71 from stefanprodan/register-instance
Register hostname and version in cache
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
|||||||
"github.com/gomodule/redigo/redis"
|
"github.com/gomodule/redigo/redis"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"github.com/stefanprodan/podinfo/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cache godoc
|
// Cache godoc
|
||||||
@@ -108,18 +110,41 @@ func (s *Server) cacheReadHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(data))
|
w.Write([]byte(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) startCachePool() {
|
func (s *Server) startCachePool(ticker *time.Ticker, stopCh <-chan struct{}) {
|
||||||
if s.config.CacheServer != "" {
|
if s.config.CacheServer == "" {
|
||||||
s.pool = &redis.Pool{
|
return
|
||||||
MaxIdle: 3,
|
|
||||||
IdleTimeout: 240 * time.Second,
|
|
||||||
Dial: func() (redis.Conn, error) {
|
|
||||||
return redis.Dial("tcp", s.config.CacheServer)
|
|
||||||
},
|
|
||||||
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
|
||||||
_, err := c.Do("PING")
|
|
||||||
return err
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
s.pool = &redis.Pool{
|
||||||
|
MaxIdle: 3,
|
||||||
|
IdleTimeout: 240 * time.Second,
|
||||||
|
Dial: func() (redis.Conn, error) {
|
||||||
|
return redis.Dial("tcp", s.config.CacheServer)
|
||||||
|
},
|
||||||
|
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
||||||
|
_, err := c.Do("PING")
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// set <hostname>=<version> with an expiry time of one minute
|
||||||
|
setVersion := func() {
|
||||||
|
conn := s.pool.Get()
|
||||||
|
if _, err := conn.Do("SET", s.config.Hostname, version.VERSION, "EX", 60); err != nil {
|
||||||
|
s.logger.Warn("cache server is offline", zap.Error(err), zap.String("server", s.config.CacheServer))
|
||||||
|
}
|
||||||
|
_ = conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// set version on a schedule
|
||||||
|
go func() {
|
||||||
|
setVersion()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stopCh:
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
setVersion()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,11 +43,6 @@ var (
|
|||||||
watcher *fscache.Watcher
|
watcher *fscache.Watcher
|
||||||
)
|
)
|
||||||
|
|
||||||
type FluxConfig struct {
|
|
||||||
GitUrl string `mapstructure:"git-url"`
|
|
||||||
GitBranch string `mapstructure:"git-branch"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
HttpClientTimeout time.Duration `mapstructure:"http-client-timeout"`
|
HttpClientTimeout time.Duration `mapstructure:"http-client-timeout"`
|
||||||
HttpServerTimeout time.Duration `mapstructure:"http-server-timeout"`
|
HttpServerTimeout time.Duration `mapstructure:"http-server-timeout"`
|
||||||
@@ -181,25 +176,8 @@ func (s *Server) ListenAndServe(stopCh <-chan struct{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start redis connection pool
|
// start redis connection pool
|
||||||
s.startCachePool()
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
if s.pool != nil {
|
s.startCachePool(ticker, stopCh)
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-stopCh:
|
|
||||||
return
|
|
||||||
case <-ticker.C:
|
|
||||||
conn := s.pool.Get()
|
|
||||||
_, err := redis.String(conn.Do("PING"))
|
|
||||||
if err != nil {
|
|
||||||
s.logger.Warn("cache server is offline", zap.Error(err), zap.String("server", s.config.CacheServer))
|
|
||||||
}
|
|
||||||
_ = conn.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
// run server in background
|
// run server in background
|
||||||
go func() {
|
go func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user