Skip to content

Instantly share code, notes, and snippets.

@Abdulsametileri
Created May 22, 2022 19:35
Show Gist options
  • Save Abdulsametileri/8f80356276cf889305ce7fd511a606d2 to your computer and use it in GitHub Desktop.
Save Abdulsametileri/8f80356276cf889305ce7fd511a606d2 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"sync/atomic"
)
type Application struct {
RequestCount uint64
SRegistry *ServiceRegistry
}
func (a *Application) Handle(w http.ResponseWriter, r *http.Request) {
atomic.AddUint64(&a.RequestCount, 1)
if a.SRegistry.Len() == 0 {
w.Write([]byte(`No backend entry in the service registry`))
return
}
backendIndex := int(atomic.LoadUint64(&a.RequestCount) % uint64(a.SRegistry.Len()))
fmt.Printf("Request routing to instance %d\n", backendIndex)
a.SRegistry.GetByIndex(backendIndex).
proxy.
ServeHTTP(w, r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment