Skip to content

Commit 53614aa

Browse files
authored
Generalize endpoint in ClientConfig (#151)
gRPC library supports DNS as a default name-system, but others are supported as well: https://github.com/grpc/grpc/blob/master/doc/naming.md This PR generalizes `ClientConfig` such that any arbitrary endpoints are supported instead of only DNS based TCP/IP connections to a server. The server code is unmodified as that involves lot more work; it can be considered in a future PR. This is a breaking change, but clients only require minimal change to move to the new structure.
1 parent 0b37ef5 commit 53614aa

File tree

11 files changed

+27
-37
lines changed

11 files changed

+27
-37
lines changed

bench/Bench.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ serverOpts =
9797

9898
main :: IO ()
9999
main = bracket startServer stopServer $ const $ withGRPC $ \grpc ->
100-
withClient grpc (ClientConfig "localhost" 50051 [] Nothing Nothing) $ \c -> do
100+
withClient grpc (ClientConfig "localhost:50051" [] Nothing Nothing) $ \c -> do
101101
rmAdd <- clientRegisterMethodNormal c addMethod
102102
rmClientStream <- clientRegisterMethodClientStreaming c addClientStreamMethod
103103
rmServerStream <- clientRegisterMethodServerStreaming c addServerStreamMethod

core/grpc-haskell-core.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: grpc-haskell-core
2-
version: 0.4.0
2+
version: 0.5.0
33
synopsis: Haskell implementation of gRPC layered on shared C library.
44
homepage: https://github.com/awakenetworks/gRPC-haskell
55
license: Apache-2.0

core/src/Network/GRPC/LowLevel/Client.hs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ data ClientSSLConfig = ClientSSLConfig
5656

5757
-- | Configuration necessary to set up a client.
5858

59-
data ClientConfig = ClientConfig {clientServerHost :: Host,
60-
clientServerPort :: Port,
59+
data ClientConfig = ClientConfig {clientServerEndpoint :: Endpoint,
6160
clientArgs :: [C.Arg],
6261
-- ^ Optional arguments for setting up the
6362
-- channel on the client. Supplying an empty
@@ -74,9 +73,6 @@ data ClientConfig = ClientConfig {clientServerHost :: Host,
7473
-- pseudo-header will be set to the supplied value.
7574
}
7675

77-
clientEndpoint :: ClientConfig -> Endpoint
78-
clientEndpoint ClientConfig{..} = endpoint clientServerHost clientServerPort
79-
8076
addMetadataCreds :: C.ChannelCredentials
8177
-> Maybe C.ClientMetadataCreate
8278
-> IO C.ChannelCredentials
@@ -86,7 +82,7 @@ addMetadataCreds c (Just create) = do
8682
C.compositeChannelCredentialsCreate c callCreds C.reserved
8783

8884
createChannel :: ClientConfig -> C.GrpcChannelArgs -> IO C.Channel
89-
createChannel conf@ClientConfig{..} chanargs =
85+
createChannel ClientConfig{..} chanargs =
9086
case clientSSLConfig of
9187
Nothing -> C.grpcInsecureChannelCreate e chanargs C.reserved
9288
Just (ClientSSLConfig rootCertPath Nothing plugin) ->
@@ -101,7 +97,7 @@ createChannel conf@ClientConfig{..} chanargs =
10197
C.withChannelCredentials rootCert privKey clientCert $ \creds -> do
10298
creds' <- addMetadataCreds creds plugin
10399
C.secureChannelCreate creds' e chanargs C.reserved
104-
where (Endpoint e) = clientEndpoint conf
100+
where (Endpoint e) = clientServerEndpoint
105101

106102
createClient :: GRPC -> ClientConfig -> IO Client
107103
createClient grpc clientConfig =
@@ -138,7 +134,7 @@ clientRegisterMethod :: Client
138134
-> MethodName
139135
-> IO (C.CallHandle)
140136
clientRegisterMethod Client{..} meth = do
141-
let host = fromMaybe (unEndpoint (clientEndpoint clientConfig)) (clientAuthority clientConfig)
137+
let host = fromMaybe (unEndpoint (clientServerEndpoint clientConfig)) (clientAuthority clientConfig)
142138
C.grpcChannelRegisterCall clientChannel
143139
(unMethodName meth)
144140
host
@@ -149,7 +145,7 @@ clientRegisterMethodNormal :: Client
149145
-> MethodName
150146
-> IO (RegisteredMethod 'Normal)
151147
clientRegisterMethodNormal c meth = do
152-
let e = clientEndpoint (clientConfig c)
148+
let e = clientServerEndpoint (clientConfig c)
153149
h <- clientRegisterMethod c meth
154150
return $ RegisteredMethodNormal meth e h
155151

@@ -158,15 +154,15 @@ clientRegisterMethodClientStreaming :: Client
158154
-> MethodName
159155
-> IO (RegisteredMethod 'ClientStreaming)
160156
clientRegisterMethodClientStreaming c meth = do
161-
let e = clientEndpoint (clientConfig c)
157+
let e = clientServerEndpoint (clientConfig c)
162158
h <- clientRegisterMethod c meth
163159
return $ RegisteredMethodClientStreaming meth e h
164160

165161
clientRegisterMethodServerStreaming :: Client
166162
-> MethodName
167163
-> IO (RegisteredMethod 'ServerStreaming)
168164
clientRegisterMethodServerStreaming c meth = do
169-
let e = clientEndpoint (clientConfig c)
165+
let e = clientServerEndpoint (clientConfig c)
170166
h <- clientRegisterMethod c meth
171167
return $ RegisteredMethodServerStreaming meth e h
172168

@@ -175,7 +171,7 @@ clientRegisterMethodBiDiStreaming :: Client
175171
-> MethodName
176172
-> IO (RegisteredMethod 'BiDiStreaming)
177173
clientRegisterMethodBiDiStreaming c meth = do
178-
let e = clientEndpoint (clientConfig c)
174+
let e = clientServerEndpoint (clientConfig c)
179175
h <- clientRegisterMethod c meth
180176
return $ RegisteredMethodBiDiStreaming meth e h
181177

core/src/Network/GRPC/LowLevel/Client/Unregistered.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import qualified Network.GRPC.Unsafe.Time as C
1616
import Network.GRPC.LowLevel.Call
1717
import Network.GRPC.LowLevel.Client (Client (..),
1818
NormalRequestResult (..),
19-
clientEndpoint,
19+
clientServerEndpoint,
2020
compileNormalRequestResults)
2121
import Network.GRPC.LowLevel.CompletionQueue (TimeoutSeconds)
2222
import qualified Network.GRPC.LowLevel.CompletionQueue.Unregistered as U
@@ -34,7 +34,7 @@ clientCreateCall Client{..} meth timeout = do
3434
let parentCall = C.Call nullPtr
3535
C.withDeadlineSeconds timeout $ \deadline -> do
3636
U.channelCreateCall clientChannel parentCall C.propagateDefaults
37-
clientCQ meth (clientEndpoint clientConfig) deadline
37+
clientCQ meth (clientServerEndpoint clientConfig) deadline
3838

3939
withClientCall :: Client
4040
-> MethodName

core/tests/LowLevelTests.hs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ testAuthMetadataPropagate = testCase "auth metadata inherited by children" $ do
339339
(Just "tests/ssl/localhost.crt")
340340
Nothing
341341
(Just server1ClientPlugin)),
342-
clientServerPort = 50052
342+
clientServerEndpoint = "localhost:50052"
343343
}
344344

345345
server = do
@@ -673,7 +673,7 @@ testCustomUserAgent =
673673
where
674674
clientArgs = [UserAgentPrefix "prefix!", UserAgentSuffix "suffix!"]
675675
client =
676-
TestClient (ClientConfig "localhost" 50051 clientArgs Nothing Nothing) $
676+
TestClient (ClientConfig "localhost:50051" clientArgs Nothing Nothing) $
677677
\c -> do rm <- clientRegisterMethodNormal c "/foo"
678678
void $ clientRequest c rm 4 "" mempty
679679
server = TestServer (serverConf (["/foo"],[],[],[])) $ \s -> do
@@ -694,8 +694,7 @@ testClientCompression =
694694
where
695695
client =
696696
TestClient (ClientConfig
697-
"localhost"
698-
50051
697+
"localhost:50051"
699698
[CompressionAlgArg GrpcCompressDeflate]
700699
Nothing
701700
Nothing) $ \c -> do
@@ -712,8 +711,7 @@ testClientServerCompression :: TestTree
712711
testClientServerCompression =
713712
csTest' "client/server compression: no errors" client server
714713
where
715-
cconf = ClientConfig "localhost"
716-
50051
714+
cconf = ClientConfig "localhost:50051"
717715
[CompressionAlgArg GrpcCompressDeflate]
718716
Nothing
719717
Nothing
@@ -743,8 +741,7 @@ testClientServerCompressionLvl :: TestTree
743741
testClientServerCompressionLvl =
744742
csTest' "client/server compression: no errors" client server
745743
where
746-
cconf = ClientConfig "localhost"
747-
50051
744+
cconf = ClientConfig "localhost:50051"
748745
[CompressionLevelArg GrpcCompressLevelHigh]
749746
Nothing
750747
Nothing
@@ -789,7 +786,7 @@ testClientMaxReceiveMessageLengthChannelArg = do
789786
rm <- clientRegisterMethodNormal c "/foo"
790787
clientRequest c rm 1 pay mempty >>= k
791788
where
792-
conf = ClientConfig "localhost" 50051 [MaxReceiveMessageLength n] Nothing Nothing
789+
conf = ClientConfig "localhost:50051" [MaxReceiveMessageLength n] Nothing Nothing
793790

794791
-- Expect success when the max recv payload size is set to 4 bytes, and we
795792
-- are sent 4.
@@ -887,7 +884,7 @@ stdTestClient :: (Client -> IO ()) -> TestClient
887884
stdTestClient = TestClient stdClientConf
888885

889886
stdClientConf :: ClientConfig
890-
stdClientConf = ClientConfig "localhost" 50051 [] Nothing Nothing
887+
stdClientConf = ClientConfig "localhost:50051" [] Nothing Nothing
891888

892889
data TestServer = TestServer ServerConfig (Server -> IO ())
893890

core/tests/LowLevelTests/Op.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ serverConf :: ServerConfig
7979
serverConf = ServerConfig "localhost" 50051 [("/foo")] [] [] [] [] Nothing
8080

8181
clientConf :: ClientConfig
82-
clientConf = ClientConfig "localhost" 50051 [] Nothing Nothing
82+
clientConf = ClientConfig "localhost:50051" [] Nothing Nothing
8383

8484
clientEmptySendOps :: [Op]
8585
clientEmptySendOps =

examples/echo/echo-hs/EchoClient.hs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import qualified Data.Text.Lazy as TL
1414
import Echo
1515
import Network.GRPC.HighLevel.Client
1616
import Network.GRPC.LowLevel
17+
import Network.GRPC.LowLevel.Call (Endpoint(..))
1718
import Options.Generic
1819
import Prelude hiding (FilePath)
1920

2021
data Args = Args
21-
{ bind :: Maybe ByteString <?> "grpc endpoint hostname (default \"localhost\")"
22-
, port :: Maybe Int <?> "grpc endpoint port (default 50051)"
22+
{ endpoint :: Maybe ByteString <?> "grpc endpoint (default \"localhost:50051\")"
2323
, payload :: Maybe TL.Text <?> "string to echo (default \"hullo!\")"
2424
} deriving (Generic, Show)
2525
instance ParseRecord Args
@@ -32,8 +32,7 @@ main = do
3232
rqt = EchoRequest pay
3333
expected = EchoResponse pay
3434
cfg = ClientConfig
35-
(Host . fromMaybe "localhost" . unHelpful $ bind)
36-
(Port . fromMaybe 50051 . unHelpful $ port)
35+
(Endpoint . fromMaybe "localhost:50051" . unHelpful $ endpoint)
3736
[] Nothing Nothing
3837
withGRPC $ \g -> withClient g cfg $ \c -> do
3938
Echo{..} <- echoClient c

examples/hellos/hellos-client/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ doHelloBi c n = do
110110

111111
highlevelMain :: IO ()
112112
highlevelMain = withGRPC $ \g ->
113-
withClient g (ClientConfig "localhost" 50051 [] Nothing Nothing) $ \c -> do
113+
withClient g (ClientConfig "localhost:50051" [] Nothing Nothing) $ \c -> do
114114
let n = 100000
115115
putStrLn "-------------- HelloSS --------------"
116116
doHelloSS c n

examples/tutorial/ArithmeticClient.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import Arithmetic
77
import Network.GRPC.HighLevel.Generated
88

99
clientConfig :: ClientConfig
10-
clientConfig = ClientConfig { clientServerHost = "localhost"
11-
, clientServerPort = 50051
10+
clientConfig = ClientConfig { clientServerEndpoint = "localhost:50051"
1211
, clientArgs = []
1312
, clientSSLConfig = Nothing
1413
, clientAuthority = Nothing

examples/tutorial/TUTORIAL.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ The client-side code generated for us is `arithmeticClient`, which takes a `Clie
155155

156156
```haskell
157157
clientConfig :: ClientConfig
158-
clientConfig = ClientConfig { clientServerHost = "localhost"
159-
, clientServerPort = 50051
158+
clientConfig = ClientConfig { clientServerEndpoint = "localhost:50051"
160159
, clientArgs = []
161160
, clientSSLConfig = Nothing
162161
, clientAuthority = Nothing

0 commit comments

Comments
 (0)