1 of 30

Newman

A Functional REST Client for Scala

Aaron Schlesinger

Sr. Software Engineer, StackMob

2 of 30

3 of 30

Overview

  • How Newman Started
  • Why We Built It
  • Why It’s Good

4 of 30

Motivation

  • Internal HTTP Services
  • External HTTP Services
  • Many HTTP Clients

5 of 30

Apache HttpClient

6 of 30

Code

  • Lots of code, lots of “knobs”
  • Setup
  • Execute a Request

7 of 30

Finagle

  • Netty event loop(s)
  • Twitter’s core library
  • http://monkey.org/~marius/funsrv.pdf

8 of 30

Code

  • Sensible Defaults
  • Builder Pattern
  • Execute a Request

9 of 30

More

10 of 30

Newman

  • Common Interface
  • Safe in Common Case
  • As Fast as Possible

11 of 30

Part I: Common Interface

  • One Library to Learn
  • Standardize Practices
  • Examples: HttpClient, HttpRequest

12 of 30

Part II: Safety

  • Functional Concepts
  • Type Safety
  • Immutability

13 of 30

Functional Newman

14 of 30

Referential Transparency

  • You can replace a function call with the value it returns
  • No thrown exceptions, no I/O, etc...
  • HttpClient in Newman

15 of 30

Type Classes

  • Different way to do polymorphism
  • No Base Classes (Ad-Hoc)
  • https://gist.github.com/arschles/6747772

16 of 30

JSON decoding in Newman

17 of 30

Type Safe Newman

  • Rich Type System
  • Use Wisely
  • Already saw Result[T]

18 of 30

Examples of Type Safety

19 of 30

Immutable Newman

  • Copy Instead of Change
  • Easy to Read & Debug Code
  • No Internal State Changes

20 of 30

Example of Immutability

21 of 30

Part III: Performance

  • Common Request Pattern
  • Clients are “Heavy”
  • One Client, Many Requests
  • Cache Everything

22 of 30

More on Clients

  • I/O gatekeepers
  • Non Blocking
  • Sometimes Leaky Abstractions

23 of 30

Client Pros and Cons

  • Apache - Thread Per Request
  • Finagle - Netty Event Loop
  • Spray - Akka Actor Per Request

24 of 30

Caching

  • Plug and Play
  • Underlying HttpClient
  • ETag support

25 of 30

Wrap Up: Upcoming Work

  • Always fixing bugs
  • Caching Improvements
  • Configurability
  • Instrumentation
  • https://github.com/stackmob/newman/issues

26 of 30

Thanks & Q/A

27 of 30

Extra: Performance Overhead

  • Immutability = copying stuff
  • Copies are small and “young”
  • GC optimizes for that

28 of 30

Extra: Best Tool For The Job

  • Threads v. Selectors
  • Apache
  • Netty
  • Spray

29 of 30

Extra: Extending

  • Subclass HttpClient
  • Add a new HTTP Verb
  • Cache Implementation
  • Response Body Serialization

30 of 30

Extra: Using Futures

  • Type Safe
  • ExecutionStrategy
  • Common Patterns