From 9959025d3bc2a378afdea39e1dbb2e436a9d5069 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Mon, 6 Mar 2017 14:19:48 -0800 Subject: [PATCH] Use RankNType to implement socket factory --- src/GitmonClient.hs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/GitmonClient.hs b/src/GitmonClient.hs index e88fb7cf3..84535fc40 100644 --- a/src/GitmonClient.hs +++ b/src/GitmonClient.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE RecordWildCards, BangPatterns, DeriveGeneric #-} +{-# LANGUAGE RecordWildCards, BangPatterns, DeriveGeneric, RankNTypes #-} {-# OPTIONS_GHC -fno-warn-unused-do-bind #-} module GitmonClient where @@ -57,10 +57,13 @@ instance ToJSON GitmonMsg where type ProcInfo = Either Y.ParseException (Maybe ProcIO) --- newtype SocketFactory a = SocketFactory { withSocket :: (Socket -> IO a) -> IO a } +newtype SocketFactory = SocketFactory { withSocket :: forall a. (Socket -> IO a) -> IO a } -reportGitmon'' :: String -> ReaderT LgRepo IO a -> ReaderT LgRepo IO a -reportGitmon'' program gitCommand = do +reportGitmon''' :: String -> ReaderT LgRepo IO a -> ReaderT LgRepo IO a +reportGitmon''' = reportGitmon'' SocketFactory { withSocket = withGitmonSocket } + +reportGitmon'' :: SocketFactory -> String -> ReaderT LgRepo IO a -> ReaderT LgRepo IO a +reportGitmon'' SocketFactory{..} program gitCommand = do (gitDir, realIP, repoName, userID) <- liftIO loadEnvVars (startTime, beforeProcIOContents) <- liftIO collectStats @@ -109,8 +112,8 @@ reportGitmon'' program gitCommand = do userID <- lookupEnv "GIT_SOCKSTAT_VAR_user_id" pure (gitDir, realIP, repoName, userID) -withSocket :: (Socket -> IO a) -> IO a -withSocket = bracket connectSocket close +withGitmonSocket :: (Socket -> IO c) -> IO c +withGitmonSocket = bracket connectSocket close where connectSocket = do s <- socket AF_UNIX Stream defaultProtocol