Network Protocol Programming in Haskell - ? 3 Why Haskell? Haskell is a statically-typed programming

Download Network Protocol Programming in Haskell - ? 3 Why Haskell? Haskell is a statically-typed programming

Post on 25-Jul-2018




0 download


1Network Protocol Programmingin HaskellKazu YamamotoInternet Initiative Japan Inc.2Background Over 7 years, I have developed several network protocols in Haskell My mission in our company is to contribute standardization of network protocols Im one of the maintainers of the network library in Haskell3Why Haskell? Haskell is a statically-typed programming language Haskell is suitable for highly concurrent network programming Haskell provides everything I want4(1) Lightweight threads The flagship compiler of Haskell is GHC Glasgow Haskell Compiler GHC provides lightweight (green) threads The overhead of a lightweight thread is about 1K bytes They can migrate to another low-load core5HTTP/1.1 implementation Event driven programming Code needs to be divided into some handlers (callbacks) States need to be maintained explicitly Lightweight thread programming Straightforward Tactics: one lightweight thread per connection6(2a) Rich data types Haskell provides integrated data types: sums of products with recursion Thanks to tags, we can cover all possible values7(3) Strong type system Each piece of Haskell code is an expression Types of expressions can be checked in two ways: how the expression is composed from the inside how it is used from the outside A sequence of statements is a syntax sugar of expressions8With rich data types,the strong type system detects many errorsat compile timeIf Haskell code compiles,the code worksas its programmer intends in many casesDebugging phase is really short9(2b) Immutable data types Most data types are immutable, thus thread-safe Immutable data can be treated as mutable data with mutable references A single mutable reference can be changed w/o locking10Deadlock Threads may use multiple variables and need to update them in consistent manner Other languages use multiple locks for this purpose Multiple locks sometime result in dead lock Common solution is to decide the total order of variables But this approach is troublesome and sometime impossible11(4) Software Transactional Memory (STM) STM is dead-lock free STM is a mechanism to make multiple locks to a single STM actions are retried until they succeed Haskells type system ensures that side-effects in STM actions can be rolled back Retries are safe: missiles are never launched12HTTP/2 HTTP/2 is re-design of the transport layer It keeps HTTP/1.1 semantics such as HTTP headers Only one TCP connection is used Multiple requests and responses are transported The order of responses is not guaranteed13HTTP/2 implementation The tactics cannot be used to implement HTTP/2 I needed to introduce several threads and some variables This system is dead-lock free thanks to STM14Benchmark Downloading short files h2load in nghttp2 Supporting both HTTP/1.1 and HTTP/2 Scaling on multi-cores nginx and my server in Haskell15Performance16Further reading