Group Cache Behaviour

53 views
Skip to first unread message

Vallabh Chugh

unread,
Feb 7, 2024, 9:13:19 AM2/7/24
to golang-nuts
Hi folks, 
Fairly new to the k8s world. I am using group cache .I run a go routine to fetch all set of pods for a deployment and update my group cache pool if there is a change every 60 second. 
The problem i see is if that if there is a change in the pool (pods added) and if the incoming request goes to the newly added pod, sometimes it gives me a cache miss. and tries to reach the data source again. Is this normal? I was in the assumption that it should reach its peers 
The other strange thing is IT WORKS SOMETIMES, so is there a sync time needed? Sharing a small code snippet. 
Am i using pool.Set(podIPs...) in a wrong way?
func main() {
addr := os.Getenv("MY_POD_IP") + ":" + os.Getenv("MY_CONTAINER_PORT")
log.Println("Current Pod IP" + addr)
k8sClient, err := client.New(config.GetConfigOrDie(), client.Options{})
if err != nil {
panic(err)
}

namespace := "cache-poc"
var prevPodIPs []string
pool := groupcache.NewHTTPPool("http://" + addr)
// podIPs, err := getPodIPs(namespace, k8sClient)
// if err != nil {
// fmt.Println("Error getting pod IPs:", err)
// return
// }
// fmt.Println("Peers which are being set:", podIPs)
// pool.Set(podIPs...)

http.HandleFunc("/color", func(w http.ResponseWriter, r *http.Request) {
log.Println("Reached 1")
color := r.FormValue("name")
var b []byte
err := Group.Get(context.TODO(), color, groupcache.AllocatingByteSliceSink(&b))
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.Write(b)
w.Write([]byte{'\n'})
})

go func() {
for {
podIPs, err := getPodIPs(namespace, k8sClient)
if err != nil {
fmt.Println("Error getting pod IPs:", err)
return
}
if !reflect.DeepEqual(podIPs, prevPodIPs) {
fmt.Println("Pod IPs have changed:")
// Print or process the changes here
pool.Set(podIPs...)
prevPodIPs = podIPs
} else {
fmt.Println("No changes in Pod IPs.")
}

log.Printf("Pod IPs: %v\n", podIPs)

time.Sleep(60 * time.Second)
}

}()

http.HandleFunc("/set", setHandler)
//http.HandleFunc("/get", getHandler)
http.HandleFunc("/ack", ackMessage)

err = http.ListenAndServe(addr, nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
Reply all
Reply to author
Forward
0 new messages