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 }