Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementation in Haskell using the transient libraries #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions haskell/transient/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Revision history for hasrocket-benchmark-transient

## 0.1.0.0 -- YYYY-mm-dd

* First version. Released on an unsuspecting world.
30 changes: 30 additions & 0 deletions haskell/transient/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Copyright (c) 2019, Alberto Gömez Corona

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Alberto Gömez Corona nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 changes: 58 additions & 0 deletions haskell/transient/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

{-# LANGUAGE OverloadedStrings #-}

-- haskell-transient application for the websocket shootout
-- https://github.com/hashrocket/websocket-shootout

-- this is the implementation of the benchmark with the transient libraries
-- https://github.com/transient-haskell

import Transient.Internals
import Transient.Move.Internals
import Transient.EVars
import Transient.Logged
import Transient.Move.Utils
import Data.Aeson as Aeson
import Data.Containers as H
import Control.Applicative

main= keep' $ do
broad <- newEVar -- An EVar is a channel with de-inverted logic and nondeterminist, see below
initNodeDef "localhost" 3000 $ apisample broad
return ()

apisample broad= api $ -- api executes the rest of the computation when some message arrives.
-- It call the computation with an empty message when there is a new
-- connection, so watchbroadcast may be called
-- messages are passed to the computation trough a state monad
-- each response of the computation, either single message or broadcast are
-- forwarded by "api" to the sender trough the websocket.

do msg <- param -- param read the message in the state. if the message is empty, it fails and
-- watchBroadcast- is executed.
processMessage broad msg

<|> watchBroadcast broad -- watch for broadcast messages
-- Since it is called once at the beginning of each socket connection
-- it will be as much readEVar watching as the number of connections



processMessage broad msg= do
Aeson.Object obj <- emptyIfNothing $ Aeson.decode msg
Aeson.String typ <- emptyIfNothing $ H.lookup "type" obj
case typ of
"echo" -> return msg
"broadcast" -> do
let res = Aeson.encode $ insertMap "type" "broadcastResult" obj
writeEVar broad msg -- write in the broadcast channel
return res


-- The EVar read the broadcast channel and return the results. It would generate a thread for
-- each response, but since `threads 0` is present, it uses a single thread in a loop.
-- Since it is called once at the beginning of each socket connection
-- it will be as much readEVar watching as the number of connections

watchBroadcast broad= threads 0 $ readEVar broad

2 changes: 2 additions & 0 deletions haskell/transient/Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
25 changes: 25 additions & 0 deletions haskell/transient/hasrocket-benchmark-transient.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Initial hasrocket-benchmark-transient.cabal generated by cabal init.
-- For further documentation, see http://haskell.org/cabal/users-guide/

name: hasrocket-benchmark-transient
version: 0.1.0.0
synopsis: version using haskell transient libraries for the hasrocket websockets shootout
-- description:
homepage: https://github.com/hashrocket/websocket-shootout
license: BSD3
license-file: LICENSE
author: Alberto Gömez Corona
maintainer: [email protected]
-- copyright:
category: Math
build-type: Simple
extra-source-files: ChangeLog.md
cabal-version: >=1.10

executable hasrocket-benchmark-transient
main-is: Main.hs
-- other-modules:
-- other-extensions:
build-depends: base >=2 && <5, transient, transient-universe, containers, aeson
hs-source-dirs: .
default-language: Haskell2010