@@ -48,6 +48,7 @@ public class SpannerOmniCredentials extends GoogleCredentials {
4848 private final byte [] password ;
4949 private final String target ;
5050 private final boolean backgroundRefresh ;
51+ private final ManagedChannel loginChannel ;
5152
5253 private ScheduledFuture <?> refreshTask ;
5354
@@ -60,13 +61,13 @@ public SpannerOmniCredentials(String username, byte[] password, String target, b
6061 this .password = password ;
6162 this .target = target ;
6263 this .backgroundRefresh = backgroundRefresh ;
64+ this .loginChannel = ManagedChannelBuilder .forTarget (target ).build ();
6365 }
6466
6567 @ Override
6668 public AccessToken refreshAccessToken () throws IOException {
67- ManagedChannel loginChannel = ManagedChannelBuilder .forTarget (target ).build ();
6869 try {
69- LoginClient loginClient = new LoginClient (loginChannel );
70+ LoginClient loginClient = new LoginClient (this . loginChannel );
7071 google .spanner .omni .v1 .AccessToken protoToken = loginClient .login (username , password );
7172 String tokenValue = Base64 .getEncoder ().encodeToString (protoToken .toByteArray ());
7273
@@ -90,16 +91,6 @@ public AccessToken refreshAccessToken() throws IOException {
9091 } catch (Exception e ) {
9192 logger .log (Level .SEVERE , "Failed to login to Spanner Omni. Username: " + username + ", Target: " + target , e );
9293 throw new IOException ("Failed to login to Spanner Omni" , e );
93- } finally {
94- loginChannel .shutdown ();
95- try {
96- if (!loginChannel .awaitTermination (1 , TimeUnit .SECONDS )) {
97- loginChannel .shutdownNow ();
98- }
99- } catch (InterruptedException ie ) {
100- Thread .currentThread ().interrupt ();
101- loginChannel .shutdownNow ();
102- }
10394 }
10495 }
10596
@@ -123,21 +114,26 @@ private void scheduleRefresh(long tokenLifetimeMillis) {
123114
124115 java .lang .ref .WeakReference <SpannerOmniCredentials > weakThis = new java .lang .ref .WeakReference <>(this );
125116
126- refreshTask = SHARED_EXECUTOR .schedule (() -> {
127- SpannerOmniCredentials creds = weakThis .get ();
128- if (creds == null ) {
129- // The credentials instance was garbage collected. Stop the background refresh loop.
130- return ;
131- }
132- try {
133- creds .refresh ();
134- } catch (IOException e ) {
135- logger .log (Level .WARNING , "Failed to auto-refresh Spanner Omni credentials" , e );
136- // Retry in a short interval on failure
137- long retryDelay = Math .min (TimeUnit .SECONDS .toMillis (5 ), tokenLifetimeMillis / 4 );
138- if (retryDelay <= 0 ) retryDelay = 1000 ;
139- creds .scheduleRefresh (retryDelay * 2 ); // Pass double the retry delay as fake lifetime to get retryDelay scheduling
117+ Runnable refreshAction = new Runnable () {
118+ @ Override
119+ public void run () {
120+ SpannerOmniCredentials creds = weakThis .get ();
121+ if (creds == null ) {
122+ // The credentials instance was garbage collected. Stop the background refresh loop.
123+ return ;
124+ }
125+ try {
126+ creds .refresh ();
127+ } catch (IOException e ) {
128+ logger .log (Level .WARNING , "Failed to auto-refresh Spanner Omni credentials" , e );
129+ // Retry in a short interval on failure
130+ long retryDelay = Math .min (TimeUnit .SECONDS .toMillis (5 ), tokenLifetimeMillis / 4 );
131+ if (retryDelay <= 0 ) retryDelay = 1000 ;
132+ creds .refreshTask = SHARED_EXECUTOR .schedule (this , retryDelay , TimeUnit .MILLISECONDS );
133+ }
140134 }
141- }, delayMillis , TimeUnit .MILLISECONDS );
135+ };
136+
137+ refreshTask = SHARED_EXECUTOR .schedule (refreshAction , delayMillis , TimeUnit .MILLISECONDS );
142138 }
143139}
0 commit comments