15
F# on the Server-Side Tomáš Petříček http :// tomasp.net/blog | @tomaspetricek

F# on the Server-Side

Embed Size (px)

DESCRIPTION

Talk about server-side programming in F# from Functional Programming eXchange 2011 at SkillsMatter. The talk progresses from callback-based programming to asynchronous workflows and to agents in F#.

Citation preview

Page 1: F# on the Server-Side

F# on the Server-Side

Tomáš Petříček

http://tomasp.net/blog | @tomaspetricek

Page 2: F# on the Server-Side

About me

Wrote book about F#Introduce functional conceptsand F# to C# developers

Worked with the F# teamContributed to VS 2010 integration

Reactive programming research

First blog post about F# in May 2006

Page 3: F# on the Server-Side

Server-side specifics

Reliability and scalability is essential

Many concurrent requests

Many I/O bound operations

Exist to share state with clients

We need different programming style!

Page 4: F# on the Server-Side

Plan of the talk

Event based Asynchronous workflows Agent based

Page 5: F# on the Server-Side

Event-based style

Programming using explicit callbacks

Solves part of the problemInherently concurrent model

Avoids blocking of threads

Gaining popularity (e.g. Node.js)

HttpServer.Start("http://localhost:8080/", fun ctx -> WebClient.DownloadAsync(getProxyUrl(ctx), fun data -> ctx.ResponseStream.WriteAsync(data, fun res -> ctx.ResponseStream.Close())))

Page 6: F# on the Server-Side

Limitations of event-based style

What we really wanted to write?

What we really should write…

let data = WebClient.DownloadAsync(getProxyUrl(ctx))ctx.ResponseStream.WriteAsync(data)ctx.ResponseStream.Close()

try let data = WebClient.DownloadAsync(getProxyUrl(ctx)) ctx.ResponseStream.WriteAsync(data)finally ctx.ResponseStream.Close()

Page 7: F# on the Server-Side

Plan of the talk

Event based Asynchronous workflows Agent based

Page 8: F# on the Server-Side

F# asynchronous workflows

Write usual sequential code

Asynchronous calls using let! and do!

Supports all F# control-flow constructs

let copyPageTo url outputStream = async { try let! html = WebClient.AsyncDownload(url) do! outputStream.AsyncWrite(html) finally ctx.ResponseStream.Close() }

Page 9: F# on the Server-Side

F# asynchronous workflows

Translated using some primitive operations

Looks similar to event-based programs…

Many thanks to Haskell for Monads™!

let copyPageTo url outputStream = async.TryFinally( async.Bind(WebClient.AsyncDownload(url), fun html -> async.Bind( outputStream.AsyncWrite(html), async.Zero() )), fun () -> ctx.ResponseStream.Close())

Page 10: F# on the Server-Side

Demo

HTTP proxy using asynchronous workflows

Page 11: F# on the Server-Side

Plan of the talk

Event based Asynchronous workflows Agent based

Page 12: F# on the Server-Side

Agent-based programming

Program consists of agents (processes)Lightweight and asynchronous

Agents communicate via messagesThread-safe and queued

Enables parallelismDifferent agents vs. multiple instances

Many thanks to Erlang for Agents™!

Page 13: F# on the Server-Side

Simple agent in F#

Send Hello to the caller

Waiting for message is asynchronous

Can perform long-running I/O before replying

Calling agent asynchronously

let echo = Agent.Start(fun agent -> async { while true do let! name, rchan = agent.Receive() rchan.Reply("Hello " + name) })

let! str = echo.PostAndAsyncReply(fun ch -> "Tomas", ch)

Page 14: F# on the Server-Side

Demo

Caching web pages using agent

Page 15: F# on the Server-Side

Summary and Q&A

Event-based is good starting point

F# workflow syntax really helps

Asynchronous workflows enable agents

Use agents for server-side state