Skip to content

Commit bd5a80a

Browse files
Issue 10245 - Implementation for Duration.toNanos() changes
1 parent 4531bc2 commit bd5a80a

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

api/src/main/java/io/grpc/CallOptions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.grpc;
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
20+
import static io.grpc.InternalTimeUtils.convert;
2021

2122
import com.google.common.base.MoreObjects;
2223
import com.google.common.base.Preconditions;
@@ -178,8 +179,7 @@ public CallOptions withDeadlineAfter(long duration, TimeUnit unit) {
178179
}
179180

180181
public CallOptions withDeadlineAfter(Duration duration) {
181-
return withDeadlineAfter(TimeUnit.NANOSECONDS.convert(duration.getSeconds(), TimeUnit.SECONDS),
182-
TimeUnit.NANOSECONDS);
182+
return withDeadlineAfter(convert(duration), TimeUnit.NANOSECONDS);
183183
}
184184

185185
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2024 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc;
18+
19+
import java.time.Duration;
20+
21+
@Internal
22+
public final class InternalTimeUtils {
23+
static long convert(Duration duration) {
24+
try {
25+
return duration.toNanos();
26+
} catch (ArithmeticException tooBig) {
27+
return duration.isNegative() ? Long.MIN_VALUE : Long.MAX_VALUE;
28+
}
29+
}
30+
}

api/src/main/java/io/grpc/SynchronizationContext.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.base.Preconditions.checkState;
21+
import static io.grpc.InternalTimeUtils.convert;
2122

2223
import java.lang.Thread.UncaughtExceptionHandler;
2324
import java.time.Duration;
@@ -197,8 +198,7 @@ public String toString() {
197198
public final ScheduledHandle scheduleWithFixedDelay(
198199
final Runnable task, Duration initialDelay, Duration delay,
199200
ScheduledExecutorService timerService) {
200-
return scheduleWithFixedDelay(task, TimeUnit.NANOSECONDS.convert(initialDelay.getSeconds(),
201-
TimeUnit.SECONDS), TimeUnit.NANOSECONDS.convert(delay.getSeconds(), TimeUnit.SECONDS),
201+
return scheduleWithFixedDelay(task, convert(initialDelay), convert(delay),
202202
TimeUnit.NANOSECONDS, timerService);
203203
}
204204

stub/src/main/java/io/grpc/stub/AbstractStub.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.grpc.stub;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static io.grpc.stub.InternalTimeUtils.convert;
2021

2122
import io.grpc.CallCredentials;
2223
import io.grpc.CallOptions;
@@ -151,8 +152,7 @@ public final S withDeadlineAfter(long duration, TimeUnit unit) {
151152
}
152153

153154
public final S withDeadlineAfter(Duration duration) {
154-
return withDeadlineAfter(TimeUnit.NANOSECONDS.convert(duration.getSeconds(),
155-
TimeUnit.SECONDS), TimeUnit.NANOSECONDS);
155+
return withDeadlineAfter(convert(duration), TimeUnit.NANOSECONDS);
156156
}
157157

158158
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2024 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.stub;
18+
19+
import io.grpc.Internal;
20+
import java.time.Duration;
21+
22+
@Internal
23+
public final class InternalTimeUtils {
24+
static long convert(Duration duration) {
25+
try {
26+
return duration.toNanos();
27+
} catch (ArithmeticException tooBig) {
28+
return duration.isNegative() ? Long.MIN_VALUE : Long.MAX_VALUE;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)