Skip to content

getgroup from different main instances will it work ? #58

@sabouaram

Description

@sabouaram

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions