Slide 1

Slide 1 text

https://www.maxpixel.net/Post-Impressionist-Post-Impressionism-Fine-Art-Dutch-1428139

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

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(); } }

Slide 9

Slide 9 text

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(); } }

Slide 10

Slide 10 text

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(); } }

Slide 11

Slide 11 text

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(); } }

Slide 12

Slide 12 text

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(); } }

Slide 13

Slide 13 text

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(); } }

Slide 14

Slide 14 text

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(); } }

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

public static InputStream blockingRequest(String url, String headers) throws IOException { URL uri = new URL(url); SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), uri.getPort() SocketChannel channel = SocketChannel.open(serverAddress); ByteBuffer buffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost() + "\r\n\ do { channel.write(buffer); } while(buffer.hasRemaining()); return channel.socket().getInputStream(); }

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

https://blog.paralleluniverse.co/2014/02/04/littles-law/

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 30

Slide 30 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 31

Slide 31 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 32

Slide 32 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 33

Slide 33 text

private void getConnection(long eta, long wait, String token, CompletionHandler handler) { if (eta > MAX_ETA_MS) { if (handler!=null) handler.failed(new EtaExceededException()); } boundedServiceExecutor.schedule(() -> { println("Retrying download after " + wait + "ms wait."); coordinator.requestConnection(token, new CompletionHandler<>() { @Override public void completed(Connection c) { if (c instanceof Connection.Available) { if (handler!=null) handler.completed((Connection.Available) c); } else { Connection.Unavailable unavail = (Connection.Unavailable) c; getConnection( unavail.getEta(), unavail.getWait(), unavail.getToken(), handler); } } @Override public void failed(Throwable t) { ...

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

public static InputStream blockingRequest(String url, String headers) throws IOException { URL uri = new URL(url); SocketAddress serverAddress = new InetSocketAddress(uri.getHost(), uri.getPort() SocketChannel channel = SocketChannel.open(serverAddress); ByteBuffer buffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost() + "\r\n\ do { channel.write(buffer); } while(buffer.hasRemaining()); return channel.socket().getInputStream(); }

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

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() { @Override public void completed(Void result, Void attachment) { ByteBuffer headersBuffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost())); 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 CompletionHandler<>() { @Override public void completed(Integer read, ByteBuffer attachment) { // More // this // way // ==>

Slide 42

Slide 42 text

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(); } }

Slide 43

Slide 43 text

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() { @Override public void completed(Void result, Void attachment) { ByteBuffer headersBuffer = ByteBuffer.wrap((headers + "Host: " + uri.getHost())); 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 CompletionHandler<>() { @Override public void completed(Integer read, ByteBuffer attachment) { // More // this // way // ==>

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

https://medium.com/@copyconstruct/the-method-to-epolls-madness-d9d2d6378642

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Flux.interval(Duration.ofSeconds(2L)) .flatMap(l -> coordinator.heartbeat(conn.getToken())) .doOnNext(c -> println("Pulse!")) .then() .doOnTerminate(() -> println("Pulse terminated"));

Slide 52

Slide 52 text

private Mono getConnection(long eta, long wait, String token) { AtomicLong etaRef = new AtomicLong(eta); AtomicLong waitRef = new AtomicLong(wait); AtomicReference 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()))); }

Slide 53

Slide 53 text

private Mono getConnection(long eta, long wait, String token) { AtomicLong etaRef = new AtomicLong(eta); AtomicLong waitRef = new AtomicLong(wait); AtomicReference 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()))); }

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

class StateMachineIterator implements Iterator { 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"); } }

Slide 63

Slide 63 text

class StateMachineIterator implements Iterator { 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"); } }

Slide 64

Slide 64 text

class StateMachineIterator implements Iterator { 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"); } }

Slide 65

Slide 65 text

class StateMachineIterator implements Iterator { 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"); } }

Slide 66

Slide 66 text

class StateMachineIterator implements Iterator { 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"); } }

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

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(); } }

Slide 71

Slide 71 text

Connection.Available conn = getConnection(); Runnable pulse = makePulse(conn); Fiber 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(); } }

Slide 72

Slide 72 text

Connection.Available conn = getConnection(); Runnable pulse = makePulse(conn); Fiber 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(); } }

Slide 73

Slide 73 text

unboundedServiceExecutor = Executors.newUnboundedVirtualThreadExecutor(); ... Connection.Available conn = getConnection(); Runnable pulse = makePulse(conn); Future f = null; try (InputStream content = gateway.downloadThingy()) { f = unboundedServiceExecutor.submit(pulse); ignoreContent(content); } catch (IOException e) { err("Download failed."); throw e; } finally { if (f!=null) { f.cancel(); } }

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

http://250bpm.com/blog:71

Slide 84

Slide 84 text

http://250bpm.com/blog:71

Slide 85

Slide 85 text

Connection.Available conn = getConnection(); Runnable pulse = makePulse(conn); Future f = null; try (InputStream content = gateway.downloadThingy()) { f = unboundedServiceExecutor.submit(pulse); ignoreContent(content); } catch (IOException e) { err("Download failed."); throw e; } finally { if (f!=null) { f.cancel(); } }

Slide 86

Slide 86 text

try (FiberScope scope = FiberScope.open(CANCEL_AT_CLOSE, PROPAGATE_CANCEL)) { var conn = getConnection(); try (InputStream content = gateway.downloadThingy()) { Runnable pulse = makePulse(conn, i); scope.schedule(pulse); ignoreContent(content); } } catch (IOException e) { err("Download failed."); throw e; }

Slide 87

Slide 87 text

try (FiberScope scope = FiberScope.open(CANCEL_AT_CLOSE, PROPAGATE_CANCEL)) { var conn = getConnection(); try (InputStream content = gateway.downloadThingy()) { Runnable pulse = makePulse(conn, i); scope.schedule(pulse); ignoreContent(content); } } catch (IOException e) { err("Download failed."); throw e; }

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

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 Pressler, Ron - https://mail.openjdk.java.net/pipermail/loom-dev/2019-November/000876.html Repos https://github.com/forax/loom-fiber/ https://github.com/arnaudbos/untangled/

Slide 91

Slide 91 text

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 Pressler, Ron - https://mail.openjdk.java.net/pipermail/loom-dev/2019-November/000876.html Repos https://github.com/forax/loom-fiber/ https://github.com/arnaudbos/untangled/

Slide 92

Slide 92 text

No content