-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sut): down throw errors in http client
- Loading branch information
1 parent
db06cfa
commit 9c59a0a
Showing
6 changed files
with
125 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Dumblog.ZeroCopy.HttpServer where | ||
|
||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Char8 as BS8 | ||
import Control.Exception (bracketOnError) | ||
import Network.Socket | ||
import Network.Socket.ByteString (sendAll) | ||
import GHC.Event | ||
import Control.Concurrent | ||
|
||
------------------------------------------------------------------------ | ||
|
||
httpServer :: Int -> IO () | ||
httpServer port = withSocketsDo $ do | ||
putStrLn "Starting http server on port 5002" | ||
sock <- listenOn port | ||
Just mgr <- getSystemEventManager | ||
key <- withFdSocket sock $ \fd -> | ||
registerFd mgr (client sock) (fromIntegral fd) evtRead MultiShot | ||
loop | ||
where | ||
loop = do | ||
threadDelay (10*1000*1000) | ||
loop | ||
|
||
client :: Socket -> FdKey -> Event -> IO () | ||
client sock _ _ = do | ||
(conn, _) <- accept sock | ||
sendAll conn msg | ||
close conn | ||
|
||
msg :: ByteString | ||
msg = BS8.pack "HTTP/1.0 200 OK\r\nContent-Length: 5\r\n\r\nPong!\r\n" | ||
|
||
|
||
listenOn :: Int -> IO Socket | ||
listenOn port = do | ||
addr <- resolve | ||
open addr | ||
where | ||
resolve = do | ||
let hints = defaultHints { | ||
addrFlags = [AI_PASSIVE] | ||
, addrSocketType = Stream | ||
} | ||
head <$> getAddrInfo (Just hints) (Just "localhost") (Just (show port)) | ||
|
||
open addr = bracketOnError (openSocket addr) (const (return ())) $ \sock -> do | ||
-- close $ \sock -> do | ||
setSocketOption sock ReuseAddr 1 | ||
withFdSocket sock setCloseOnExecIfNeeded | ||
bind sock (addrAddress addr) | ||
listen sock 1024 | ||
return sock | ||
|
||
openSocket :: AddrInfo -> IO Socket | ||
openSocket addr = socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Dumblog.ZeroCopy.Main where | ||
|
||
import Dumblog.ZeroCopy.HttpServer | ||
|
||
------------------------------------------------------------------------ | ||
|
||
zeroCopyDumblog :: Int -> Int -> IO () | ||
zeroCopyDumblog _capacity port = httpServer port | ||
|
||
main :: IO () | ||
main = zeroCopyDumblog (64 * 1024) 8054 |