Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vapor 3

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Vapor 3

Avatar for Joannis Orlandos

Joannis Orlandos

March 01, 2018

Other Decks in Programming

Transcript

  1. — Insanely fast & efficient — Clean, simple and maintainable

    — Reliable at small and large scale — Flexibility — A big and friendly community
  2. Example: File uploads router.post("files", String.parameter) { request -> Future<HTTPResponse> in

    // Unsafe! Don't let users decide file paths let filename = try request.parameter(String.self) let file = try File(atPath: "/path/to/" + filename, flags: [.mustCreate]) let written = try request.http.body.output(to: file) return written.transform(to: HTTPResponse(status: .ok)) } — Would normally require fileSize of RAM — Now requires as little as 2KB
  3. Example: File uploading with Base64 router.post("files", String.parameter) { request ->

    Future<HTTPResponse> in // Unsafe! Don't let users decide file paths let filename = try request.parameter(String.self) let file = try File(atPath: "/path/to/" + filename, flags: [.mustCreate]) let base64encoder = Base64Encoder().stream() let written = try request.http.body.stream(to: base64encoder).output(to: file) return written.transform(to: HTTPResponse(status: .ok)) } — Would normally need fileSize * 2.25 RAM — Now only needs as little as 3KB of RAM
  4. Example: HTTP Proxy behaviour router.get("json") { req -> Future<Response> in

    let client = try req.make(Client.self) // Return the response from this URL return client.get("http://example.vapor.codes/json") }
  5. Reactive — Event driven programming — Not blocked by external

    sources — Low memory usage — Consistent performance
  6. Comparison to humans 1. Frontend and backend dev are working

    2. Frontend dev needs an API and asks the backend dev 3. Backend dev blocks frontend dev 4. Frontend dev is now in "idle" state 5. Backend dev finished API 6. Frontend dev can now continue work
  7. Ideal scenario 1. Frontend and backend dev are working 2.

    Frontend dev needs an API and asks the backend dev 3. Backend dev starts development 4. Frontend dev hops to the next task 5. Backend dev finished API 6. Frontend dev can continue the previous task
  8. "Multi-reactor" pa!ern — Reactors are Event Loops — One "Reactor"

    per virtual core — Independent Extra benefits — No locks, no deadlocks — No Data races — ✅ Stability
  9. Thread safety 1. Frontend and backend dev are working 2.

    Frontend dev needs an API and asks the manager 3. Manager blocks both frontend and backend devs 4. Backend dev gets the API request from the manager 5. Backend dev starts development. Frontend dev is idle 6. Backend dev finishes his task. Hands it to manager 7. Manager hands the task to the frontend dev 8. Both continue working
  10. Conclusion about locks — More overhead (idle time) — Less

    productivity — More complexity (bugs) — Not common between humans either
  11. Type Languages Compiled C, C++, Swift, Go JIT Compiled Java,

    NodeJS, PHP 7 Interpreted Traditional JS, PHP 5.6
  12. Name Req/sec Avg. Responsetime Base usage Top usage Vapor Engine

    103_000 1.03ms 1.6MB 3.2MB Vapor 3 80_000 1.82ms 1.7MB 3.4MB Gin 77_000 1.26ms 6.5MB 14.0MB ExpressJS 46_000 2.40ms 16MB / Thread 64MB / Thread Laravel 1_600 14.20ms 30MB / Request 60MB / Request — High DDoS resistance — Low memory, High throughput — Configurable memory versus CPU tradeoff
  13. Planned optimizations — Custom HTTP scanner — Better TCP buffer

    reuse — Protocol boundary scanning — Lazy protocol parsing