refactoring
This commit is contained in:
parent
9f66851529
commit
718c39de00
|
@ -20,22 +20,31 @@ type EtcdGroupCacheHandler struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEtcdGroupCacheHandler(cfg *viper.Viper) (*EtcdGroupCacheHandler, error) {
|
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)
|
etcdHandler, err := NewEtcdHandler(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
pool := groupcache.NewHTTPPoolOpts("http://"+endpoint, &groupcache.HTTPPoolOptions{})
|
pool := groupcache.NewHTTPPoolOpts("", &groupcache.HTTPPoolOptions{})
|
||||||
server := http.Server{
|
for _, port := range ports {
|
||||||
Addr: endpoint,
|
pool.Set("http://" + endpoint + ":" + port)
|
||||||
Handler: pool,
|
server := &http.Server{
|
||||||
}
|
Addr: endpoint + ":" + port,
|
||||||
go func() {
|
Handler: pool,
|
||||||
log.Printf("Serving....\n")
|
|
||||||
if err := server.ListenAndServe(); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
}()
|
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(
|
group := groupcache.NewGroup("data", 3000000, groupcache.GetterFunc(
|
||||||
func(ctx context.Context, key string, dest groupcache.Sink) error {
|
func(ctx context.Context, key string, dest groupcache.Sink) error {
|
||||||
|
|
||||||
|
@ -50,7 +59,7 @@ func NewEtcdGroupCacheHandler(cfg *viper.Viper) (*EtcdGroupCacheHandler, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to decode value for key %s: %v", key, err)
|
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
|
return nil
|
||||||
},
|
},
|
||||||
))
|
))
|
|
@ -70,6 +70,8 @@ func createConfig() *viper.Viper {
|
||||||
cfg.Set("storage.kv.etcd.username", "")
|
cfg.Set("storage.kv.etcd.username", "")
|
||||||
cfg.Set("storage.kv.etcd.prefix", "parcoursmob/cache/")
|
cfg.Set("storage.kv.etcd.prefix", "parcoursmob/cache/")
|
||||||
cfg.Set("storage.kv.etcd.password", "")
|
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
|
return cfg
|
||||||
}
|
}
|
Loading…
Reference in New Issue