Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Building an F# Web Server with Freya

Building an F# Web Server with Freya

This item in japanese


Freya is an F# web framework focusing on HTTP primitives and concurency. It doesn't include interface constructs such as templating. Marcus Griep presented Freya at F# eXchange 2017, where he explained its core model. He also showed the different mechanisms available for performance and concurrency, such as Hopac and Kestrel integrations.

Freya supports different hosting configurations. It can run as a self-hosted application, started directly with dotnet run. It can also run on top of Kestrel. The .NET Framework and .NET Core are both supported.

For performance, Griep recommends to run Freya on Kestrel to benefit from the many optimizations of Kestrel. In the benchmark he shows, the classical "Hello World" takes 6 ms in ASP.NET Core, 13 ms with Freya on Hopac and 26 ms with Freya on F# Async.

Hopac, as Griep's benchmark shows, can improve performance significantly in comparison to F# Async. Hopac uses a cooperative model of threading, instead of preemptive. Cooperative scheduling leads to less context switching and thus more efficient CPU usage. However, it doesn't perform well with long running jobs, as they will run through completion and potentially starve other jobs waiting to be executed.

Freya's programming model is aimed towards providing a type safe abstraction of HTTP. The freya computation expression abstracts OWIN state. The following shows a basic example of getting a query string parameter and returning a result:

let name_ = Route.atom_ "name"
let name =
    freya {
        // Get the query string parameter "name"
        let! name = Freya.Optic.get name_

        match name with
        | Some name -> return name
        | None -> return "World" }

let sayHello =
    freya {
        let! name = name

        return Represent.text (sprintf "Hello, %s!" name) }

Freya machines are an abstraction of a decision tree. A decision represents part of an HTTP rule, such as "Is CORS header present?". The full tree contains hundreds of decisions and can be extended. It also features automatic optimization, pruning all the decisions that aren't relevant for the given configuration.

Freya machines can also be defined using computation expressions. Following the earlier example, this machine sets the conditions for responding with HelloWord.

let helloMachine =
    freyaMachine {
        methods [GET; HEAD; OPTIONS]
        handleOk sayHello }

The final piece to complete the example is binding a machine to a route:

let router =
    freyaRouter {
        resource "/hello{/name}" machine }

Written with StackEdit.

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p