From 718c39de006e749dd3ffab3f8840f7921f4e0b78 Mon Sep 17 00:00:00 2001 From: sbouaram Date: Tue, 16 May 2023 08:40:05 +0200 Subject: [PATCH] refactoring --- .../{groupcache.go => groupcache_etcd.go} | 33 ++++++++++++------- ...pcache_test.go => groupcache_etcd_test.go} | 4 ++- 2 files changed, 24 insertions(+), 13 deletions(-) rename utils/storage/{groupcache.go => groupcache_etcd.go} (70%) rename utils/storage/{groupcache_test.go => groupcache_etcd_test.go} (91%) diff --git a/utils/storage/groupcache.go b/utils/storage/groupcache_etcd.go similarity index 70% rename from utils/storage/groupcache.go rename to utils/storage/groupcache_etcd.go index 0e81c2d..3cc0642 100644 --- a/utils/storage/groupcache.go +++ b/utils/storage/groupcache_etcd.go @@ -20,22 +20,31 @@ type EtcdGroupCacheHandler struct { } func NewEtcdGroupCacheHandler(cfg *viper.Viper) (*EtcdGroupCacheHandler, error) { - var endpoint = cfg.GetString("storage.kv.groupcache.endpoint") + var ( + endpoint = cfg.GetString("storage.kv.groupcache.endpoint") + ports = cfg.GetStringSlice("storage.kv.groupcache.ports") + cacheTTL = cfg.GetInt64("storage.kv.groupcache.cacheTTL") + cacheServers []*http.Server + ) etcdHandler, err := NewEtcdHandler(cfg) if err != nil { return nil, err } - pool := groupcache.NewHTTPPoolOpts("http://"+endpoint, &groupcache.HTTPPoolOptions{}) - server := http.Server{ - Addr: endpoint, - Handler: pool, - } - go func() { - log.Printf("Serving....\n") - if err := server.ListenAndServe(); err != nil { - log.Fatal(err) + pool := groupcache.NewHTTPPoolOpts("", &groupcache.HTTPPoolOptions{}) + for _, port := range ports { + pool.Set("http://" + endpoint + ":" + port) + server := &http.Server{ + Addr: endpoint + ":" + port, + Handler: pool, } - }() + cacheServers = append(cacheServers, server) + go func(srv *http.Server) { + log.Printf("Serving cache server %s \n", srv.Addr) + if err := srv.ListenAndServe(); err != nil { + log.Fatal(err) + } + }(server) + } group := groupcache.NewGroup("data", 3000000, groupcache.GetterFunc( func(ctx context.Context, key string, dest groupcache.Sink) error { @@ -50,7 +59,7 @@ func NewEtcdGroupCacheHandler(cfg *viper.Viper) (*EtcdGroupCacheHandler, error) if err != nil { return fmt.Errorf("failed to decode value for key %s: %v", key, err) } - dest.SetString(string(decoded), time.Now().Add(time.Minute*5)) + dest.SetString(string(decoded), time.Now().Add(time.Duration(cacheTTL)*time.Minute)) return nil }, )) diff --git a/utils/storage/groupcache_test.go b/utils/storage/groupcache_etcd_test.go similarity index 91% rename from utils/storage/groupcache_test.go rename to utils/storage/groupcache_etcd_test.go index fb875c1..818f381 100644 --- a/utils/storage/groupcache_test.go +++ b/utils/storage/groupcache_etcd_test.go @@ -70,6 +70,8 @@ func createConfig() *viper.Viper { cfg.Set("storage.kv.etcd.username", "") cfg.Set("storage.kv.etcd.prefix", "parcoursmob/cache/") cfg.Set("storage.kv.etcd.password", "") - cfg.Set("storage.kv.groupcache.endpoint", "localhost:8080") + cfg.Set("storage.kv.groupcache.endpoint", "localhost") + cfg.Set("storage.kv.groupcache.ports", []string{"36000", "36500"}) + cfg.Set("storage.kv.groupcache.cacheTTL", 10) return cfg }