Skip to content

Commit

Permalink
fix: remove currentTimeMillis from code, tests are OK (#1617)
Browse files Browse the repository at this point in the history
  • Loading branch information
davecramer committed Nov 26, 2019
1 parent 9a193de commit ff4a66d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/Driver.java
Expand Up @@ -397,7 +397,7 @@ public void run() {
* @throws SQLException if a connection error occurs or the timeout is reached
*/
public Connection getResult(long timeout) throws SQLException {
long expiry = System.currentTimeMillis() + timeout;
long expiry = System.nanoTime() / 1000 + timeout;
synchronized (this) {
while (true) {
if (result != null) {
Expand All @@ -416,7 +416,7 @@ public Connection getResult(long timeout) throws SQLException {
}
}

long delay = expiry - System.currentTimeMillis();
long delay = expiry - System.nanoTime() / 1000;
if (delay <= 0) {
abandoned = true;
throw new PSQLException(GT.tr("Connection attempt timed out."),
Expand Down
Expand Up @@ -717,7 +717,7 @@ public synchronized void processNotifies(int timeoutMillis) throws SQLException
long startTime = 0;
int oldTimeout = 0;
if (useTimeout) {
startTime = System.currentTimeMillis();
startTime = System.nanoTime() / 1000;
try {
oldTimeout = pgStream.getSocket().getSoTimeout();
} catch (SocketException e) {
Expand Down Expand Up @@ -747,7 +747,7 @@ public synchronized void processNotifies(int timeoutMillis) throws SQLException
SQLWarning warning = receiveNoticeResponse();
addWarning(warning);
if (useTimeout) {
long newTimeMillis = System.currentTimeMillis();
long newTimeMillis = System.nanoTime() / 1000;
timeoutMillis += startTime - newTimeMillis; // Overflows after 49 days, ignore that
startTime = newTimeMillis;
if (timeoutMillis == 0) {
Expand Down
Expand Up @@ -5,8 +5,6 @@

package org.postgresql.hostchooser;

import static java.lang.System.currentTimeMillis;

import org.postgresql.util.HostSpec;

import java.util.ArrayList;
Expand All @@ -28,7 +26,7 @@ public class GlobalHostStatusTracker {
* @param hostStatus Latest known status for the host.
*/
public static void reportHostStatus(HostSpec hostSpec, HostStatus hostStatus) {
long now = currentTimeMillis();
long now = System.nanoTime() / 1000;
synchronized (hostStatusMap) {
HostSpecStatus hostSpecStatus = hostStatusMap.get(hostSpec);
if (hostSpecStatus == null) {
Expand All @@ -51,7 +49,7 @@ public static void reportHostStatus(HostSpec hostSpec, HostStatus hostStatus) {
static List<HostSpec> getCandidateHosts(HostSpec[] hostSpecs,
HostRequirement targetServerType, long hostRecheckMillis) {
List<HostSpec> candidates = new ArrayList<HostSpec>(hostSpecs.length);
long latestAllowedUpdate = currentTimeMillis() - hostRecheckMillis;
long latestAllowedUpdate = System.nanoTime() / 1000 - hostRecheckMillis;
synchronized (hostStatusMap) {
for (HostSpec hostSpec : hostSpecs) {
HostSpecStatus hostInfo = hostStatusMap.get(hostSpec);
Expand Down

0 comments on commit ff4a66d

Please sign in to comment.