11package org .hypertrace .core .grpcutils .client ;
22
3- import static java .time .temporal .ChronoUnit .MINUTES ;
4- import static java .time .temporal .ChronoUnit .SECONDS ;
3+ import static java .util .concurrent .TimeUnit .MILLISECONDS ;
54
5+ import io .grpc .Deadline ;
66import io .grpc .ManagedChannel ;
77import io .grpc .ManagedChannelBuilder ;
8- import java .time .Clock ;
9- import java .time .Instant ;
108import java .util .Map ;
119import java .util .concurrent .ConcurrentHashMap ;
1210import java .util .concurrent .TimeUnit ;
1614public class GrpcChannelRegistry {
1715 private static final Logger LOG = LoggerFactory .getLogger (GrpcChannelRegistry .class );
1816 private final Map <String , ManagedChannel > channelMap = new ConcurrentHashMap <>();
19- private final Clock clock ;
2017 private volatile boolean isShutdown = false ;
2118
22- @ Deprecated
23- public GrpcChannelRegistry () {
24- this (Clock .systemUTC ());
25- }
26-
27- public GrpcChannelRegistry (Clock clock ) {
28- this .clock = clock ;
29- }
30-
3119 /**
3220 * Use either {@link #forSecureAddress(String, int)} or {@link #forPlaintextAddress(String, int)}
3321 */
@@ -68,10 +56,10 @@ private String getChannelId(String host, int port, boolean isPlaintext) {
6856 /**
6957 * Shuts down channels using a default deadline of 1 minute.
7058 *
71- * @see #shutdown(Instant )
59+ * @see #shutdown(Deadline )
7260 */
7361 public void shutdown () {
74- this .shutdown (this . clock . instant (). plus ( 1 , MINUTES ));
62+ this .shutdown (Deadline . after ( 1 , TimeUnit . MINUTES ));
7563 }
7664
7765 /**
@@ -96,11 +84,11 @@ public void shutdown() {
9684 *
9785 * @param deadline Deadline for all channels to complete graceful shutdown.
9886 */
99- public void shutdown (Instant deadline ) {
87+ public void shutdown (Deadline deadline ) {
10088 channelMap .forEach (this ::initiateChannelShutdown );
10189 channelMap .keySet ().stream ()
10290 .filter (channelId -> !this .waitForGracefulShutdown (channelId , deadline ))
103- .forEach (this :: forceShutdown );
91+ .forEach (channelId -> this . forceShutdown ( channelId , deadline . offset ( 5 , TimeUnit . SECONDS )) );
10492
10593 this .isShutdown = true ;
10694 this .channelMap .clear ();
@@ -111,30 +99,28 @@ private void initiateChannelShutdown(String channelId, ManagedChannel managedCha
11199 managedChannel .shutdown ();
112100 }
113101
114- private boolean waitForGracefulShutdown (String channelId , Instant deadline ) {
102+ private boolean waitForGracefulShutdown (String channelId , Deadline deadline ) {
115103 boolean successfullyShutdown = this .waitForTermination (channelId , deadline );
116104 if (successfullyShutdown ) {
117105 LOG .info ("Shutdown channel successfully [{}]" , channelId );
118106 }
119107 return successfullyShutdown ;
120108 }
121109
122- private void forceShutdown (String channelId ) {
110+ private void forceShutdown (String channelId , Deadline deadline ) {
123111 LOG .error ("Shutting down channel [{}] forcefully" , channelId );
124112 this .channelMap .get (channelId ).shutdownNow ();
125- Instant forceShutdownDeadline = this .clock .instant ().plus (5 , SECONDS );
126- if (this .waitForTermination (channelId , forceShutdownDeadline )) {
113+ if (this .waitForTermination (channelId , deadline )) {
127114 LOG .error ("Forced channel [{}] shutdown successful" , channelId );
128115 } else {
129116 LOG .error ("Unable to force channel [{}] shutdown in 5s - giving up!" , channelId );
130117 }
131118 }
132119
133- private boolean waitForTermination (String channelId , Instant deadline ) {
120+ private boolean waitForTermination (String channelId , Deadline deadline ) {
134121 ManagedChannel managedChannel = this .channelMap .get (channelId );
135- long millisRemaining = Math .max (0 , deadline .toEpochMilli () - this .clock .millis ());
136122 try {
137- if (!managedChannel .awaitTermination (millisRemaining , TimeUnit . MILLISECONDS )) {
123+ if (!managedChannel .awaitTermination (deadline . timeRemaining ( MILLISECONDS ), MILLISECONDS )) {
138124 LOG .error ("Channel [{}] did not shut down after waiting" , channelId );
139125 }
140126 } catch (InterruptedException ex ) {
0 commit comments