2020import java .util .List ;
2121import java .util .Map ;
2222import java .util .concurrent .*;
23+ import java .util .stream .Collectors ;
2324
2425public class AssetUtil {
2526 private static final String CFPA_ASSET_ROOT = "http://downloader1.meitangdehulu.com:22943/" ;
@@ -45,51 +46,30 @@ public static String getString(String url) throws IOException, URISyntaxExceptio
4546 }
4647
4748 public static String getFastestUrl () {
48- List <String > urls = new ArrayList <>();
49-
50- // 根据地理位置选择源列表
51- if (LocationDetectUtil .isMainlandChina ()) {
52- // 中国大陆:测速选择最快的国内源
53- urls .addAll (MIRRORS );
54- urls .add (CFPA_ASSET_ROOT );
55- Log .info ("Inside mainland China: Testing mirrors..." );
56- } else {
57- // 海外用户:直接使用 GitHub 源
58- urls .add (GITHUB );
59- Log .info ("Outside mainland China: Using GitHub source..." );
49+ // 海外用户直接用 GitHub,不测速
50+ if (!LocationDetectUtil .isMainlandChina ()) {
51+ Log .info ("Outside mainland China: Using GitHub source" );
52+ return GITHUB ;
6053 }
6154
62- ExecutorService executor = Executors .newFixedThreadPool (Math .max (urls .size (), 10 ));
63- try {
64- List <CompletableFuture <String >> futures = new ArrayList <>();
65- for (String url : urls ) {
66- futures .add (CompletableFuture .supplyAsync (() -> {
67- try {
68- return testUrlConnection (url );
69- } catch (IOException e ) {
70- return null ;
71- }
72- }, executor ));
73- }
74-
75- String fastest = null ;
76- while (!futures .isEmpty ()) {
77- fastest = (String ) CompletableFuture .anyOf (futures .toArray (new CompletableFuture [0 ])).join ();
78- futures .removeIf (CompletableFuture ::isDone );
79- if (fastest != null ) {
80- for (CompletableFuture <String > f : futures ) {
81- f .cancel (true );
82- }
83- Log .info ("Using fastest url: %s" , fastest );
84- return fastest ;
85- }
86- }
55+ // 中国大陆:测速选择最快的国内源
56+ Log .info ("Inside mainland China: Testing mirrors..." );
57+ List <String > urls = new ArrayList <>(MIRRORS );
58+ urls .add (CFPA_ASSET_ROOT );
8759
60+ ExecutorService executor = Executors .newCachedThreadPool ();
61+ try {
62+ String fastest = executor .invokeAny (
63+ urls .stream ().map (url -> (Callable <String >) () -> testUrlConnection (url )).collect (Collectors .toList ()),
64+ 10 , TimeUnit .SECONDS
65+ );
66+ executor .shutdownNow ();
67+ Log .info ("Using fastest url: %s" , fastest );
68+ return fastest ;
69+ } catch (Exception e ) {
70+ executor .shutdownNow ();
8871 Log .info ("All sources unreachable, using CFPA_ASSET_ROOT as fallback" );
8972 return CFPA_ASSET_ROOT ;
90-
91- } finally {
92- executor .shutdownNow ();
9373 }
9474 }
9575
0 commit comments