Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

I need connection pools!

Slide 3

Slide 3 text

http_client

Slide 4

Slide 4 text

http_client server w/ Keep-Alive

Slide 5

Slide 5 text

http_client server w/ Keep-Alive T1

Slide 6

Slide 6 text

http_client server w/ Keep-Alive T1 T2

Slide 7

Slide 7 text

http_client server w/ Keep-Alive T1 T2 T3

Slide 8

Slide 8 text

http_client server w/ Keep-Alive T1 T2 T3 T4

Slide 9

Slide 9 text

http_client server w/ Keep-Alive T1 T2 T3 T4 T5

Slide 10

Slide 10 text

http_client server w/ Keep-Alive T1 T2 T3 T4 T5

Slide 11

Slide 11 text

http_client server w/ Keep-Alive T1 T2 T3 T4 T5

Slide 12

Slide 12 text

resource control ! :max_connections

Slide 13

Slide 13 text

circuit breaker ! timeout when requesting too many resources

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

jruby http client

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

aesterline/jruby-httpclient

Slide 19

Slide 19 text

aesterline/jruby-httpclient $LOAD_PATH.unshift

Slide 20

Slide 20 text

aesterline/jruby-httpclient but the sadest thing is this one issue

Slide 21

Slide 21 text

aesterline/jruby-httpclient

Slide 22

Slide 22 text

aesterline/jruby-httpclient rb2k

Slide 23

Slide 23 text

aesterline/jruby-httpclient no connection pool

Slide 24

Slide 24 text

aesterline/jruby-httpclient :(

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

nahi/httpclient

Slide 27

Slide 27 text

nahi/httpclient all ruby

Slide 28

Slide 28 text

nahi/httpclient correct timeouts

Slide 29

Slide 29 text

nahi/httpclient threadsafe!

Slide 30

Slide 30 text

nahi/httpclient from Japan!

Slide 31

Slide 31 text

nahi/httpclient awesome rock solid http library

Slide 32

Slide 32 text

nahi/httpclient no connection pools

Slide 33

Slide 33 text

nahi/httpclient :(

Slide 34

Slide 34 text

Wrapping Apache HTTPClient from Javaland

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Java libraries provide everything but the kitchen sink

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

stay focused on what you want to accomplish

Slide 39

Slide 39 text

require "http_client" ! client = HTTPClient.new response = client.get("http://www.google.com/robots.txt") ! response.status # => 200 ! response.body # => # User-agent: * # ...

Slide 40

Slide 40 text

require all necessary jars but import only a subset of all the classes

Slide 41

Slide 41 text

import org.apache.http.impl.client.HttpClients import org.apache.http.impl.conn.BasicHttpClientConnectionManager import org.apache.http.impl.conn.PoolingHttpClientConnectionManager import org.apache.http.client.methods.HttpGet import org.apache.http.client.methods.HttpPost import org.apache.http.client.methods.HttpPut import org.apache.http.client.methods.HttpPatch import org.apache.http.client.methods.HttpDelete import org.apache.http.client.methods.HttpHead import org.apache.http.client.methods.HttpOptions import org.apache.http.client.config.RequestConfig import org.apache.http.entity.StringEntity import org.apache.http.client.entity.UrlEncodedFormEntity import org.apache.http.client.entity.GzipDecompressingEntity import org.apache.http.client.entity.DeflateDecompressingEntity import org.apache.http.message.BasicNameValuePair import org.apache.http.entity.ContentType import org.apache.http.util.EntityUtils import org.apache.http.HttpException import org.apache.http.conn.ConnectTimeoutException import java.io.IOException import java.net.SocketTimeoutException import java.util.concurrent.TimeUnit

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

wrap exceptions from Javaland

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

learn about the thread safety of the classes you use

Slide 47

Slide 47 text

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html

Slide 48

Slide 48 text

cloc 250 lines

Slide 49

Slide 49 text

managed connection pools, thanks to

Slide 50

Slide 50 text

managed connection pools, thanks to PoolingHttpClientConnectionManager

Slide 51

Slide 51 text

JRuby

Slide 52

Slide 52 text

J

Slide 53

Slide 53 text

def create_connection_manager(options) options[:use_connection_pool] ? create_pooling_connection_manager(options) : BasicHttpClientConnectionManager.new end ! def create_pooling_connection_manager(options) connection_manager = PoolingHttpClientConnectionManager.new connection_manager.max_total = options[:max_connections] max_per_route = options[:max_connections_per_route] || \ options[:max_connections] connection_manager.default_max_per_route = max_per_route end

Slide 54

Slide 54 text

def execute(request) retries = max_retries begin closeable_response = client.execute(request) response_class.new(closeable_response) rescue ConnectTimeoutException, SocketTimeoutException => e retry if (retries -= 1) > 0 raise Timeout, "#{e.message}" rescue IOException => e retry if (retries -= 1) > 0 raise IOError, "#{e.message}" rescue HttpException => e raise Error, "#{e.message}" ensure closeable_response.close if closeable_response end end

Slide 55

Slide 55 text

def read_body(closeable_response) return "" unless entity = closeable_response.entity return "" unless entity.content_length > 0 if content_encoding = entity.content_encoding entity = case content_encoding.value when "gzip", "x-gzip" then GzipDecompressingEntity.new(entity) when "deflate" then DeflateDecompressingEntity.new(entity) else entity end end EntityUtils.to_string(entity, "UTF-8") end end

Slide 56

Slide 56 text

Lukas Rieder ! @Overbryd https://github.com/Overbryd/http_client