Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.

Commit cdbb9fa

Browse files
author
Stephane Landelle
committed
Don't use System.currentTimeMillis, master close AsyncHttpClient#280
1 parent ebb9c26 commit cdbb9fa

File tree

15 files changed

+49
-34
lines changed

15 files changed

+49
-34
lines changed

api/src/main/java/com/ning/http/client/Realm.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.ning.http.client;
1818

19+
import static com.ning.http.util.DateUtil.millisTime;
1920
import static com.ning.http.util.MiscUtil.isNonEmpty;
2021

2122
import java.io.UnsupportedEncodingException;
@@ -482,7 +483,7 @@ public RealmBuilder clone(Realm clone) {
482483
private void newCnonce() {
483484
try {
484485
MessageDigest md = MessageDigest.getInstance("MD5");
485-
byte[] b = md.digest(String.valueOf(System.currentTimeMillis()).getBytes("ISO-8859-1"));
486+
byte[] b = md.digest(String.valueOf(millisTime()).getBytes("ISO-8859-1"));
486487
cnonce = toHexString(b);
487488
} catch (Exception e) {
488489
throw new SecurityException(e);

api/src/main/java/com/ning/http/client/ntlm/NTLMEngine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
package com.ning.http.client.ntlm;
4040

41+
import static com.ning.http.util.DateUtil.millisTime;
4142
import com.ning.http.util.Base64;
4243

4344
import javax.crypto.Cipher;
@@ -513,7 +514,7 @@ private static byte[] createBlob(byte[] clientChallenge, byte[] targetInformatio
513514
byte[] blobSignature = new byte[]{(byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00};
514515
byte[] reserved = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
515516
byte[] unknown1 = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
516-
long time = System.currentTimeMillis();
517+
long time = millisTime();
517518
time += 11644473600000l; // milliseconds from January 1, 1601 -> epoch.
518519
time *= 10000; // tenths of a microsecond.
519520
// convert to little-endian byte array.

api/src/main/java/com/ning/http/client/oauth/OAuthSignatureCalculator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package com.ning.http.client.oauth;
1818

19-
19+
import static com.ning.http.util.DateUtil.millisTime;
2020
import com.ning.http.client.FluentStringsMap;
2121
import com.ning.http.client.Request;
2222
import com.ning.http.client.RequestBuilderBase;
@@ -78,15 +78,15 @@ public OAuthSignatureCalculator(ConsumerKey consumerAuth, RequestToken userAuth)
7878
mac = new ThreadSafeHMAC(consumerAuth, userAuth);
7979
this.consumerAuth = consumerAuth;
8080
this.userAuth = userAuth;
81-
random = new Random(System.identityHashCode(this) + System.currentTimeMillis());
81+
random = new Random(System.identityHashCode(this) + millisTime());
8282
}
8383

8484
//@Override // silly 1.5; doesn't allow this for interfaces
8585

8686
public void calculateAndAddSignature(String baseURL, Request request, RequestBuilderBase<?> requestBuilder) {
8787
String method = request.getMethod(); // POST etc
8888
String nonce = generateNonce();
89-
long timestamp = System.currentTimeMillis() / 1000L;
89+
long timestamp = millisTime() / 1000L;
9090
String signature = calculateSignature(method, baseURL, timestamp, nonce, request.getParams(), request.getQueryParams());
9191
String headerValue = constructAuthHeader(signature, nonce, timestamp);
9292
requestBuilder.setHeader(HEADER_AUTHORIZATION, headerValue);

api/src/main/java/com/ning/http/client/providers/jdk/JDKDelegateFuture.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.ning.http.client.providers.jdk;
1414

15+
import static com.ning.http.util.DateUtil.millisTime;
1516
import com.ning.http.client.AsyncHandler;
1617
import com.ning.http.client.ListenableFuture;
1718

@@ -67,7 +68,7 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
6768
content = innerFuture.get(timeout, unit);
6869
}
6970
} catch (Throwable t) {
70-
if (!contentProcessed.get() && timeout != -1 && ((System.currentTimeMillis() - touch.get()) <= responseTimeoutInMs)) {
71+
if (!contentProcessed.get() && timeout != -1 && ((millisTime() - touch.get()) <= responseTimeoutInMs)) {
7172
return get(timeout, unit);
7273
}
7374
timedOut.set(true);

api/src/main/java/com/ning/http/client/providers/jdk/JDKFuture.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.ning.http.client.providers.jdk;
1414

15+
import static com.ning.http.util.DateUtil.millisTime;
1516
import com.ning.http.client.AsyncHandler;
1617
import com.ning.http.client.listenable.AbstractListenableFuture;
1718
import org.slf4j.Logger;
@@ -40,7 +41,7 @@ public class JDKFuture<V> extends AbstractListenableFuture<V> {
4041
protected final AtomicBoolean timedOut = new AtomicBoolean(false);
4142
protected final AtomicBoolean isDone = new AtomicBoolean(false);
4243
protected final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
43-
protected final AtomicLong touch = new AtomicLong(System.currentTimeMillis());
44+
protected final AtomicLong touch = new AtomicLong(millisTime());
4445
protected final AtomicBoolean contentProcessed = new AtomicBoolean(false);
4546
protected final HttpURLConnection urlConnection;
4647
private boolean writeHeaders;
@@ -126,7 +127,7 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
126127
content = innerFuture.get(timeout, unit);
127128
}
128129
} catch (TimeoutException t) {
129-
if (!contentProcessed.get() && timeout != -1 && ((System.currentTimeMillis() - touch.get()) <= responseTimeoutInMs)) {
130+
if (!contentProcessed.get() && timeout != -1 && ((millisTime() - touch.get()) <= responseTimeoutInMs)) {
130131
return get(timeout, unit);
131132
}
132133

@@ -149,15 +150,15 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
149150
* @return <code>true</code> if response has expired and should be terminated.
150151
*/
151152
public boolean hasExpired() {
152-
return responseTimeoutInMs != -1 && ((System.currentTimeMillis() - touch.get()) > responseTimeoutInMs);
153+
return responseTimeoutInMs != -1 && ((millisTime() - touch.get()) > responseTimeoutInMs);
153154
}
154155

155156
/**
156157
* {@inheritDoc}
157158
*/
158159
/* @Override */
159160
public void touch() {
160-
touch.set(System.currentTimeMillis());
161+
touch.set(millisTime());
161162
}
162163

163164
/**

api/src/main/java/com/ning/http/util/AsyncHttpProviderUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.ning.http.util;
1414

15+
import static com.ning.http.util.DateUtil.millisTime;
1516
import java.io.ByteArrayInputStream;
1617
import java.io.FileNotFoundException;
1718
import java.io.IOException;
@@ -514,7 +515,7 @@ public static int convertExpireField(String timestring) {
514515
for (SimpleDateFormat sdf : simpleDateFormat.get()) {
515516
Date date = sdf.parse(trimmedTimeString, new ParsePosition(0));
516517
if (date != null) {
517-
long now = System.currentTimeMillis();
518+
long now = millisTime();
518519
long maxAgeMillis = date.getTime() - now;
519520
return (int) (maxAgeMillis / 1000) + (maxAgeMillis % 1000 != 0 ? 1 : 0);
520521
}

api/src/main/java/com/ning/http/util/DateUtil.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ public DateParseException() {
226226
public DateParseException(String message) {
227227
super(message);
228228
}
229-
230229
}
231230

231+
public static long millisTime() {
232+
return System.nanoTime() / 1000000;
233+
}
232234
}

api/src/test/java/com/ning/http/client/async/AsyncProvidersBasicTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ning.http.client.async;
1717

18+
import static com.ning.http.util.DateUtil.millisTime;
1819
import static org.testng.Assert.assertEquals;
1920
import static org.testng.Assert.assertNull;
2021
import static org.testng.Assert.assertTrue;
@@ -1591,7 +1592,7 @@ public void idleRequestTimeoutTest() throws Exception {
15911592
h.add("Content-Type", "application/x-www-form-urlencoded");
15921593
h.add("LockThread", "true");
15931594

1594-
long t1 = System.currentTimeMillis();
1595+
long t1 = millisTime();
15951596
try {
15961597
c.prepareGet(getTargetUrl()).setHeaders(h).setUrl(getTargetUrl()).execute(new AsyncHandlerAdapter() {
15971598

@@ -1603,7 +1604,7 @@ public void onThrowable(Throwable t) {
16031604
}).get();
16041605
Assert.fail();
16051606
} catch (Throwable ex) {
1606-
final long elapsedTime = System.currentTimeMillis() - t1;
1607+
final long elapsedTime = millisTime() - t1;
16071608
System.out.println("EXPIRED: " + (elapsedTime));
16081609
Assert.assertNotNull(ex.getCause());
16091610
Assert.assertTrue(elapsedTime >= 10000 && elapsedTime <= 25000);

api/src/test/java/com/ning/http/client/async/PerRequestTimeoutTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ning.http.client.async;
1717

18+
import static com.ning.http.util.DateUtil.millisTime;
1819
import com.ning.http.client.AsyncCompletionHandler;
1920
import com.ning.http.client.AsyncHttpClient;
2021
import com.ning.http.client.AsyncHttpClientConfig;
@@ -163,13 +164,13 @@ public Response onCompleted(Response response) throws Exception {
163164

164165
@Override
165166
public STATE onBodyPartReceived(HttpResponseBodyPart content) throws Exception {
166-
times[0] = System.currentTimeMillis();
167+
times[0] = millisTime();
167168
return super.onBodyPartReceived(content);
168169
}
169170

170171
@Override
171172
public void onThrowable(Throwable t) {
172-
times[1] = System.currentTimeMillis();
173+
times[1] = millisTime();
173174
super.onThrowable(t);
174175
}
175176
});

providers/apache/src/main/java/com/ning/http/client/providers/apache/ApacheResponseFuture.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.ning.http.client.providers.apache;
1414

15+
import static com.ning.http.util.DateUtil.millisTime;
1516
import com.ning.http.client.AsyncHandler;
1617
import com.ning.http.client.Request;
1718
import com.ning.http.client.listenable.AbstractListenableFuture;
@@ -41,7 +42,7 @@ public class ApacheResponseFuture<V> extends AbstractListenableFuture<V> {
4142
private final AtomicBoolean timedOut = new AtomicBoolean(false);
4243
private final AtomicBoolean isDone = new AtomicBoolean(false);
4344
private final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
44-
private final AtomicLong touch = new AtomicLong(System.currentTimeMillis());
45+
private final AtomicLong touch = new AtomicLong(millisTime());
4546
private final AtomicBoolean contentProcessed = new AtomicBoolean(false);
4647
private final Request request;
4748
private final HttpMethodBase method;
@@ -174,7 +175,7 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
174175
content = innerFuture.get(timeout, unit);
175176
}
176177
} catch (TimeoutException t) {
177-
if (!contentProcessed.get() && timeout != -1 && ((System.currentTimeMillis() - touch.get()) <= responseTimeoutInMs)) {
178+
if (!contentProcessed.get() && timeout != -1 && ((millisTime() - touch.get()) <= responseTimeoutInMs)) {
178179
return get(timeout, unit);
179180
}
180181

@@ -197,11 +198,11 @@ public V get(long timeout, TimeUnit unit) throws InterruptedException, Execution
197198
* @return <code>true</code> if response has expired and should be terminated.
198199
*/
199200
public boolean hasExpired() {
200-
return responseTimeoutInMs != -1 && ((System.currentTimeMillis() - touch.get()) >= responseTimeoutInMs);
201+
return responseTimeoutInMs != -1 && ((millisTime() - touch.get()) >= responseTimeoutInMs);
201202
}
202203

203204
public void touch() {
204-
touch.set(System.currentTimeMillis());
205+
touch.set(millisTime());
205206
}
206207

207208
public Request getRequest() {

providers/grizzly/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package com.ning.http.client.providers.grizzly;
1515

16+
import static com.ning.http.util.DateUtil.millisTime;
1617
import com.ning.org.jboss.netty.handler.codec.http.CookieDecoder;
1718
import com.ning.http.client.AsyncHandler;
1819
import com.ning.http.client.AsyncHttpClientConfig;
@@ -428,7 +429,7 @@ void touchConnection(final Connection c, final Request request) {
428429
int requestTimeout = AsyncHttpProviderUtils.requestTimeout(clientConfig, request);
429430
if (requestTimeout > 0) {
430431
if (resolver != null) {
431-
resolver.setTimeoutMillis(c, System.currentTimeMillis() + requestTimeout);
432+
resolver.setTimeoutMillis(c, millisTime() + requestTimeout);
432433
}
433434
}
434435

providers/grizzly/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyConnectionsPool.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package com.ning.http.client.providers.grizzly;
1515

16+
import static com.ning.http.util.DateUtil.millisTime;
1617
import com.ning.http.client.AsyncHttpClientConfig;
1718
import com.ning.http.client.ConnectionsPool;
1819

@@ -309,7 +310,7 @@ private class DelayedRunnable implements Runnable {
309310
@Override
310311
public void run() {
311312
while (isStarted) {
312-
final long currentTimeMs = System.currentTimeMillis();
313+
final long currentTimeMs = millisTime();
313314

314315
for (final IdleConnectionQueue delayQueue : queues) {
315316
if (delayQueue.queue.isEmpty()) continue;
@@ -380,7 +381,7 @@ public IdleConnectionQueue(final long timeout) {
380381

381382
void offer(final Connection c) {
382383
if (timeout >= 0) {
383-
resolver.setTimeoutMs(c, System.currentTimeMillis() + timeout);
384+
resolver.setTimeoutMs(c, millisTime() + timeout);
384385
}
385386
queue.offer(c);
386387
count.incrementAndGet();

providers/netty/src/main/java/com/ning/http/client/providers/netty/NettyConnectionsPool.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package com.ning.http.client.providers.netty;
1414

15+
import static com.ning.http.util.DateUtil.millisTime;
1516
import com.ning.http.client.ConnectionsPool;
1617
import org.jboss.netty.channel.Channel;
1718
import org.slf4j.Logger;
@@ -64,7 +65,7 @@ private static class IdleChannel {
6465
IdleChannel(String uri, Channel channel) {
6566
this.uri = uri;
6667
this.channel = channel;
67-
this.start = System.currentTimeMillis();
68+
this.start = millisTime();
6869
}
6970

7071
@Override
@@ -100,7 +101,7 @@ public void run() {
100101
}
101102

102103
List<IdleChannel> channelsInTimeout = new ArrayList<IdleChannel>();
103-
long currentTime = System.currentTimeMillis();
104+
long currentTime = millisTime();
104105

105106
for (IdleChannel idleChannel : channel2IdleChannel.values()) {
106107
long age = currentTime - idleChannel.start;
@@ -112,7 +113,7 @@ public void run() {
112113
channelsInTimeout.add(idleChannel);
113114
}
114115
}
115-
long endConcurrentLoop = System.currentTimeMillis();
116+
long endConcurrentLoop = millisTime();
116117

117118
for (IdleChannel idleChannel : channelsInTimeout) {
118119
Object attachment = idleChannel.channel.getPipeline().getContext(NettyAsyncHttpProvider.class).getAttachment();
@@ -139,7 +140,7 @@ public void run() {
139140
openChannels += hostChannels.size();
140141
}
141142
log.trace(String.format("%d channel open, %d idle channels closed (times: 1st-loop=%d, 2nd-loop=%d).\n",
142-
openChannels, channelsInTimeout.size(), endConcurrentLoop - currentTime, System.currentTimeMillis() - endConcurrentLoop));
143+
openChannels, channelsInTimeout.size(), endConcurrentLoop - currentTime, millisTime() - endConcurrentLoop));
143144
}
144145
} catch (Throwable t) {
145146
log.error("uncaught exception!", t);
@@ -159,9 +160,9 @@ public boolean offer(String uri, Channel channel) {
159160

160161
Long createTime = channel2CreationDate.get(channel);
161162
if (createTime == null){
162-
channel2CreationDate.putIfAbsent(channel, System.currentTimeMillis());
163+
channel2CreationDate.putIfAbsent(channel, millisTime());
163164
}
164-
else if (maxConnectionLifeTimeInMs != -1 && (createTime + maxConnectionLifeTimeInMs) < System.currentTimeMillis() ) {
165+
else if (maxConnectionLifeTimeInMs != -1 && (createTime + maxConnectionLifeTimeInMs) < millisTime() ) {
165166
log.debug("Channel {} expired", channel);
166167
return false;
167168
}

providers/netty/src/main/java/com/ning/http/client/providers/netty/NettyResponseFuture.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ning.http.client.providers.netty;
1717

18+
import static com.ning.http.util.DateUtil.millisTime;
1819
import com.ning.http.client.AsyncHandler;
1920
import com.ning.http.client.ConnectionPoolKeyStrategy;
2021
import com.ning.http.client.ProxyServer;
@@ -74,8 +75,8 @@ enum STATE {
7475
private volatile Future<?> reaperFuture;
7576
private final AtomicBoolean inAuth = new AtomicBoolean(false);
7677
private final AtomicBoolean statusReceived = new AtomicBoolean(false);
77-
private final AtomicLong touch = new AtomicLong(System.currentTimeMillis());
78-
private final long start = System.currentTimeMillis();
78+
private final AtomicLong touch = new AtomicLong(millisTime());
79+
private final long start = millisTime();
7980
private final NettyAsyncHttpProvider asyncHttpProvider;
8081
private final AtomicReference<STATE> state = new AtomicReference<STATE>(STATE.NEW);
8182
private final AtomicBoolean contentProcessed = new AtomicBoolean(false);
@@ -189,7 +190,7 @@ public boolean cancel(boolean force) {
189190
* @return <code>true</code> if response has expired and should be terminated.
190191
*/
191192
public boolean hasExpired() {
192-
long now = System.currentTimeMillis();
193+
long now = millisTime();
193194
return idleConnectionTimeoutInMs != -1 && ((now - touch.get()) >= idleConnectionTimeoutInMs)
194195
|| responseTimeoutInMs != -1 && ((now - start) >= responseTimeoutInMs);
195196
}
@@ -408,7 +409,7 @@ public boolean getAndSetStatusReceived(boolean sr) {
408409
*/
409410
/* @Override */
410411
public void touch() {
411-
touch.set(System.currentTimeMillis());
412+
touch.set(millisTime());
412413
}
413414

414415
/**

providers/netty/src/test/java/com/ning/http/client/providers/netty/NettyAsyncResponseTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package com.ning.http.client.providers.netty;
1515

16+
import static com.ning.http.util.DateUtil.millisTime;
1617
import com.ning.http.client.Cookie;
1718
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1819
import com.ning.http.client.HttpResponseHeaders;
@@ -38,7 +39,7 @@ public void testCookieParseExpires() {
3839
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
3940
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
4041

41-
Date date = new Date(System.currentTimeMillis() + 60000); // sdf.parse( dateString );
42+
Date date = new Date(millisTime() + 60000); // sdf.parse( dateString );
4243
final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date));
4344

4445
NettyResponse response = new NettyResponse(new ResponseStatus(null, null, null), new HttpResponseHeaders(null, null, false) {

0 commit comments

Comments
 (0)