Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getgroup from different main instances will it work ? #58

Open
swbo97 opened this issue May 20, 2023 · 0 comments
Open

getgroup from different main instances will it work ? #58

swbo97 opened this issue May 20, 2023 · 0 comments

Comments

@swbo97
Copy link

swbo97 commented May 20, 2023

` 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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant