10000 Refactored raw connection creating · postgres-haskell/postgres-wire@763a901 · GitHub
[go: up one dir, main page]

Skip to content

Commit 763a901

Browse files
Refactored raw connection creating
1 parent f2a59ae commit 763a901

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

postgres-wire.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ library
3030
, socket-unix
3131
, vector
3232
, binary
33+
, safe
3334
, time
3435
, hashable
3536
, hashtables

src/Database/PostgreSQL/Driver/Connection.hs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# language FlexibleContexts #-}
12
module Database.PostgreSQL.Driver.Connection where
23

34

@@ -7,6 +8,7 @@ import Data.ByteString.Lazy (toStrict)
78
import Data.ByteString.Builder (Builder, toLazyByteString)
89
import Control.Monad
910
import Data.Traversable
11+
import Safe (headMay)
1012
import Data.Foldable
1113
import Control.Applicative
1214
import Data.IORef
@@ -87,7 +89,6 @@ defaultUnixPathDirectory = "/var/run/postgresql"
8789
unixPathFilename :: B.ByteString
8890
unixPathFilename = ".s.PGSQL."
8991

90-
9192
createRawConnection :: ConnectionSettings -> IO (Either Error RawConnection)
9293
createRawConnection settings
9394
| host == "" = unixConnection defaultUnixPathDirectory
@@ -96,30 +97,26 @@ createRawConnection settings
9697
where
9798
unixConnection dirPath = do
9899
let mAddress = socketAddressUnixPath $ makeUnixPath dirPath
99-
case mAddress of
100-
Nothing -> throwAuthErrorInIO AuthInvalidAddress
101-
Just address -> do
102-
s <- socket :: IO (Socket Unix Stream Unix)
103-
Socket.connect s address
104-
pure . Right $ constructRawConnection s
100+
createAndConnect mAddress (socket :: IO (Socket Unix Stream Unix))
105101

106102
tcpConnection = do
107-
addressInfo <- getAddressInfo (Just host) Nothing aiV4Mapped
108-
:: IO [AddressInfo Inet Stream TCP]
109-
case socketAddress <$> addressInfo of
110-
[] -> throwAuthErrorInIO AuthInvalidAddress
111-
(address:_) -> do
112-
s <- socket :: IO (Socket Inet Stream TCP)
113-
Socket.connect s address
114-
{ inetPort = fromIntegral $ settingsPort settings }
115-
pure . Right $ constructRawConnection s
116-
117-
host = settingsHost settings
103+
mAddress <- fmap socketAddress . headMay <$>
104+
(getAddressInfo (Just host) (Just portStr) aiV4Mapped
105+
:: IO [AddressInfo Inet Stream TCP])
106+
createAndConnect mAddress (socket :: IO (Socket Inet Stream TCP))
107+
108+
createAndConnect Nothing creating = throwAuthErrorInIO AuthInvalidAddress
109+
createAndConnect (Just address) creating = do
110+
s <- creating
111+
Socket.connect s address
112+
pure . Right $ constructRawConnection s
113+
114+
portStr = BS.pack . show $ settingsPort settings
115+
host = settingsHost settings
118116
makeUnixPath dirPath =
119117
-- 47 - `/`, removing slash on the end of the path
120118
let dir = B.reverse . B.dropWhile (== 47) $ B.reverse dirPath
121-
in dir <> "/" <> unixPathFilename
122-
<> BS.pack (show $ settingsPort settings)
119+
in dir <> "/" <> unixPathFilename <> portStr
123120

124121
constructRawConnection :: Socket f t p -> RawConnection
125122
constructRawConnection s = RawConnection

tests_connection/test.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ testConnection name confContent = testCase name $ withPghba confContent $
2828
, defaultSettings { settingsHost = "localhost" }
2929
]
3030
where
31-
connectAndClose settings = connect settings >>= close
31+
connectAndClose settings = connect settings >>= either (error . show) close
3232
defaultSettings = ConnectionSettings
3333
{ settingsHost = ""
3434
, settingsPort = 5432

0 commit comments

Comments
 (0)
0