Created
June 29, 2016 19:03
-
-
Save carl-mastrangelo/725ca884fa1f7a99a4b86f50bf89205c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.grpc.examples; | |
import io.grpc.CallOptions; | |
import io.grpc.ClientCall; | |
import io.grpc.ManagedChannel; | |
import io.grpc.ManagedChannelBuilder; | |
import io.grpc.Metadata; | |
import io.grpc.MethodDescriptor; | |
import io.grpc.MethodDescriptor.Marshaller; | |
import io.grpc.MethodDescriptor.MethodType; | |
import io.grpc.Server; | |
import io.grpc.ServerBuilder; | |
import io.grpc.Status; | |
import java.io.ByteArrayInputStream; | |
import java.io.InputStream; | |
import java.util.concurrent.Executor; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Semaphore; | |
import java.util.concurrent.atomic.AtomicLong; | |
import java.util.logging.Logger; | |
public class LoopClient { | |
private static final Logger log = Logger.getLogger(LoopClient.class.getName()); | |
private static final AtomicLong count = new AtomicLong(); | |
private static final Semaphore sema = new Semaphore(10000); | |
private static Marshaller<Void> marshaller = new Marshaller<Void>() { | |
@Override | |
public InputStream stream(Void value) { | |
return new ByteArrayInputStream(new byte[]{}); | |
} | |
@Override | |
public Void parse(InputStream stream) { | |
return null; | |
} | |
}; | |
private static final MethodDescriptor<Void, Void> desc = | |
MethodDescriptor.create(MethodType.BIDI_STREAMING, "service/method", marshaller, marshaller); | |
public static void main(String [] args) throws Exception { | |
new Thread() { | |
@Override | |
public void run() { | |
super.run(); | |
long c = count.get(); | |
while (true) { | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
log.info(String.format("%s/s %s", (count.get() - c) / 1000.0, sema.availablePermits())); | |
c = count.get(); | |
} | |
} | |
}.start(); | |
Executor e = Executors.newFixedThreadPool(20); | |
final Server s = ServerBuilder.forPort(65534).executor(e).build().start(); | |
ManagedChannel chan = ManagedChannelBuilder.forAddress("localhost", 65534) | |
.usePlaintext(true) | |
.executor(e) | |
.build(); | |
while (true) { | |
ClientCall<Void, Void> call = chan.newCall(desc, CallOptions.DEFAULT); | |
sema.acquire(); | |
call.start(new ClientCall.Listener<Void>() { | |
@Override | |
public void onClose(Status status, Metadata trailers) { | |
count.incrementAndGet(); | |
sema.release(); | |
if (!status.isOk()) { | |
//log.severe(status.toString()); | |
} | |
} | |
}, new Metadata()); | |
//call.sendMessage(null); | |
call.halfClose(); | |
call.request(1); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment