everything you wanted to know about writing async ... you...feature/library ning client http async...

101
Everything You Wanted to Know About Writing Async, Concurrent HTTP Apps in Java

Upload: others

Post on 30-Dec-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Everything You Wanted to

Know

About Writing Async,

Concurrent HTTP Apps in Java

Page 2: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Agenda

• Mostly this:

Page 3: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Agenda

• And this:

Page 4: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Agenda

• And this:

Page 5: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

About your speaker

linkd.in/jbaruch

stackoverflow.com/users/402053/jbaruch

Page 6: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

What Frog?

Page 7: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

What Frog?

Page 8: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

What Frog?

Page 9: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

What Frog?

Page 10: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 11: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 12: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Requirements

– parallel file Downloads

– Parallel file parts

– interrupt/pause/resume

– Progress events

– Checksums caching

Page 13: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

First Association for “concurrent

downloader”

Page 14: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 15: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Lucky day: Download manager

written in Java!

Page 16: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 17: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Let’s look if we can use it!

1. No traceable license

2. No website or docs

3. No traceable sources

4. It’s an app, not a lib

Page 18: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 19: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 20: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Java.net.urlconnection

1. Memory wasteful (buffering)

2. Minimal API

3. Blocking streams

Page 21: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 22: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

What we’re looking for

1. Async/non-blocking

2. Event callbacks

Page 23: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

What is IT going to take

1. Reactor

2. nio

Page 24: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Welcome to the reactor

Page 25: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

– pattern for lightweight concurrency

– Event driven

– Threads reuse

– Uses non-blocking Io

Page 26: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Original pattern

http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf

Page 27: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Guess the author by the

diagram

http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf

Page 28: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

In Java, Reactor means NIO

Page 29: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Selector as a multiplexer

Page 30: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Java version - Registering

SocketChannel channel= SocketChannel.open();

socketChannel.connect(new

InetSocketAddress("http://remote.com", 80));

...

Selector selector = Selector.open();

channel.configureBlocking(false);

SelectionKey k = channel.register(selector,

SelectionKey.OP_READ);

k.attach(handler);

Page 31: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Java version - Dispatcher

while (!Thread.interrupted()) {

selector.select();

Set selected = selector.selectedKeys();

Iterator it = selected.iterator();

while (it.hasNext())

SelectionKey k = (SelectionKey)(it.next();

((Runnable)(k.attachment())).run();

selected.clear();

}

Page 32: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Handling reactor events is complex

– Need to maintain state

– Buffering – assembling chunks

– Coordinating async events

Page 33: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 34: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Nio libraries

– Most of them are servers

– Netty, grizzly, etc.

– Apache Mina

– Apache HTTP components asyncclient

– Ning http client

Page 35: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Nio libraries

– Most of them are servers

– Netty, grizzly, etc.

– Apache Mina

– Apache HTTP components asyncclient

– Ning http client

Page 36: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

– Client and server nio library

– Evolved from netty

– Latest release October 2012

Page 37: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 38: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Nio libraries

– Most of them are servers

– Netty, grizzly, etc

– Apache Mina

– Apache HTTP components asyncclient

– Ning http client

Page 39: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 40: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Ning’s async http client

Page 41: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 42: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 43: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Here it is!

Page 44: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

try (AsyncHttpClient asyncHttpClient = new AsyncHttpClient()) {

ListenableFuture<Response> future = asyncHttpClient.prepareGet(

"http://oss.jfrog.org/api/system/ping").execute(

new AsyncCompletionHandler<Response>() {

@Override

public Response onCompleted(Response response) {

System.out.println(response.getResponseBody());

return response;

}

@Override

public void onThrowable(Throwable t) {

t.printStackTrace();

}

});

Response response = future.get();

}

Page 45: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 46: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

HAC Concepts

– Request producer

– Response consumer

Page 47: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

try(CloseableHttpAsyncClientasyncHttpClient=HttpAsyncClients.createDefault()){asyncHttpClient.start();Future<HttpResponse>future=asyncHttpClient.execute(HttpAsyncMethods.createGet("http://oss.jfrog.org/api/system/ping"),newAsyncByteConsumer<HttpResponse>(){@OverrideprotectedvoidonResponseReceived(finalHttpResponseresponse){System.out.println(response.getStatusLine().getReasonPhrase());}@OverrideprotectedvoidonByteReceived(finalCharBufferbuf,finalIOControlioctrl){}@OverrideprotectedvoidreleaseResources(){}@OverrideprotectedHttpResponsebuildResult(finalHttpContextcontext){return(HttpResponse)context.getAttribute("http.response");}},null);HttpResponseresponse=future.get();}

Page 48: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 49: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Choosing between ning and http

asyncclient

Page 50: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

"All problems in computer science can

be solved by another level of

indirection" David

Wheeler

Page 51: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

public interface HttpProviderDownloadHandler { void onResponseReceived(int statusCode, Map<String, List<String>> headers); boolean onBytesReceived(ByteBuffer buf); void onFailed(Throwable error); void onCanceled(); void onCompleted(); }

Page 52: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Head to head

Feature/Library Ning client Http Async Client

Maturity Good Very new (early 2014)

Download cancelation Easy With bugs

Progress hooks Events not granular enough

Just use onByteReceived()

Documentation A bit sparse Minimal

Server-side counterpart None, client only org.apache.httpcomponents httpcore-nio

Page 53: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Performance?

0

100

200

300

400

500

600

700

800

900

Small file Medium file Large file

Ning

AHAC

http://blogs.atlassian.com/2013/07/http-client-performance-io/

Page 54: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Rfc2616: a universe of its own

Page 55: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 56: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Confused?

Page 57: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Just read some stackoverflow

(and improve your rep as you go)

Page 58: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

And that one for discovering that range header is lost on redirect

Page 59: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Question!

What should be

content-length

when using

compression?

Page 60: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 61: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 62: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

https://github.com/http2/http2-spec/issues/46

Page 63: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Question!

Why when redirected to CDN all the chunks start from zero?

Page 64: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

HttpAsyncClientBuilder builder = HttpAsyncClients.custom(); // add redirect strategy that copies "range" headers, if exist builder.setRedirectStrategy(new DefaultRedirectStrategy() { @Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) HttpUriRequest redirectRequest = super.getRedirect(request, response, context); // copy "Range" headers, if exist Header[] rangeHeaders = request.getHeaders(HttpHeaders.RANGE);

if (rangeHeaders != null) { for (Header header : rangeHeaders) { redirectRequest.addHeader(header); } } return redirectRequest; }});

