forked from snapframework/snap-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleBackend.hs
324 lines (272 loc) · 10.2 KB
/
SimpleBackend.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Snap.Internal.Http.Server.SimpleBackend
( simpleEventLoop
) where
------------------------------------------------------------------------------
import Control.Monad.Trans
import Control.Concurrent hiding (yield)
import Control.Exception
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString as S
import Data.ByteString.Internal (c2w)
import Data.Maybe
import Data.Typeable
import Data.Word
import Foreign hiding (new)
import Foreign.C
import GHC.Conc (labelThread, forkOnIO)
import Network.Socket
import Prelude hiding (catch)
------------------------------------------------------------------------------
import Data.Concurrent.HashMap (hashString)
import Snap.Internal.Debug
import Snap.Internal.Http.Server.Date
import qualified Snap.Internal.Http.Server.TimeoutTable as TT
import Snap.Internal.Http.Server.TimeoutTable (TimeoutTable)
import Snap.Internal.Http.Server.Backend
import qualified Snap.Internal.Http.Server.ListenHelpers as Listen
import Snap.Iteratee hiding (map)
#if defined(HAS_SENDFILE)
import qualified System.SendFile as SF
import System.Posix.IO
import System.Posix.Types (Fd(..))
#endif
------------------------------------------------------------------------------
{- For each cpu, we store:
+ a list of accept threads, one per port.
+ one timeout table and one timeout thread. These timeout the session threads.
+ a mvar to signal when the timeout thread is shutdown
-}
data EventLoopCpu = EventLoopCpu
{ _boundCpu :: Int
, _acceptThreads :: [ThreadId]
, _timeoutTable :: TimeoutTable
, _timeoutThread :: ThreadId
, _exitMVar :: !(MVar ())
}
simpleEventLoop :: EventLoop
simpleEventLoop sockets cap elog handler = do
loops <- Prelude.mapM (newLoop sockets handler elog) [0..(cap-1)]
debug "simpleEventLoop: waiting for mvars"
--wait for all threads to exit
Prelude.mapM_ (takeMVar . _exitMVar) loops `finally` do
debug "simpleEventLoop: killing all threads"
mapM stopLoop loops
mapM Listen.closeSocket sockets
newLoop :: [ListenSocket]
-> SessionHandler
-> (S.ByteString -> IO ())
-> Int
-> IO EventLoopCpu
newLoop sockets handler elog cpu = do
tt <- TT.new
exit <- newEmptyMVar
accThreads <- forM sockets $ \p -> forkOnIO cpu $ acceptThread handler tt elog cpu p
tid <- forkOnIO cpu $ timeoutThread tt exit
return $ EventLoopCpu cpu accThreads tt tid exit
stopLoop :: EventLoopCpu -> IO ()
stopLoop loop = block $ do
Prelude.mapM_ killThread $ _acceptThreads loop
killThread $ _timeoutThread loop
acceptThread :: SessionHandler
-> TimeoutTable
-> (S.ByteString -> IO ())
-> Int
-> ListenSocket
-> IO ()
acceptThread handler tt elog cpu sock = loop
where
loop = do
debug $ "acceptThread: calling accept()"
(s,addr) <- accept $ Listen.listenSocket sock
debug $ "acceptThread: accepted connection from remote: " ++ show addr
forkOnIO cpu (go s addr `catches` cleanup)
loop
go = runSession handler tt sock
cleanup = [
Handler $ \(e :: SomeException) -> elog $ S.concat [ "SimpleBackend.acceptThread: ", S.pack . map c2w $ show e]
]
timeoutThread :: TimeoutTable -> MVar () -> IO ()
timeoutThread table exitMVar = do
go `catch` (\(_::SomeException) -> killAll)
putMVar exitMVar ()
where
go = do
debug "timeoutThread: waiting for activity on thread table"
TT.waitForActivity table
debug "timeoutThread: woke up, killing old connections"
killTooOld
go
killTooOld = do
now <- getCurrentDateTime
TT.killOlderThan (now - tIMEOUT) table
-- timeout = 30 seconds
tIMEOUT = 30
killAll = do
debug "Backend.timeoutThread: shutdown, killing all connections"
TT.killAll table
data AddressNotSupportedException = AddressNotSupportedException String
deriving (Typeable)
instance Show AddressNotSupportedException where
show (AddressNotSupportedException x) = "Address not supported: " ++ x
instance Exception AddressNotSupportedException
runSession :: SessionHandler -> TimeoutTable -> ListenSocket -> Socket -> SockAddr -> IO ()
runSession handler tt lsock sock addr = do
let fd = fdSocket sock
curId <- myThreadId
debug $ "Backend.withConnection: running session: " ++ show addr
labelThread curId $ "connHndl " ++ show fd
(rport,rhost) <-
case addr of
SockAddrInet p h -> do
h' <- inet_ntoa h
return (fromIntegral p, S.pack $ map c2w h')
x -> throwIO $ AddressNotSupportedException $ show x
laddr <- getSocketName sock
(lport,lhost) <-
case laddr of
SockAddrInet p h -> do
h' <- inet_ntoa h
return (fromIntegral p, S.pack $ map c2w h')
x -> throwIO $ AddressNotSupportedException $ show x
let sinfo = SessionInfo lhost lport rhost rport $ Listen.isSecure lsock
let curHash = hashString $ show curId
let timeout = tickleTimeout tt curId curHash
timeout
bracket (Listen.createSession lsock 8192 fd (threadWaitRead $ fromIntegral fd))
(\session -> block $ do
debug "thread killed, closing socket"
-- remove thread from timeout table
TT.delete curHash curId tt
eatException $ Listen.endSession lsock session
eatException $ shutdown sock ShutdownBoth
eatException $ sClose sock
)
(\s -> let writeEnd = writeOut lsock s sock timeout
in handler sinfo
(enumerate lsock s sock)
writeEnd
(sendFile lsock timeout fd writeEnd)
timeout
)
eatException :: IO a -> IO ()
eatException act = (act >> return ()) `catch` \(_::SomeException) -> return ()
------------------------------------------------------------------------------
sendFile :: ListenSocket
-> IO ()
-> CInt
-> Iteratee ByteString IO ()
-> FilePath
-> Int64
-> Int64
-> IO ()
#if defined(HAS_SENDFILE)
sendFile lsock tickle sock writeEnd fp start sz =
case lsock of
ListenHttp _ -> bracket (openFd fp ReadOnly Nothing defaultFileFlags)
(closeFd)
(go start sz)
_ -> do
step <- runIteratee writeEnd
run_ $ enumFilePartial fp (start,start+sz) step
where
go off bytes fd
| bytes == 0 = return ()
| otherwise = do
sent <- SF.sendFile sfd fd off bytes
if sent < bytes
then tickle >> go (off+sent) (bytes-sent) fd
else return ()
sfd = Fd sock
#else
sendFile _ _ _ writeEnd fp start sz = do
-- no need to count bytes
step <- runIteratee writeEnd
run_ $ enumFilePartial fp (start,start+sz) step
return ()
#endif
tickleTimeout :: TimeoutTable -> ThreadId -> Word -> IO ()
tickleTimeout table tid thash = do
debug "Backend.tickleTimeout"
now <- getCurrentDateTime
TT.insert thash tid now table
enumerate :: (MonadIO m)
=> ListenSocket
-> NetworkSession
-> Socket
-> Enumerator ByteString m a
enumerate port session sock = loop
where
dbg s = debug $ "SimpleBackend.enumerate(" ++ show (_socket session)
++ "): " ++ s
loop (Continue k) = do
dbg "reading from socket"
s <- liftIO $ timeoutRecv
case s of
Nothing -> do
dbg "got EOF from socket"
sendOne k ""
Just s' -> do
dbg $ "got " ++ Prelude.show (S.length s')
++ " bytes from read end"
sendOne k s'
loop x = returnI x
sendOne k s | S.null s = do
dbg "sending EOF to continuation"
enumEOF $ Continue k
| otherwise = do
dbg $ "sending " ++ show s ++ " to continuation"
step <- lift $ runIteratee $ k $ Chunks [s]
case step of
(Yield x st) -> do
dbg $ "got yield, remainder is " ++ show st
yield x st
r@(Continue _) -> do
dbg $ "got continue"
loop r
(Error e) -> throwError e
fd = fdSocket sock
#ifdef PORTABLE
timeoutRecv = Listen.recv port sock (threadWaitRead $ fromIntegral fd) session
#else
timeoutRecv = Listen.recv port (threadWaitRead $ fromIntegral fd) session
#endif
writeOut :: (MonadIO m)
=> ListenSocket
-> NetworkSession
-> Socket
-> IO ()
-> Iteratee ByteString m ()
writeOut port session sock tickle = loop
where
dbg s = debug $ "SimpleBackend.writeOut(" ++ show (_socket session)
++ "): " ++ s
loop = continue k
k EOF = yield () EOF
k (Chunks xs) = do
let s = S.concat xs
let n = S.length s
dbg $ "got chunk with " ++ show n ++ " bytes"
ee <- liftIO $ try $ timeoutSend s
case ee of
(Left (e::SomeException)) -> do
dbg $ "timeoutSend got error " ++ show e
throwError e
(Right _) -> do
let last10 = S.drop (n-10) s
dbg $ "wrote " ++ show n ++ " bytes, last 10=" ++ show last10
loop
fd = fdSocket sock
#ifdef PORTABLE
timeoutSend = Listen.send port sock tickle (threadWaitWrite $ fromIntegral fd) session
#else
timeoutSend = Listen.send port tickle (threadWaitWrite $ fromIntegral fd) session
#endif