https://www.maxpixel.net/Post-Impressionist-Post-Impressionism-Fine-Art-Dutch-1428139
private Connection.Available getConnection(long eta,
long wait,
String token)
{
for(;;) {
if (eta > MAX_ETA_MS) {
throw new EtaExceededException();
}
if (wait > 0) {
Thread.sleep(wait);
}
println("Retrying download after " + wait + "ms wait.");
Connection c = coordinator.requestConnection(token);
if (c instanceof Connection.Available) {
return (Connection.Available) c;
}
Connection.Unavailable unavail = (Connection.Unavailable) c;
eta = unavail.getEta();
wait = unavail.getWait();
token = unavail.getToken();
}
}
public static InputStream blockingRequest(String url, String headers)
throws IOException
{
URL uri = new URL(url);
SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), uri.getP
SocketChannel channel = SocketChannel.open(serverAddress);
ByteBuffer buffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost() + "
do {
channel.write(buffer);
} while(buffer.hasRemaining());
return channel.socket().getInputStream();
}
https://blog.paralleluniverse.co/2014/02/04/littles-law/
private void getThingy(int i, CompletionHandler<Void> handler)
{
println("Start getThingy.");
getConnection(new CompletionHandler<>() {
@Override
public void completed(Connection.Available conn) {
println("Got token, " + conn.getToken());
CompletableFuture<Void> downloadFut = new CompletableFuture<>();
gateway.downloadThingy(new RequestHandler() {
private int total = 0;
private boolean pulsing = false;
private boolean cancelled = false;
@Override
public void received(byte[] data) {
int read = data.length;
println(i + " :: Thingy received " + read);
if (!pulsing) {
Runnable pulse = new PulseRunnable(i, downloadFut, con
boundedPulseExecutor.schedule(pulse, 2_000L, TimeUnit.
pulsing = true;
}
// ... to be continued
public static void asyncNonBlockingRequest(ExecutorService executor,
String url, String headers,
RequestHandler handler) {
executor.submit(() -> {
try {
println("Starting request to " + url);
URL uri = new URL(url);
SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), 80);
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group);
channel.connect(serverAddress, null, new CompletionHandler<Void>() {
@Override
public void completed(Void result, Void attachment) {
ByteBuffer headersBuffer = ByteBuffer.wrap((headers + "Host: " +
uri.ge
ByteBuffer responseBuffer = ByteBuffer.allocate(1024);
channel.write(headersBuffer, headersBuffer, new
CompletionHandler<>() {
@Override
public void completed(Integer written, ByteBuffer attachment) {
if (attachment.hasRemaining()) {
channel.write(attachment, attachment, this);
} else {
channel.read(responseBuffer, responseBuffer, new
Completion
@Override
public void completed(Integer read,
ByteBuffer attachment) {
// More
// this
// way
// ==>
private Mono<Connection.Available> getConnection(long eta, long wait, String token)
{
AtomicLong etaRef = new AtomicLong(eta);
AtomicLong waitRef = new AtomicLong(wait);
AtomicReference<String> tokenRef = new AtomicReference<>(token);
return Mono.defer(() -> {
if (etaRef.get() > MAX_ETA_MS) {
return Mono.error(new EtaExceededException());
}
return Mono.delay(Duration.ofMillis(waitRef.get()))
.flatMap(i -> coordinator.requestConnection(tokenRef.get()));
}).flatMap(c -> {
if (c instanceof Connection.Available) {
return Mono.just((Connection.Available) c);
} else {
Connection.Unavailable unavail = (Connection.Unavailable) c;
etaRef.set(unavail.getEta());
waitRef.set(unavail.getWait());
tokenRef.set(unavail.getToken());
return Mono.empty();
}
}).repeatWhenEmpty(Repeat
.onlyIf(ctx -> true)
.doOnRepeat(ctx ->
println(waitRef.get() + ", " + etaRef.get() + ", " + tokenRef.get())));
}
private Mono<Connection.Available> getConnection(long eta, long wait, String token) {
AtomicLong etaRef = new AtomicLong(eta);
AtomicLong waitRef = new AtomicLong(wait);
AtomicReference<String> tokenRef = new AtomicReference<>(token);
return Mono.defer(() -> {
if (etaRef.get() > MAX_ETA_MS) {
return Mono.error(new EtaExceededException());
}
return Mono.delay(Duration.ofMillis(waitRef.get()))
.flatMap(i -> coordinator.requestConnection(tokenRef.get()));
}).flatMap(c -> {
if (c instanceof Connection.Available) {
return Mono.just((Connection.Available) c);
} else {
Connection.Unavailable unavail = (Connection.Unavailable) c;
etaRef.set(unavail.getEta());
waitRef.set(unavail.getWait());
tokenRef.set(unavail.getToken());
return Mono.empty();
}
}).repeatWhenEmpty(Repeat
.onlyIf(ctx -> true)
.doOnRepeat(ctx ->
println(waitRef.get() + ", " + etaRef.get() + ", " + tokenRef.get())));
}
class StateMachineIterator implements Iterator<String> {
private int state;
private int i;
public String next() {
switch(state) {
case 0: state=1; return "A";
case 1: state=2; i=0; return "B";
case 2:
if(i == 3) state = 3;
return "C" + (i++);
case 3: state=4; return "D";
case 4: state=5; return "E";
default: throw new NoSuchElementException();
}
}
public boolean hasNext() {
return state < 5;
}
public void remove() {
throw new UnsupportedOperationException("Not supported");
}
}
class StateMachineIterator implements Iterator<String> {
private int state;
private int i;
public String next() {
switch(state) {
case 0: state=1; return "A";
case 1: state=2; i=0; return "B";
case 2:
if(i == 3) state = 3;
return "C" + (i++);
case 3: state=4; return "D";
case 4: state=5; return "E";
default: throw new NoSuchElementException();
}
}
public boolean hasNext() {
return state < 5;
}
public void remove() {
throw new UnsupportedOperationException("Not supported");
}
}
class StateMachineIterator implements Iterator<String> {
private int state;
private int i;
public String next() {
switch(state) {
case 0: state=1; return "A";
case 1: state=2; i=0; return "B";
case 2:
if(i == 3) state = 3;
return "C" + (i++);
case 3: state=4; return "D";
case 4: state=5; return "E";
default: throw new NoSuchElementException();
}
}
public boolean hasNext() {
return state < 5;
}
public void remove() {
throw new UnsupportedOperationException("Not supported");
}
}
class StateMachineIterator implements Iterator<String> {
private int state;
private int i;
public String next() {
switch(state) {
case 0: state=1; return "A";
case 1: state=2; i=0; return "B";
case 2:
if(i == 3) state = 3;
return "C" + (i++);
case 3: state=4; return "D";
case 4: state=5; return "E";
default: throw new NoSuchElementException();
}
}
public boolean hasNext() {
return state < 5;
}
public void remove() {
throw new UnsupportedOperationException("Not supported");
}
}
private Connection.Available getConnection(long eta,
long wait,
String token)
{
for(;;) {
if (eta > MAX_ETA_MS) {
throw new EtaExceededException();
}
if (wait > 0) {
Thread.sleep(wait);
}
println("Retrying download after " + wait + "ms wait.");
Connection c = coordinator.requestConnection(token);
if (c instanceof Connection.Available) {
return (Connection.Available) c;
}
Connection.Unavailable unavail = (Connection.Unavailable) c;
eta = unavail.getEta();
wait = unavail.getWait();
token = unavail.getToken();
}
}
Connection.Available conn = getConnection();
Runnable pulse = makePulse(conn);
Fiber<Object> f = null;
try (InputStream content = gateway.downloadThingy()) {
f = FiberScope.background().schedule(pulse);
ignoreContent(content);
} catch (IOException e) {
err("Download failed.");
throw e;
}
finally {
if (f!=null) {
f.cancel();
}
}
Papers and Webpages
Flynn, Michael J. - Some Computer Organizations and Their Effectiveness
Haynes, Christopher T. - Logic Continuations
Wand, Mitchell - Continuation-based Multiprocessing
Loitsch, Florian - Exceptional Continuations in JavaScript
Long, James - What's In A Continuation
Reynolds, John C. - The Discoveries of Continuations
Clinger, Hartheimer, Ost - Implementation Strategies for Continuations
Haynes, Friedman, Wand - Obtaining Coroutines with Continuations
Pressler, Ron - Why Continuations are Coming to Java
Repos
https://github.com/forax/loom-fiber/
https://bitbucket.org/arnaudbos/untangled/

2019-10-05 - Untangled - Voxxed days ticino

  • 1.
  • 5.
    private Connection.Available getConnection(longeta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }
  • 11.
    public static InputStreamblockingRequest(String url, String headers) throws IOException { URL uri = new URL(url); SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), uri.getP SocketChannel channel = SocketChannel.open(serverAddress); ByteBuffer buffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost() + " do { channel.write(buffer); } while(buffer.hasRemaining()); return channel.socket().getInputStream(); }
  • 18.
  • 20.
    private void getThingy(inti, CompletionHandler<Void> handler) { println("Start getThingy."); getConnection(new CompletionHandler<>() { @Override public void completed(Connection.Available conn) { println("Got token, " + conn.getToken()); CompletableFuture<Void> downloadFut = new CompletableFuture<>(); gateway.downloadThingy(new RequestHandler() { private int total = 0; private boolean pulsing = false; private boolean cancelled = false; @Override public void received(byte[] data) { int read = data.length; println(i + " :: Thingy received " + read); if (!pulsing) { Runnable pulse = new PulseRunnable(i, downloadFut, con boundedPulseExecutor.schedule(pulse, 2_000L, TimeUnit. pulsing = true; } // ... to be continued
  • 26.
    public static voidasyncNonBlockingRequest(ExecutorService executor, String url, String headers, RequestHandler handler) { executor.submit(() -> { try { println("Starting request to " + url); URL uri = new URL(url); SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), 80); AsynchronousSocketChannel channel = AsynchronousSocketChannel.open(group); channel.connect(serverAddress, null, new CompletionHandler<Void>() { @Override public void completed(Void result, Void attachment) { ByteBuffer headersBuffer = ByteBuffer.wrap((headers + "Host: " + uri.ge ByteBuffer responseBuffer = ByteBuffer.allocate(1024); channel.write(headersBuffer, headersBuffer, new CompletionHandler<>() { @Override public void completed(Integer written, ByteBuffer attachment) { if (attachment.hasRemaining()) { channel.write(attachment, attachment, this); } else { channel.read(responseBuffer, responseBuffer, new Completion @Override public void completed(Integer read, ByteBuffer attachment) { // More // this // way // ==>
  • 35.
    private Mono<Connection.Available> getConnection(longeta, long wait, String token) { AtomicLong etaRef = new AtomicLong(eta); AtomicLong waitRef = new AtomicLong(wait); AtomicReference<String> tokenRef = new AtomicReference<>(token); return Mono.defer(() -> { if (etaRef.get() > MAX_ETA_MS) { return Mono.error(new EtaExceededException()); } return Mono.delay(Duration.ofMillis(waitRef.get())) .flatMap(i -> coordinator.requestConnection(tokenRef.get())); }).flatMap(c -> { if (c instanceof Connection.Available) { return Mono.just((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; etaRef.set(unavail.getEta()); waitRef.set(unavail.getWait()); tokenRef.set(unavail.getToken()); return Mono.empty(); } }).repeatWhenEmpty(Repeat .onlyIf(ctx -> true) .doOnRepeat(ctx -> println(waitRef.get() + ", " + etaRef.get() + ", " + tokenRef.get()))); }
  • 36.
    private Mono<Connection.Available> getConnection(longeta, long wait, String token) { AtomicLong etaRef = new AtomicLong(eta); AtomicLong waitRef = new AtomicLong(wait); AtomicReference<String> tokenRef = new AtomicReference<>(token); return Mono.defer(() -> { if (etaRef.get() > MAX_ETA_MS) { return Mono.error(new EtaExceededException()); } return Mono.delay(Duration.ofMillis(waitRef.get())) .flatMap(i -> coordinator.requestConnection(tokenRef.get())); }).flatMap(c -> { if (c instanceof Connection.Available) { return Mono.just((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; etaRef.set(unavail.getEta()); waitRef.set(unavail.getWait()); tokenRef.set(unavail.getToken()); return Mono.empty(); } }).repeatWhenEmpty(Repeat .onlyIf(ctx -> true) .doOnRepeat(ctx -> println(waitRef.get() + ", " + etaRef.get() + ", " + tokenRef.get()))); }
  • 44.
    class StateMachineIterator implementsIterator<String> { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }
  • 45.
    class StateMachineIterator implementsIterator<String> { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }
  • 46.
    class StateMachineIterator implementsIterator<String> { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }
  • 47.
    class StateMachineIterator implementsIterator<String> { private int state; private int i; public String next() { switch(state) { case 0: state=1; return "A"; case 1: state=2; i=0; return "B"; case 2: if(i == 3) state = 3; return "C" + (i++); case 3: state=4; return "D"; case 4: state=5; return "E"; default: throw new NoSuchElementException(); } } public boolean hasNext() { return state < 5; } public void remove() { throw new UnsupportedOperationException("Not supported"); } }
  • 49.
    private Connection.Available getConnection(longeta, long wait, String token) { for(;;) { if (eta > MAX_ETA_MS) { throw new EtaExceededException(); } if (wait > 0) { Thread.sleep(wait); } println("Retrying download after " + wait + "ms wait."); Connection c = coordinator.requestConnection(token); if (c instanceof Connection.Available) { return (Connection.Available) c; } Connection.Unavailable unavail = (Connection.Unavailable) c; eta = unavail.getEta(); wait = unavail.getWait(); token = unavail.getToken(); } }
  • 50.
    Connection.Available conn =getConnection(); Runnable pulse = makePulse(conn); Fiber<Object> f = null; try (InputStream content = gateway.downloadThingy()) { f = FiberScope.background().schedule(pulse); ignoreContent(content); } catch (IOException e) { err("Download failed."); throw e; } finally { if (f!=null) { f.cancel(); } }
  • 60.
    Papers and Webpages Flynn,Michael J. - Some Computer Organizations and Their Effectiveness Haynes, Christopher T. - Logic Continuations Wand, Mitchell - Continuation-based Multiprocessing Loitsch, Florian - Exceptional Continuations in JavaScript Long, James - What's In A Continuation Reynolds, John C. - The Discoveries of Continuations Clinger, Hartheimer, Ost - Implementation Strategies for Continuations Haynes, Friedman, Wand - Obtaining Coroutines with Continuations Pressler, Ron - Why Continuations are Coming to Java Repos https://github.com/forax/loom-fiber/ https://bitbucket.org/arnaudbos/untangled/