From 453087ed28196b74d7f2bbc8c74f4b9e488e2149 Mon Sep 17 00:00:00 2001 From: sergystepanov Date: Sat, 3 Oct 2020 18:53:46 +0300 Subject: [PATCH] Remove duplicate worker addresses during ping check (client, server) (#234) --- pkg/coordinator/handlers.go | 10 +++++++--- web/game.html | 2 +- web/js/controller.js | 31 ++++++++++++++++--------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/pkg/coordinator/handlers.go b/pkg/coordinator/handlers.go index af5e5ff7..27dc2d8d 100644 --- a/pkg/coordinator/handlers.go +++ b/pkg/coordinator/handlers.go @@ -342,14 +342,18 @@ func (o *Server) findBestServerFromBrowser(workerClients map[string]*WorkerClien // getLatencyMapFromBrowser get all latencies from worker to user func (o *Server) getLatencyMapFromBrowser(workerClients map[string]*WorkerClient, client *BrowserClient) map[*WorkerClient]int64 { - workersList := []*WorkerClient{} - addressList := []string{} + var workersList []*WorkerClient + var addressList []string + uniqueAddresses := map[string]bool{} latencyMap := map[*WorkerClient]int64{} // addressList is the list of worker addresses for _, workerClient := range workerClients { + if _, ok := uniqueAddresses[workerClient.PingServer]; !ok { + addressList = append(addressList, workerClient.PingServer) + } + uniqueAddresses[workerClient.PingServer] = true workersList = append(workersList, workerClient) - addressList = append(addressList, workerClient.PingServer) } // send this address to user and get back latency diff --git a/web/game.html b/web/game.html index c2e81398..b4896d1e 100644 --- a/web/game.html +++ b/web/game.html @@ -128,7 +128,7 @@ - + diff --git a/web/js/controller.js b/web/js/controller.js index 9026992a..1d067b33 100644 --- a/web/js/controller.js +++ b/web/js/controller.js @@ -77,21 +77,22 @@ } }; - const onLatencyCheckRequest = (data) => { - popup('Ping check...'); - const timeoutMs = 2000; + const onLatencyCheck = (data) => { + popup('Connecting to fastest server...'); + const timeoutMs = 1111; + // deduplicate + const addresses = [...new Set(data.addresses || [])]; - Promise.all((data.addresses || []) - .map(ip => { - const requestTime = Date.now(); - return ajax.fetch(`${ip}?_=${requestTime}`, {method: "GET", redirect: "follow"}, timeoutMs) - .then(() => ({[ip]: Date.now() - requestTime}), () => ({[ip]: timeoutMs})); - })) - .then(results => { - const latencies = Object.assign({}, ...results); - log.info('[ping] <->', latencies); - socket.latency(latencies, data.packetId); - }); + Promise.all(addresses.map(address => { + const start = Date.now(); + return ajax.fetch(`${address}?_=${start}`, {method: "GET", redirect: "follow"}, timeoutMs) + .then(() => ({[address]: Date.now() - start})) + .catch(() => ({[address]: 9999})); + })).then(servers => { + const latencies = Object.assign({}, ...servers); + log.info('[ping] <->', latencies); + socket.latency(latencies, data.packetId); + }); }; const helpScreen = { @@ -436,7 +437,7 @@ event.sub(MEDIA_STREAM_READY, () => rtcp.start()); event.sub(CONNECTION_READY, onConnectionReady); event.sub(CONNECTION_CLOSED, () => input.poll().disable()); - event.sub(LATENCY_CHECK_REQUESTED, onLatencyCheckRequest); + event.sub(LATENCY_CHECK_REQUESTED, onLatencyCheck); event.sub(GAMEPAD_CONNECTED, () => popup('Gamepad connected')); event.sub(GAMEPAD_DISCONNECTED, () => popup('Gamepad disconnected')); // touch stuff