From d20c4be2288a30cc18460a6b11c04c665d23c60e Mon Sep 17 00:00:00 2001 From: jiangtaoli2016 Date: Mon, 16 Nov 2020 21:00:33 -0800 Subject: [PATCH 1/2] alts: create handshaker RPC lazily --- .../alts/internal/AltsHandshakerStub.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java b/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java index 7a617845992..1a48cd5ee7d 100644 --- a/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java +++ b/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java @@ -29,7 +29,8 @@ /** An interface to the ALTS handshaker service. */ class AltsHandshakerStub { private final StreamObserver reader = new Reader(); - private final StreamObserver writer; + private StreamObserver writer; + private final HandshakerServiceStub serviceStub; private final ArrayBlockingQueue> responseQueue = new ArrayBlockingQueue<>(1); private final AtomicReference exceptionMessage = new AtomicReference<>(); @@ -37,20 +38,20 @@ class AltsHandshakerStub { private static final long HANDSHAKE_RPC_DEADLINE_SECS = 20; AltsHandshakerStub(HandshakerServiceStub serviceStub) { - this.writer = - serviceStub - .withDeadlineAfter(HANDSHAKE_RPC_DEADLINE_SECS, SECONDS) - .doHandshake(this.reader); + writer = null; + this.serviceStub = serviceStub; } @VisibleForTesting AltsHandshakerStub() { writer = null; + serviceStub = null; } @VisibleForTesting AltsHandshakerStub(StreamObserver writer) { this.writer = writer; + serviceStub = null; } @VisibleForTesting @@ -60,6 +61,7 @@ StreamObserver getReaderForTest() { /** Send a handshaker request and return the handshaker response. */ public HandshakerResp send(HandshakerReq req) throws InterruptedException, IOException { + createWriterIfNull(); maybeThrowIoException(); if (!responseQueue.isEmpty()) { throw new IOException("Received an unexpected response."); @@ -72,6 +74,14 @@ public HandshakerResp send(HandshakerReq req) throws InterruptedException, IOExc return result.get(); } + /** Create a new writer if the writer is null. */ + private synchronized void createWriterIfNull() { + if (writer == null) { + writer = + serviceStub.withDeadlineAfter(HANDSHAKE_RPC_DEADLINE_SECS, SECONDS).doHandshake(reader); + } + } + /** Throw exception if there is an outstanding exception. */ private void maybeThrowIoException() throws IOException { if (exceptionMessage.get() != null) { @@ -81,7 +91,9 @@ private void maybeThrowIoException() throws IOException { /** Close the connection. */ public void close() { - writer.onCompleted(); + if (writer != null) { + writer.onCompleted(); + } } private class Reader implements StreamObserver { From f15738da65d579c36e92344a7f036d8b833efa27 Mon Sep 17 00:00:00 2001 From: jiangtaoli2016 Date: Tue, 17 Nov 2020 15:41:20 -0800 Subject: [PATCH 2/2] alts: address review comments --- .../main/java/io/grpc/alts/internal/AltsHandshakerStub.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java b/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java index 1a48cd5ee7d..61d9fd2f894 100644 --- a/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java +++ b/alts/src/main/java/io/grpc/alts/internal/AltsHandshakerStub.java @@ -38,13 +38,11 @@ class AltsHandshakerStub { private static final long HANDSHAKE_RPC_DEADLINE_SECS = 20; AltsHandshakerStub(HandshakerServiceStub serviceStub) { - writer = null; this.serviceStub = serviceStub; } @VisibleForTesting AltsHandshakerStub() { - writer = null; serviceStub = null; } @@ -75,7 +73,7 @@ public HandshakerResp send(HandshakerReq req) throws InterruptedException, IOExc } /** Create a new writer if the writer is null. */ - private synchronized void createWriterIfNull() { + private void createWriterIfNull() { if (writer == null) { writer = serviceStub.withDeadlineAfter(HANDSHAKE_RPC_DEADLINE_SECS, SECONDS).doHandshake(reader);