Skip to content

Commit

Permalink
Fix time offset handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sugmanue committed Oct 4, 2023
1 parent d5b787d commit e14811c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.function.Consumer;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkGlobalTime;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.internal.http.pipeline.RequestPipeline;
import software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder;
Expand All @@ -32,15 +31,15 @@
*/
@SdkInternalApi
public final class HttpClientDependencies implements SdkAutoCloseable {
private final ClockSkewAdjuster clockSkewAdjuster;
private final SdkClientConfiguration clientConfiguration;

/**
* Time offset may be mutated by {@link RequestPipeline} implementations if a clock skew is detected.
*/
private volatile int timeOffset = SdkGlobalTime.getGlobalTimeOffset();
private final SdkClientTime sdkClientTime;
private final ClockSkewAdjuster clockSkewAdjuster;
private final SdkClientConfiguration clientConfiguration;

private HttpClientDependencies(Builder builder) {
this.sdkClientTime = builder.sdkClientTime != null ? builder.sdkClientTime : new SdkClientTime();
this.clockSkewAdjuster = builder.clockSkewAdjuster != null ? builder.clockSkewAdjuster : new ClockSkewAdjuster();
this.clientConfiguration = paramNotNull(builder.clientConfiguration, "ClientConfiguration");
}
Expand All @@ -64,15 +63,14 @@ public ClockSkewAdjuster clockSkewAdjuster() {
* @return Current time offset. This is mutable and should not be cached.
*/
public int timeOffset() {
return timeOffset;
return sdkClientTime.getTimeOffset();
}

/**
* Updates the time offset of the client as well as the global time offset.
*/
public void updateTimeOffset(int timeOffset) {
this.timeOffset = timeOffset;
SdkGlobalTime.setGlobalTimeOffset(timeOffset);
sdkClientTime.setTimeOffset(timeOffset);
}

public Builder toBuilder() {
Expand All @@ -88,13 +86,15 @@ public void close() {
* Builder for {@link HttpClientDependencies}.
*/
public static class Builder {
private SdkClientTime sdkClientTime;
private ClockSkewAdjuster clockSkewAdjuster;
private SdkClientConfiguration clientConfiguration;

private Builder() {
}

private Builder(HttpClientDependencies from) {
this.sdkClientTime = from.sdkClientTime;
this.clientConfiguration = from.clientConfiguration;
this.clockSkewAdjuster = from.clockSkewAdjuster;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.core.internal.http;

import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkGlobalTime;
import software.amazon.awssdk.core.internal.http.pipeline.RequestPipeline;

/**
* Used for clock skew adjustment between the client JVM where the SDK is run,
* and the server side. This class mirrors {@link SdkGlobalTime} but it's local to the current client and only accessed
* internally by {@link HttpClientDependencies}.
*/
@SdkInternalApi
public final class SdkClientTime {
/**
* Time offset may be mutated by {@link RequestPipeline} implementations if a clock skew is detected.
*/
private volatile int timeOffset = SdkGlobalTime.getGlobalTimeOffset();

/**
* Gets the latest recorded time offset.
*/
public int getTimeOffset() {
return timeOffset;
}

/**
* Sets the latest recorded time offset.
*/
public void setTimeOffset(int timeOffset) {
this.timeOffset = timeOffset;
SdkGlobalTime.setGlobalTimeOffset(timeOffset);
}
}

0 comments on commit e14811c

Please sign in to comment.