Send EFA requests to multiple servers #2

Merged
paul merged 3 commits from feature/multiple-servers into main 2025-04-25 13:38:31 +02:00
Showing only changes of commit dcc3cfa44f - Show all commits

14
main.go
View File

@ -10,6 +10,7 @@ import "os"
import "slices"
import "strings"
import "strconv"
import "sync/atomic"
import "time"
// JSON unmarshaling types for departure monitor API
@ -110,8 +111,7 @@ func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) {
return req, nil
}
func FetchDepartures(stopId string) (DMResponse, error) {
var c EFAClient = KVVEFAClient{}
func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) {
req, err := c.BuildRequest(stopId)
if err != nil {
return DMResponse{}, err
@ -203,6 +203,13 @@ func main() {
panic("Required environment variable VRNP_PASSWORD is not set")
}
// Use round-robin to send incoming requests to different servers
var efaClient atomic.Uint64
efaClients := []EFAClient{
VRNEFAClient{},
KVVEFAClient{},
}
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !(ok && user == "admin" && pass == password) {
@ -224,7 +231,8 @@ func main() {
platform = &query["platform"][0]
}
ds, err := FetchDepartures(stopId)
c := efaClients[(efaClient.Add(1) - 1) % uint64(len(efaClients))]
ds, err := FetchDepartures(c, stopId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return