[refactor] Simplified selecting cache datasource and fail-safe mechanism
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
80a0cba90f
commit
58eaaef0b3
39
pkg/cache/cache.go
vendored
Normal file
39
pkg/cache/cache.go
vendored
Normal file
@ -0,0 +1,39 @@
|
||||
// ___ ____ ___ ___
|
||||
// \ \ / / | _ | __| \ \ / / || | __ || || _ |
|
||||
// \ \/ / |___ | |__ \ \/ / || |___ || ||___|
|
||||
// \ / | _ | _ | \ / || __ | || ||\\
|
||||
// \/ |___ |___ | \/ || ____| || || \\
|
||||
//
|
||||
// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License
|
||||
// Repo: https://git.pbiernat.dev/golang/vegvisir
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"log"
|
||||
"vegvisir/pkg/config"
|
||||
)
|
||||
|
||||
const (
|
||||
TYPE_REDIS = "redis"
|
||||
TYPE_MEMORY = "memory"
|
||||
)
|
||||
|
||||
func GetCacheDatastore(config config.Cache) *CacheDatastore {
|
||||
var datastore CacheDatastore
|
||||
|
||||
if config.Type == TYPE_REDIS {
|
||||
datastore = NewRedisDatastore(config.Host, config.Port)
|
||||
} else {
|
||||
datastore = NewMemoryDatastore()
|
||||
}
|
||||
|
||||
//fail-safe switch to memory datasource
|
||||
if !datastore.IsConnected() {
|
||||
log.Println("Cache server is not responding, switching to memory cache.")
|
||||
|
||||
datastore = NewMemoryDatastore()
|
||||
}
|
||||
|
||||
return &datastore
|
||||
}
|
@ -17,6 +17,7 @@ import (
|
||||
|
||||
type Config struct {
|
||||
Server Server
|
||||
Cache Cache
|
||||
Backends map[string]Backend
|
||||
|
||||
confPath string
|
||||
@ -27,6 +28,17 @@ type Server struct {
|
||||
Port int
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
Type string
|
||||
Host string
|
||||
Port int
|
||||
Username string
|
||||
Password string
|
||||
Database string
|
||||
RouteTtl int
|
||||
ResponseTtl int
|
||||
}
|
||||
|
||||
type Backend struct {
|
||||
PrefixUrl string
|
||||
BackendAddress string
|
||||
|
@ -19,15 +19,13 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
cFile = flag.String("c", "vegvisir.json", "Path to config file")
|
||||
|
||||
cPath = flag.String("c", "vegvisir.json", "Path to config file")
|
||||
// for profiling...
|
||||
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
|
||||
memprofile = flag.String("memprofile", "", "write memory profile to `file`")
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
flag.Parse()
|
||||
// cpu profiling
|
||||
if *cpuprofile != "" {
|
||||
@ -42,7 +40,7 @@ func main() {
|
||||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
server.NewServer(*cFile).Run()
|
||||
server.NewServer(*cPath).Run()
|
||||
|
||||
// memory profiling
|
||||
if *memprofile != "" {
|
||||
|
@ -40,23 +40,13 @@ func NewServer(cPath string) *Server {
|
||||
log.Fatalln("Unable to find config file: ", cPath, err)
|
||||
}
|
||||
|
||||
server := &Server{
|
||||
datastore := cache.GetCacheDatastore(config.Cache)
|
||||
|
||||
return &Server{
|
||||
config: config,
|
||||
router: NewRouter(config, *datastore, config.Cache.RouteTtl),
|
||||
respCM: cache.NewResponseCacheManager(*datastore, config.Cache.ResponseTtl),
|
||||
}
|
||||
|
||||
redisDS := cache.NewRedisDatastore("127.0.0.1", 6379) // FIXME use config or env...
|
||||
if !redisDS.IsConnected() {
|
||||
log.Println("Redis server not responding, switching to memory cache...")
|
||||
|
||||
memDS := cache.NewMemoryDatastore()
|
||||
server.router = NewRouter(config, memDS, 30) //FIXME for memory datasource ttl is useles right now...
|
||||
server.respCM = cache.NewResponseCacheManager(memDS, 30) //FIXME for memory datasource ttl is useles right now...
|
||||
} else {
|
||||
server.router = NewRouter(config, redisDS, 30) //FIXME use ttl(seconds) from config or env...
|
||||
server.respCM = cache.NewResponseCacheManager(redisDS, 30) //FIXME use ttl(seconds) from config or env...
|
||||
}
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *Server) Run() {
|
||||
|
@ -29,5 +29,15 @@
|
||||
"target": "article-global/$1"
|
||||
}]
|
||||
}
|
||||
},
|
||||
"cache": {
|
||||
"type": "redis",
|
||||
"host": "localhost",
|
||||
"port": 6379,
|
||||
"username": "",
|
||||
"password": "",
|
||||
"database": "0",
|
||||
"routeTtl": 300,
|
||||
"responseTtl": 300
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user