groupcache icon indicating copy to clipboard operation
groupcache copied to clipboard

getgroup from different main instances will it work ?

Open sabouaram opened this issue 2 years ago • 0 comments

` func NewGroupCacheHandler(cfg *viper.Viper) (*GroupCacheHandler, error) { 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 group *groupcache.Group )

var kvHandler KVHandler
var getterFunc groupcache.GetterFunc
kvType := cfg.GetString("storage.kv.groupcache.db")
getterFunc = func(ctx context.Context, key string, dest groupcache.Sink) error {

	resp, err := kvHandler.Get(key)
	if err != nil {
		return err
	}
	if resp == nil {
		return fmt.Errorf("key not found in etcd: %s", key)
	}
	jsonResp, _ := json.Marshal(resp)
	dest.SetBytes(jsonResp, time.Now().Add(time.Duration(cacheTTL)*time.Minute))
	return nil

}
switch kvType {
case "etcd":
	etcdHandler, err := NewEtcdHandler(cfg)
	if err != nil {
		return nil, err
	}
	kvHandler = etcdHandler
case "badger":
	badgerHandler, err := NewBadgerHandler(cfg)
	if err != nil {
		return nil, err
	}
	kvHandler = badgerHandler
default:
	return nil, fmt.Errorf("unsupported kvType: %s", kvType)
}

pool := groupcache.NewHTTPPool("")
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.GetGroup("data")
if group == nil {
	fmt.Println("Error getting group from groupcache:")
	group = groupcache.NewGroup("data", 3000000, getterFunc)
}

handler := &GroupCacheHandler{
	KVHandler: kvHandler,
	Group:     group,
	cacheTTL:  cacheTTL,
}

return handler, nil

} I am running multiple main instances that use this function what I need is to be able to reach cache instance from another instance the problem is when am doing getgroup it doesn't work anyone have a solution?

sabouaram avatar May 20 '23 00:05 sabouaram