18
18
package org .openqa .selenium .remote .http .netty ;
19
19
20
20
import com .google .auto .service .AutoService ;
21
+ import io .netty .util .HashedWheelTimer ;
22
+ import io .netty .util .Timer ;
21
23
import io .netty .util .concurrent .DefaultThreadFactory ;
22
24
import org .asynchttpclient .AsyncHttpClient ;
25
+ import org .asynchttpclient .AsyncHttpClientConfig ;
23
26
import org .asynchttpclient .DefaultAsyncHttpClientConfig ;
24
27
import org .asynchttpclient .Dsl ;
28
+ import org .asynchttpclient .config .AsyncHttpClientConfigDefaults ;
29
+ import org .asynchttpclient .netty .channel .DefaultChannelPool ;
25
30
import org .openqa .selenium .internal .Require ;
26
31
import org .openqa .selenium .remote .http .ClientConfig ;
27
32
import org .openqa .selenium .remote .http .Filter ;
33
38
import org .openqa .selenium .remote .http .WebSocket ;
34
39
35
40
import java .io .IOException ;
41
+ import java .util .Map ;
42
+ import java .util .concurrent .ThreadFactory ;
43
+ import java .util .concurrent .TimeUnit ;
36
44
import java .util .function .BiFunction ;
37
45
38
46
public class NettyClient implements HttpClient {
39
-
47
+ private final static Timer TIMER ;
48
+ static {
49
+ ThreadFactory threadFactory = new DefaultThreadFactory ("netty-client-timer" , true );
50
+ HashedWheelTimer timer = new HashedWheelTimer (
51
+ threadFactory ,
52
+ AsyncHttpClientConfigDefaults .defaultHashedWheelTimerTickDuration (),
53
+ TimeUnit .MILLISECONDS ,
54
+ AsyncHttpClientConfigDefaults .defaultHashedWheelTimerSize ());
55
+ timer .start ();
56
+ TIMER = timer ;
57
+ }
40
58
private final ClientConfig config ;
41
59
private final AsyncHttpClient client ;
42
60
private final HttpHandler handler ;
@@ -57,19 +75,21 @@ private AsyncHttpClient createHttpClient(ClientConfig config) {
57
75
.setAggregateWebSocketFrameFragments (true )
58
76
.setWebSocketMaxBufferSize (Integer .MAX_VALUE )
59
77
.setWebSocketMaxFrameSize (Integer .MAX_VALUE )
60
- .setConnectTimeout ((int ) config .connectionTimeout ().toMillis ());
61
-
62
- // String info = config.baseUrl().getUserInfo();
63
- // if (info != null && !info.equals("")) {
64
- // String[] parts = info.split(":", 2);
65
- // String user = parts[0];
66
- // String pass = parts.length > 1 ? parts[1] : null;
67
- // builder.setRealm(Dsl.basicAuthRealm(user, pass).setUsePreemptiveAuth(true).build());
68
- // }
78
+ .setNettyTimer (TIMER )
79
+ .setConnectTimeout (toClampedInt (config .connectionTimeout ().toMillis ()))
80
+ .setReadTimeout (toClampedInt (config .readTimeout ().toMillis ()));
69
81
70
82
return Dsl .asyncHttpClient (builder );
71
83
}
72
84
85
+ /**
86
+ * Converts a long to an int, clamping the maximum and minimum values to
87
+ * fit in an integer without overflowing.
88
+ */
89
+ private int toClampedInt (long value ) {
90
+ return (int ) Math .max (Integer .MIN_VALUE , Math .min (Integer .MAX_VALUE , value ));
91
+ }
92
+
73
93
@ Override
74
94
public HttpResponse execute (HttpRequest request ) {
75
95
return handler .execute (request );
0 commit comments