Skip to content

Instantly share code, notes, and snippets.

@arschles
Last active December 24, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arschles/6802348 to your computer and use it in GitHub Desktop.
Save arschles/6802348 to your computer and use it in GitHub Desktop.
//Newman has a DSL for building up HttpRequests.
//the DSL uses the familiar builder pattern from Java.
//it copies the state of the request on each call, instead of modifying any internal state.
val url = new URL("http://siliconvalley-codecamp.com")
//the familiar GET function returns an instance of a Builder
//you can convert any Builder to a Newman HttpRequest
val builder1 = GET(url)
//we can add headers to any Builder.
//addHeaders copies the existing Builder to a new one with the headers
val builder2 = builder1.addHeaders("X-SVCC-Hello", "World")
//we can't add a body to a GET, so this won't compile.
//remember type safety?
val builder3 = builder1.addBody("Hello SVCC!")
//but we can add a body to a POST request, so this will compile
val builder4 = POST(url).addBody("Hello SVCC!")
//we can call toRequest on any Builder object to copy all its information into an HttpRequest
val req = builder4.toRequest
...
@arschles
Copy link
Author

arschles commented Oct 3, 2013

a few notes on performance:

  • we debated the performance of this immutable system a few times over, because it does so many copies
  • each object the DSL creates and subsequently copies is small and usually is short lived in most of our internal code
  • because they're small, they're fast to copy
  • because they're small and recycle fast, they go into the GC eden space and we usually don't pay a memory allocation penalty
  • regardless this model is less performant than a mutable state model, but the tradeoff is highly reusable and threadsafe code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment