Просмотр исходного кода

Improved speed of server selection

adolfintel 8 лет назад
Родитель
Сommit
64f614737d
2 измененных файлов с 13 добавлено и 11 удалено
  1. 12 10
      Frontend/server_selector.js
  2. 1 1
      Frontend/server_selector.min.js

+ 12 - 10
Frontend/server_selector.js

@@ -41,16 +41,18 @@ var PINGS=3, //up to 3 pings are performed, unless the server is down...
 function checkServer(server,done){
 	var i=0;
 	server.pingT=-1;
-	var nextPing=function(){
-		if(i++==PINGS){done(); return;}
-		ping(server.server+server.pingURL,function(t){
-			if(t>=0){
-				if(t<server.pingT||server.pingT==-1) server.pingT=t;
-				if(t<SLOW_THRESHOLD) nextPing(); else done();
-			}else done();
-		}.bind(this));
-	}.bind(this);
-	nextPing();
+	if(server.server.indexOf(location.protocol)==-1) done(); else{
+		var nextPing=function(){
+			if(i++==PINGS){done(); return;}
+			ping(server.server+server.pingURL,function(t){
+				if(t>=0){
+					if(t<server.pingT||server.pingT==-1) server.pingT=t;
+					if(t<SLOW_THRESHOLD) nextPing(); else done();
+				}else done();
+			}.bind(this));
+		}.bind(this);
+		nextPing();
+	}
 }
 
 /*this function goes through a list of servers, each with this format:

+ 1 - 1
Frontend/server_selector.min.js

@@ -1 +1 @@
-function ping(url,result){var xhr=new XMLHttpRequest,t=(new Date).getTime();xhr.onload=function(){if(0==xhr.responseText.length){var instspd=(new Date).getTime()-t;try{var p=performance.getEntries(),d=(p=p[p.length-1]).responseStart-p.requestStart;d<=0&&(d=p.duration),0<d&&d<instspd&&(instspd=d)}catch(e){}result(instspd)}else result(-1)}.bind(this),xhr.onerror=function(){result(-1)}.bind(this),xhr.open("GET",url),xhr.send()}var PINGS=3,SLOW_THRESHOLD=500;function checkServer(server,done){var i=0;server.pingT=-1;var nextPing=function(){i++!=PINGS?ping(server.server+server.pingURL,function(t){0<=t?((t<server.pingT||-1==server.pingT)&&(server.pingT=t),t<SLOW_THRESHOLD?nextPing():done()):done()}.bind(this)):done()}.bind(this);nextPing()}function selectServer(serverList,result){var i=0,done=function(){for(var bestServer=null,i=0;i<serverList.length;i++)-1!=serverList[i].pingT&&(null==bestServer||serverList[i].pingT<bestServer.pingT)&&(bestServer=serverList[i]);result(bestServer)}.bind(this),nextServer=function(){i!=serverList.length?checkServer(serverList[i++],nextServer):done()}.bind(this);nextServer()}var CONCURRENCY=4;function fastSelectServer(serverList,result){for(var serverLists=[],i=0;i<CONCURRENCY;i++)serverLists[i]=[];for(i=0;i<serverList.length;i++)serverLists[i%CONCURRENCY].push(serverList[i]);var completed=0,bestServer=null;for(i=0;i<CONCURRENCY;i++)selectServer(serverLists[i],function(server){null!=server&&(null==bestServer||server.pingT<bestServer.pingT)&&(bestServer=server),++completed==CONCURRENCY&&result(bestServer)}.bind(this))}
+function ping(url,result){var xhr=new XMLHttpRequest,t=(new Date).getTime();xhr.onload=function(){if(0==xhr.responseText.length){var instspd=(new Date).getTime()-t;try{var p=performance.getEntries(),d=(p=p[p.length-1]).responseStart-p.requestStart;d<=0&&(d=p.duration),0<d&&d<instspd&&(instspd=d)}catch(e){}result(instspd)}else result(-1)}.bind(this),xhr.onerror=function(){result(-1)}.bind(this),xhr.open("GET",url),xhr.send()}var PINGS=3,SLOW_THRESHOLD=500;function checkServer(server,done){var i=0;if((server.pingT=-1)==server.server.indexOf(location.protocol))done();else{var nextPing=function(){i++!=PINGS?ping(server.server+server.pingURL,function(t){0<=t?((t<server.pingT||-1==server.pingT)&&(server.pingT=t),t<SLOW_THRESHOLD?nextPing():done()):done()}.bind(this)):done()}.bind(this);nextPing()}}function selectServer(serverList,result){var i=0,done=function(){for(var bestServer=null,i=0;i<serverList.length;i++)-1!=serverList[i].pingT&&(null==bestServer||serverList[i].pingT<bestServer.pingT)&&(bestServer=serverList[i]);result(bestServer)}.bind(this),nextServer=function(){i!=serverList.length?checkServer(serverList[i++],nextServer):done()}.bind(this);nextServer()}var CONCURRENCY=4;function fastSelectServer(serverList,result){for(var serverLists=[],i=0;i<CONCURRENCY;i++)serverLists[i]=[];for(i=0;i<serverList.length;i++)serverLists[i%CONCURRENCY].push(serverList[i]);var completed=0,bestServer=null;for(i=0;i<CONCURRENCY;i++)selectServer(serverLists[i],function(server){null!=server&&(null==bestServer||server.pingT<bestServer.pingT)&&(bestServer=server),++completed==CONCURRENCY&&result(bestServer)}.bind(this))}