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 5ca9343
Show file tree
Hide file tree
Showing 2 changed files with 54 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 TimeOffsetKeeper timeOffsetKeeper;
private final ClockSkewAdjuster clockSkewAdjuster;
private final SdkClientConfiguration clientConfiguration;

private HttpClientDependencies(Builder builder) {
this.timeOffsetKeeper = builder.timeOffsetKeeper != null ? builder.timeOffsetKeeper : new TimeOffsetKeeper();
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 timeOffsetKeeper.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);
timeOffsetKeeper.setTimeOffset(timeOffset);
}

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

private Builder() {
}

private Builder(HttpClientDependencies from) {
this.timeOffsetKeeper = from.timeOffsetKeeper;
this.clientConfiguration = from.clientConfiguration;
this.clockSkewAdjuster = from.clockSkewAdjuster;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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;

/**
* Keeps track of the latest known time offset, this value set during the retry stage and read in the signing stage.
*/
@SdkInternalApi
public final class TimeOffsetKeeper {
/**
* 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 5ca9343

Please sign in to comment.