From a0f162b91c5814eef927eb5d3a45b6a53efeeebf Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Sat, 24 Jul 2021 17:46:22 +0200 Subject: [PATCH] Rebuild of cache module: - Added new "fail-safe" memory-datasource - Moved some logic from Server to Router --- pkg/cache/datastore.go | 2 + pkg/cache/memory_datastore.go | 42 +++++++++++++++ pkg/cache/redis_datastore.go | 14 +++-- pkg/cache/response.go | 24 +++++++-- pkg/cache/route.go | 44 +++++++++++----- pkg/config/config.go | 14 +++-- pkg/server/response.go | 10 ++++ pkg/server/router.go | 83 +++++++++++++++++++++++++++++ pkg/server/server.go | 99 +++++++++++------------------------ 9 files changed, 235 insertions(+), 97 deletions(-) create mode 100644 pkg/cache/memory_datastore.go create mode 100644 pkg/server/response.go create mode 100644 pkg/server/router.go diff --git a/pkg/cache/datastore.go b/pkg/cache/datastore.go index f61593d..9208916 100644 --- a/pkg/cache/datastore.go +++ b/pkg/cache/datastore.go @@ -13,4 +13,6 @@ type CacheDatastore interface { SetKey(string, interface{}, int) error GetKey(string) (interface{}, error) + + IsConnected() bool } diff --git a/pkg/cache/memory_datastore.go b/pkg/cache/memory_datastore.go new file mode 100644 index 0000000..7c2a997 --- /dev/null +++ b/pkg/cache/memory_datastore.go @@ -0,0 +1,42 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package cache + +import ( + "errors" +) + +func NewMemoryDatastore() *MemoryDatastore { + return &MemoryDatastore{ + cache: make(map[string]interface{}), + } +} + +type MemoryDatastore struct { + cache map[string]interface{} +} + +func (ds *MemoryDatastore) SetKey(key string, data interface{}, ttl int) error { + ds.cache[key] = data + + return nil +} + +func (ds *MemoryDatastore) GetKey(key string) (interface{}, error) { + if data, ok := ds.cache[key]; ok { + return data, nil + } + + return nil, errors.New("Key not found" + key) +} + +func (ds *MemoryDatastore) IsConnected() bool { + return true +} diff --git a/pkg/cache/redis_datastore.go b/pkg/cache/redis_datastore.go index 4fac007..bf84819 100644 --- a/pkg/cache/redis_datastore.go +++ b/pkg/cache/redis_datastore.go @@ -23,13 +23,11 @@ func NewRedisDatastore(host string, port int) *RedisDatastore { Password: "", // FIXME: use env or param DB: 0, // FIXME: use env or param }), - cache: make(map[string]interface{}), } } type RedisDatastore struct { client *redis.Client - cache map[string]interface{} } func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { @@ -38,16 +36,10 @@ func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { return err } - // ds.cache[key] = data - return nil } func (ds *RedisDatastore) GetKey(key string) (interface{}, error) { - // if data, ok := ds.cache[key]; ok { - // return data, nil - // } - data, err := ds.client.Get(key).Result() if err != nil { return nil, err @@ -55,3 +47,9 @@ func (ds *RedisDatastore) GetKey(key string) (interface{}, error) { return data, nil } + +func (ds *RedisDatastore) IsConnected() bool { + _, err := ds.client.Ping().Result() + + return err == nil +} diff --git a/pkg/cache/response.go b/pkg/cache/response.go index c213157..52562e4 100644 --- a/pkg/cache/response.go +++ b/pkg/cache/response.go @@ -34,22 +34,35 @@ func NewResponseCacheManager(datastore CacheDatastore, ttl int) ResponseCacheMan } } -func (rm *ResponseCacheManager) Save(name string, r ResponseCache) { +func (rm *ResponseCacheManager) Save(name string, r ResponseCache) bool { + if !rm.datastore.IsConnected() { + log.Println("Response-cache:", "Not connected to server") + return false + } + data, err := json.Marshal(r) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Response-cache", "JSON encode:", err) + return false } name = rm.prefix + name err = rm.datastore.SetKey(name, string(data), rm.ttl) if err != nil { - log.Println("REDIS:", err, name) // FIXME + log.Println("Response-cache:", err, name) // FIXME + return false } + + return true } func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) { - name = rm.prefix + name + if !rm.datastore.IsConnected() { + log.Println("Response-cache:", "Not connected to server") + return false, &ResponseCache{} + } + name = rm.prefix + name data, err := rm.datastore.GetKey(name) if err != nil { return false, &ResponseCache{} @@ -58,7 +71,8 @@ func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) { rc := &ResponseCache{} err = json.Unmarshal([]byte(data.(string)), &rc) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Response-cache:", "JSON docode:", err) + return false, &ResponseCache{} } return true, rc diff --git a/pkg/cache/route.go b/pkg/cache/route.go index ff28a1c..b79820f 100644 --- a/pkg/cache/route.go +++ b/pkg/cache/route.go @@ -21,42 +21,58 @@ type RouteCache struct { type RouteCacheManager struct { datastore CacheDatastore + prefix string ttl int } func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager { return RouteCacheManager{ datastore: datastore, + prefix: "route_", ttl: ttl, } } -func (rm *RouteCacheManager) Save(name string, r RouteCache) { +func (rm *RouteCacheManager) Save(name string, r RouteCache) bool { + if !rm.datastore.IsConnected() { + log.Println("Route-cache:", "Not connected to server") + return false + } + data, err := json.Marshal(r) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Route-cache", "JSON encode:", err) + return false } - err = rm.datastore.SetKey("route_"+name, data, rm.ttl) + name = rm.prefix + name + err = rm.datastore.SetKey(name, data, rm.ttl) if err != nil { - log.Println("REDIS:", err, name) // FIXME + log.Println("Route-cache:", "Unable to save", name, err) + return false } + + return true } -func (rm *RouteCacheManager) Load(name string) (bool, RouteCache) { - name = "route_" + name - - data, err := rm.datastore.GetKey(name) - if err != nil { - log.Println("REDIS:", err, name) // FIXME - - return false, RouteCache{} +func (rm *RouteCacheManager) Load(name string) (bool, *RouteCache) { + if !rm.datastore.IsConnected() { + log.Println("Route-cache:", "Not connected to server") + return false, &RouteCache{} } - rc := RouteCache{} + name = rm.prefix + name + data, err := rm.datastore.GetKey(name) + if err != nil { + log.Println("Route-cache:", "Unable to load", name, err) + return false, &RouteCache{} + } + + rc := &RouteCache{} err = json.Unmarshal([]byte(data.(string)), &rc) if err != nil { - log.Println("JSON:", err) // FIXME + log.Println("Route-cache:", "JSON decode:", err) + return false, &RouteCache{} } return true, rc diff --git a/pkg/config/config.go b/pkg/config/config.go index 6fb1785..23b6fff 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -18,6 +18,8 @@ import ( type Config struct { Server Server Backends map[string]Backend + + confPath string } type Server struct { @@ -42,12 +44,18 @@ var DefaultRoute = Route{ Target: "", } -func (c *Config) Load(cPath string) error { - if _, err := os.Stat(cPath); err != nil { +func New(confPath string) *Config { + return &Config{ + confPath: confPath, + } +} + +func (c *Config) Load() error { + if _, err := os.Stat(c.confPath); err != nil { return err } - data, err := ioutil.ReadFile(cPath) + data, err := ioutil.ReadFile(c.confPath) if err != nil { return err } diff --git a/pkg/server/response.go b/pkg/server/response.go new file mode 100644 index 0000000..8dbb0e4 --- /dev/null +++ b/pkg/server/response.go @@ -0,0 +1,10 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package server diff --git a/pkg/server/router.go b/pkg/server/router.go new file mode 100644 index 0000000..481824d --- /dev/null +++ b/pkg/server/router.go @@ -0,0 +1,83 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package server + +import ( + "fmt" + "regexp" + "strings" + "vegvisir/pkg/cache" + "vegvisir/pkg/config" +) + +type Router struct { + config *config.Config + rcm cache.RouteCacheManager // Redis cache + rCache map[string]cache.RouteCache // Internal route cache - TMP? +} + +type Route struct { + SourceUrl string + TargetUrl string +} + +func NewRouter(conf *config.Config, cacheDS cache.CacheDatastore, ttl int) *Router { + return &Router{ + config: conf, + rcm: cache.NewRouteCacheManager(cacheDS, ttl), + rCache: make(map[string]cache.RouteCache), + } +} + +func (r *Router) FindByRequestURL(url []byte) (bool, Route) { + var sUrl string = string(url) + + for bId := range r.config.Backends { + bck := r.config.Backends[bId] + if !strings.Contains(sUrl, bck.PrefixUrl) { + continue + } + + for rId := range bck.Routes { + routeCfg := &bck.Routes[rId] + + // if ok, cRoute := s.rCacheManager.Load(sUri); ok { + // return true, cRoute + // } + if cRoute, ok := r.rCache[sUrl]; ok { + route := Route{ + SourceUrl: cRoute.SourceUrl, + TargetUrl: cRoute.TargetUrl, + } + return true, route + } + + rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixUrl, routeCfg.Pattern)) + if rgxp.Match(url) { + targetUrl := bck.BackendAddress + rgxp.ReplaceAllString(sUrl, routeCfg.Target) + + route := Route{ + SourceUrl: sUrl, + TargetUrl: targetUrl, + } + + // s.rCacheManager.Save(sUri, cRoute) + r.rCache[sUrl] = cache.RouteCache{ + SourceUrl: route.SourceUrl, + TargetUrl: route.TargetUrl, + } + + return true, route + } + } + } + + return false, Route{} +} diff --git a/pkg/server/server.go b/pkg/server/server.go index d61b2fc..31790d8 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -15,8 +15,6 @@ import ( "log" "os" "os/signal" - "regexp" - "strings" "time" "vegvisir/pkg/cache" "vegvisir/pkg/config" @@ -30,32 +28,40 @@ const ( ) type Server struct { - Config config.Config - - cFilePath string - rCache map[string]cache.RouteCache // Internal route cache - routeCM cache.RouteCacheManager // Redis route cache - respCM cache.ResponseCacheManager // Redis response cache + config *config.Config + router *Router + respCM cache.ResponseCacheManager // Redis response cache } func NewServer(cPath string) *Server { - datastore := cache.NewRedisDatastore("127.0.0.7", 6379) // FIXME use config or env... + config := config.New(cPath) - return &Server{ - cFilePath: cPath, - rCache: make(map[string]cache.RouteCache), - routeCM: cache.NewRouteCacheManager(datastore, 30), //FIXME use ttl(seconds) from config or env... - respCM: cache.NewResponseCacheManager(datastore, 30), //FIXME use ttl(seconds) from config or env... + if err := config.Load(); err != nil { + log.Fatalln("Unable to find config file: ", cPath, err) } + + server := &Server{ + config: config, + } + + 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() { - if err := s.Config.Load(s.cFilePath); err != nil { - log.Fatalln("Unable to find config file: ", s.cFilePath, err) - } - go func() { - serverAddress := s.Config.Server.Address + ":" + fmt.Sprint(s.Config.Server.Port) + serverAddress := s.config.Server.Address + ":" + fmt.Sprint(s.config.Server.Port) if err := fasthttp.ListenAndServe(serverAddress, s.mainHandler); err != nil { log.Fatalf("Server panic! Error message: %s", err) } @@ -86,10 +92,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { ctx.Response.Header.Add(fasthttp.HeaderServer, Name) // move all below logic to concrete handler or sth.... - reqUri, sReqUri, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) - log.Println("Incomming request:", sReqMethod, sReqUri) + reqUrl, sReqUrl, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) + log.Println("Incomming request:", sReqMethod, sReqUrl) - found, route := s.findRouteByRequestURI(reqUri) + found, route := s.router.FindByRequestURL(reqUrl) if !found { // FIXME: return 404/5xx error in repsonse, maybe define it in Backend config? ctx.SetStatusCode(fasthttp.StatusNotFound) @@ -97,10 +103,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { } // handle response caching - if ok, data := s.respCM.Load(sReqUri); ok { + if ok, data := s.respCM.Load(sReqUrl); ok { log.Println("Read resp from cache: ", route.TargetUrl) - ctx.SetBody([]byte(data.Body)) + ctx.SetBody([]byte(data.Body)) // FIXME missing headers etc... } else { log.Println("Send req to backend url: ", route.TargetUrl) @@ -126,51 +132,10 @@ func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { // save response to cache respCache := cache.ResponseCache{ - URL: sReqUri, + URL: sReqUrl, Body: string(bckResp.Body()), // Headers: [] } // FIXME: prepare resp cache struct in respCM.Save method or other service... - s.respCM.Save(sReqUri, respCache) + s.respCM.Save(sReqUrl, respCache) } } - -func (s *Server) findRouteByRequestURI(uri []byte) (bool, cache.RouteCache) { - var sUri string = string(uri) - - for bId := range s.Config.Backends { - bck := s.Config.Backends[bId] - if !strings.Contains(sUri, bck.PrefixUrl) { - continue - } - - for rId := range bck.Routes { - route := &bck.Routes[rId] - - // if ok, cRoute := s.rCacheManager.Load(sUri); ok { - // return true, cRoute - // } - if cRoute, ok := s.rCache[sUri]; ok { - return true, cRoute - } - - rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixUrl, route.Pattern)) - if rgxp.Match(uri) { - targetUrl := bck.BackendAddress + rgxp.ReplaceAllString(sUri, route.Target) - - cRoute := cache.RouteCache{ // FIXME: data duplication and use short alias for backend and route! - // Backend: bck, - // Route: *route, - SourceUrl: sUri, - TargetUrl: targetUrl, - } - - // s.rCacheManager.Save(sUri, cRoute) - s.rCache[sUri] = cRoute - - return true, cRoute - } - } - } - - return false, cache.RouteCache{} -} -- 2.39.5