Send EFA requests to multiple servers #2
14
main.go
14
main.go
@ -10,6 +10,7 @@ import "os"
|
|||||||
import "slices"
|
import "slices"
|
||||||
import "strings"
|
import "strings"
|
||||||
import "strconv"
|
import "strconv"
|
||||||
|
import "sync/atomic"
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// JSON unmarshaling types for departure monitor API
|
// JSON unmarshaling types for departure monitor API
|
||||||
@ -110,8 +111,7 @@ func (c KVVEFAClient) BuildRequest(stopId string) (*http.Request, error) {
|
|||||||
return req, nil
|
return req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchDepartures(stopId string) (DMResponse, error) {
|
func FetchDepartures(c EFAClient, stopId string) (DMResponse, error) {
|
||||||
var c EFAClient = KVVEFAClient{}
|
|
||||||
req, err := c.BuildRequest(stopId)
|
req, err := c.BuildRequest(stopId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return DMResponse{}, err
|
return DMResponse{}, err
|
||||||
@ -203,6 +203,13 @@ func main() {
|
|||||||
panic("Required environment variable VRNP_PASSWORD is not set")
|
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) {
|
http.HandleFunc("/departures", func(w http.ResponseWriter, r *http.Request) {
|
||||||
user, pass, ok := r.BasicAuth()
|
user, pass, ok := r.BasicAuth()
|
||||||
if !(ok && user == "admin" && pass == password) {
|
if !(ok && user == "admin" && pass == password) {
|
||||||
@ -224,7 +231,8 @@ func main() {
|
|||||||
platform = &query["platform"][0]
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user