Created
May 22, 2022 19:35
-
-
Save Abdulsametileri/8f80356276cf889305ce7fd511a606d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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