@@ -1,5 +1,6 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE CPP #-}
34
45module Network.Wai.Handler.Warp.Run where
56
@@ -25,6 +26,17 @@ import qualified Network.Wai.Handler.Warp.Timeout as T
2526import Network.Wai.Handler.Warp.Types
2627import Prelude hiding (catch)
2728
29+-- Sock.recv first tries to call recvfrom() optimistically.
30+-- If EAGAIN returns, it polls incoming data with epoll/kqueue.
31+-- This code first polls incoming data with epoll/kqueue.
32+#define PESSIMISTIC_RECV 1
33+
34+#ifdef PESSIMISTIC_RECV
35+import System.Posix.Types (Fd(..))
36+import Control.Concurrent (threadWaitRead)
37+import Network.Socket (Socket(..))
38+#endif
39+
2840#if WINDOWS
2941import qualified Control.Concurrent.MVar as MV
3042import Network.Socket (withSocketsDo)
@@ -36,12 +48,20 @@ bytesPerRead = 4096
3648
3749-- | Default action value for 'Connection'
3850socketConnection :: Socket -> Connection
51+#ifdef PESSIMISTIC_RECV
52+socketConnection s@(MkSocket fd _ _ _ _) = Connection
53+#else
3954socketConnection s = Connection
55+#endif
4056 { connSendMany = Sock.sendMany s
4157 , connSendAll = Sock.sendAll s
4258 , connSendFile = \fp off len act hdr -> sendfileWithHeader s fp (PartOfFile off len) act hdr
4359 , connClose = sClose s
60+#ifdef PESSIMISTIC_RECV
61+ , connRecv = threadWaitRead (Fd fd) >> Sock.recv s bytesPerRead
62+#else
4463 , connRecv = Sock.recv s bytesPerRead
64+#endif
4565 }
4666
4767#if __GLASGOW_HASKELL__ < 702