Page 65: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Question!

How many simultaneous

connections should I open?

Page 66: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 67: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 68: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 69: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 70: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Question!

What’s wrong

with the

following

code?

Page 71: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

public static String encodeUrl(String urlStr) { URLEncoder.encode(urlStr, "UTF-8"); ... }

Page 72: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Decoded URLs cannot be

re-encoded to the same form

http://example.com/?query=a&b==c Cannot be decoded back after it was encoded: http://example.com/?query=a%26b==c

Page 73: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Don’t use java.net.URLEncoder

“Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format.

For more information about HTML form encoding, consult the HTML specification.”

Page 74: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

AHC Alternatives

Page 75: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 76: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Question!

How do I

close a

socket

correctly?

Page 77: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

How hard can it be to close a

socket?

Page 78: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

The art of socket closing

http://www.safaribooksonline.com/library/view/http-the-definitive/1565925092/ch04s07.html

Page 79: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Half-closed: no new customers

Page 80: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Never block in socket close()

• The other side expects you

to clean up nicely

• It will give up on time out

• You will wait (forever)

Page 81: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 82: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Remember?

Page 83: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Question!

How can I write

file parts

concurrently?

Page 84: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

– Write to separate files, combine on finish

– Write to same file, seeking to the right position

Page 85: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 86: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress
Page 87: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Use FileChannel

• Implements SeekableByteChannel

java.nio.channels.FileChannel#write( java.nio.ByteBuffer src, long position)

Page 88: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Download progress tracking

• PersistentFileProgressInfo – Save the total size, sha1, number of parts

– State of each part (offset, size, completed...)

FileProgressInfo FilePartProgressInfo *

Page 89: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

File Locking

Page 90: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

File locking Levels

– VM level

– OS level

Page 91: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

OS level File locking

• Multiple downloader instances writing to

the same file

• Needed for writing:

– Partial download file

– Persistent download progress

Page 92: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

FileLock lock = fileChannel.tryLock(); //Non-shared: (0L, Long.MAX_VALUE, false) if (lock == null) { throw new OverlappingFileLockException(); } return lock; }

OS Level File Locking -

Exclusive

Page 93: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

private FileLock lock(FileChannel fileChannel) throws IOException { FileLock lock = fileChannel.tryLock(Long.MAX_VALUE - 1, 1, false); if (lock == null) { throw new OverlappingFileLockException(); } return lock; }

OS Level File Locking –

Advisory exclusive

WTF?!

Page 94: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

VM Level File Locking

Page 95: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

VM Level File Locking

– Prevent same VM threads writing to the file when we started closing it

– Closing sequence: – Release file locks

– Close channels

– Rename a file to it's final name (remove .part)

– Erase progress info

Page 96: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

VM Level File Locking ReentrantReadWriteLock.ReadLock writeToFileLock = rwl.readLock(); ReentrantReadWriteLock.WriteLock closeFileLock = rwl.writeLock(); public void close() throws IOException { this.closeFileLock.lock(); } public int write(int partIndex, ByteBuffer buf) { if (!this.writeToFileLock.tryLock()) { throw new IllegalStateException("File is being closed"); } ... }

Page 97: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

What’s next?

Page 98: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

http/2

– Mostly standardizing Google's spdy – Header compression

– multiplexing

– Prioritization

– Server push

– On the way clear some stuff – E.g. compressed content length

Page 99: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

Ease the load

Page 101: Everything You Wanted to Know About Writing Async ... You...Feature/Library Ning client Http Async Client Maturity Good Very new (early 2014) Download cancelation Easy With bugs Progress

No, Thank you!