1 /* 2 * Copyright 2017 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.examples.manualflowcontrol; 18 19 import io.grpc.ManagedChannel; 20 import io.grpc.ManagedChannelBuilder; 21 import io.grpc.stub.ClientCallStreamObserver; 22 import io.grpc.stub.ClientResponseObserver; 23 24 import java.util.Arrays; 25 import java.util.Iterator; 26 import java.util.List; 27 import java.util.concurrent.CountDownLatch; 28 import java.util.concurrent.TimeUnit; 29 import java.util.logging.Logger; 30 31 public class ManualFlowControlClient { 32 private static final Logger logger = 33 Logger.getLogger(ManualFlowControlClient.class.getName()); 34 main(String[] args)35 public static void main(String[] args) throws InterruptedException { 36 final CountDownLatch done = new CountDownLatch(1); 37 38 // Create a channel and a stub 39 ManagedChannel channel = ManagedChannelBuilder 40 .forAddress("localhost", 50051) 41 .usePlaintext() 42 .build(); 43 StreamingGreeterGrpc.StreamingGreeterStub stub = StreamingGreeterGrpc.newStub(channel); 44 45 // When using manual flow-control and back-pressure on the client, the ClientResponseObserver handles both 46 // request and response streams. 47 ClientResponseObserver<HelloRequest, HelloReply> clientResponseObserver = 48 new ClientResponseObserver<HelloRequest, HelloReply>() { 49 50 ClientCallStreamObserver<HelloRequest> requestStream; 51 52 @Override 53 public void beforeStart(final ClientCallStreamObserver<HelloRequest> requestStream) { 54 this.requestStream = requestStream; 55 // Set up manual flow control for the response stream. It feels backwards to configure the response 56 // stream's flow control using the request stream's observer, but this is the way it is. 57 requestStream.disableAutoInboundFlowControl(); 58 59 // Set up a back-pressure-aware producer for the request stream. The onReadyHandler will be invoked 60 // when the consuming side has enough buffer space to receive more messages. 61 // 62 // Messages are serialized into a transport-specific transmit buffer. Depending on the size of this buffer, 63 // MANY messages may be buffered, however, they haven't yet been sent to the server. The server must call 64 // request() to pull a buffered message from the client. 65 // 66 // Note: the onReadyHandler's invocation is serialized on the same thread pool as the incoming 67 // StreamObserver'sonNext(), onError(), and onComplete() handlers. Blocking the onReadyHandler will prevent 68 // additional messages from being processed by the incoming StreamObserver. The onReadyHandler must return 69 // in a timely manor or else message processing throughput will suffer. 70 requestStream.setOnReadyHandler(new Runnable() { 71 // An iterator is used so we can pause and resume iteration of the request data. 72 Iterator<String> iterator = names().iterator(); 73 74 @Override 75 public void run() { 76 // Start generating values from where we left off on a non-gRPC thread. 77 while (requestStream.isReady()) { 78 if (iterator.hasNext()) { 79 // Send more messages if there are more messages to send. 80 String name = iterator.next(); 81 logger.info("--> " + name); 82 HelloRequest request = HelloRequest.newBuilder().setName(name).build(); 83 requestStream.onNext(request); 84 } else { 85 // Signal completion if there is nothing left to send. 86 requestStream.onCompleted(); 87 } 88 } 89 } 90 }); 91 } 92 93 @Override 94 public void onNext(HelloReply value) { 95 logger.info("<-- " + value.getMessage()); 96 // Signal the sender to send one message. 97 requestStream.request(1); 98 } 99 100 @Override 101 public void onError(Throwable t) { 102 t.printStackTrace(); 103 done.countDown(); 104 } 105 106 @Override 107 public void onCompleted() { 108 logger.info("All Done"); 109 done.countDown(); 110 } 111 }; 112 113 // Note: clientResponseObserver is handling both request and response stream processing. 114 stub.sayHelloStreaming(clientResponseObserver); 115 116 done.await(); 117 118 channel.shutdown(); 119 channel.awaitTermination(1, TimeUnit.SECONDS); 120 } 121 names()122 private static List<String> names() { 123 return Arrays.asList( 124 "Sophia", 125 "Jackson", 126 "Emma", 127 "Aiden", 128 "Olivia", 129 "Lucas", 130 "Ava", 131 "Liam", 132 "Mia", 133 "Noah", 134 "Isabella", 135 "Ethan", 136 "Riley", 137 "Mason", 138 "Aria", 139 "Caden", 140 "Zoe", 141 "Oliver", 142 "Charlotte", 143 "Elijah", 144 "Lily", 145 "Grayson", 146 "Layla", 147 "Jacob", 148 "Amelia", 149 "Michael", 150 "Emily", 151 "Benjamin", 152 "Madelyn", 153 "Carter", 154 "Aubrey", 155 "James", 156 "Adalyn", 157 "Jayden", 158 "Madison", 159 "Logan", 160 "Chloe", 161 "Alexander", 162 "Harper", 163 "Caleb", 164 "Abigail", 165 "Ryan", 166 "Aaliyah", 167 "Luke", 168 "Avery", 169 "Daniel", 170 "Evelyn", 171 "Jack", 172 "Kaylee", 173 "William", 174 "Ella", 175 "Owen", 176 "Ellie", 177 "Gabriel", 178 "Scarlett", 179 "Matthew", 180 "Arianna", 181 "Connor", 182 "Hailey", 183 "Jayce", 184 "Nora", 185 "Isaac", 186 "Addison", 187 "Sebastian", 188 "Brooklyn", 189 "Henry", 190 "Hannah", 191 "Muhammad", 192 "Mila", 193 "Cameron", 194 "Leah", 195 "Wyatt", 196 "Elizabeth", 197 "Dylan", 198 "Sarah", 199 "Nathan", 200 "Eliana", 201 "Nicholas", 202 "Mackenzie", 203 "Julian", 204 "Peyton", 205 "Eli", 206 "Maria", 207 "Levi", 208 "Grace", 209 "Isaiah", 210 "Adeline", 211 "Landon", 212 "Elena", 213 "David", 214 "Anna", 215 "Christian", 216 "Victoria", 217 "Andrew", 218 "Camilla", 219 "Brayden", 220 "Lillian", 221 "John", 222 "Natalie", 223 "Lincoln" 224 ); 225 } 226 } 227