Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6-federation/close-grpc-client
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Close GRPC client after making a request to a remote federator.
19 changes: 14 additions & 5 deletions libs/wire-api-federation/src/Wire/API/Federation/GRPC/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Wire.API.Federation.GRPC.Client where
module Wire.API.Federation.GRPC.Client
( GrpcClientErr (..),
createGrpcClient,
grpcClientError,
)
where

import Control.Exception
import qualified Data.Text as T
Expand All @@ -32,11 +37,15 @@ createGrpcClient :: MonadIO m => GrpcClientConfig -> m (Either GrpcClientErr Grp
createGrpcClient cfg = do
res <- liftIO $ try @IOException $ setupGrpcClient' cfg
pure $ case res of
Left err -> Left (GrpcClientErr (T.pack (show err <> errorInfo)))
Right (Left err) -> Left (GrpcClientErr (T.pack (show err <> errorInfo)))
Left err -> Left (grpcClientError (Just cfg) err)
Right (Left err) -> Left (grpcClientError (Just cfg) err)
Right (Right client) -> Right client
where
errorInfo = addressToErrInfo $ _grpcClientConfigAddress cfg

grpcClientError :: Exception e => Maybe GrpcClientConfig -> e -> GrpcClientErr
grpcClientError mcfg err =
GrpcClientErr . T.pack $
displayException err
<> maybe "" (\cfg -> " " <> addressToErrInfo (_grpcClientConfigAddress cfg)) mcfg

addressToErrInfo :: Address -> String
addressToErrInfo = \case
Expand Down
3 changes: 3 additions & 0 deletions services/federator/src/Federator/InternalServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import qualified Polysemy.Error as Polysemy
import Polysemy.IO (embedToMonadIO)
import qualified Polysemy.Input as Polysemy
import qualified Polysemy.Reader as Polysemy
import qualified Polysemy.Resource as Polysemy
import Polysemy.TinyLog (TinyLog)
import qualified Polysemy.TinyLog as Log
import Wire.API.Federation.GRPC.Client (GrpcClientErr (..))
Expand Down Expand Up @@ -134,6 +135,7 @@ serveOutward env port = do
Polysemy.Error ServerError,
Polysemy.Reader RunSettings,
Polysemy.Input TLSSettings,
Polysemy.Resource,
Embed IO,
Embed Federator
]
Expand All @@ -143,6 +145,7 @@ serveOutward env port = do
runAppT env
. runM -- Embed Federator
. embedToMonadIO @Federator -- Embed IO
. Polysemy.runResource -- Resource
. Polysemy.runInputSem (embed @IO (readIORef (view tls env))) -- Input TLSSettings
. Polysemy.runReader (view runSettings env) -- Reader RunSettings
. absorbServerError
Expand Down
24 changes: 21 additions & 3 deletions services/federator/src/Federator/Remote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Federator.Remote
where

import Control.Lens ((^.))
import Control.Monad.Except
import Data.Default (def)
import Data.Domain (Domain, domainText)
import Data.String.Conversions (cs)
Expand All @@ -42,12 +43,14 @@ import Mu.GRpc.Client.Optics (GRpcReply)
import Mu.GRpc.Client.Record (GRpcMessageProtocol (MsgProtoBuf))
import Mu.GRpc.Client.TyApps (gRpcCall)
import Network.GRPC.Client.Helpers
import Network.HTTP2.Client.Exceptions
import Network.TLS as TLS
import qualified Network.TLS.Extra.Cipher as TLS
import Polysemy
import qualified Polysemy.Error as Polysemy
import qualified Polysemy.Input as Polysemy
import qualified Polysemy.Reader as Polysemy
import qualified Polysemy.Resource as Polysemy
import Polysemy.TinyLog (TinyLog)
import qualified Polysemy.TinyLog as Log
import qualified System.Logger.Message as Log
Expand All @@ -72,7 +75,8 @@ interpretRemote ::
DiscoverFederator,
TinyLog,
Polysemy.Reader RunSettings,
Polysemy.Input TLSSettings
Polysemy.Input TLSSettings,
Polysemy.Resource
]
r =>
Sem (Remote ': r) a ->
Expand All @@ -82,8 +86,8 @@ interpretRemote = interpret $ \case
target <-
Polysemy.mapError (RemoteErrorDiscoveryFailure vDomain) $
discoverFederatorWithError vDomain
client <- mkGrpcClient target
callInward client vRequest
Polysemy.bracket (mkGrpcClient target) (closeGrpcClient target) $ \client ->
callInward client vRequest

callInward :: MonadIO m => GrpcClient -> Request -> m (GRpcReply InwardResponse)
callInward client request =
Expand Down Expand Up @@ -154,6 +158,20 @@ mkGrpcClient target@(SrvTarget host port) = do
. Polysemy.fromEither
=<< Polysemy.fromExceptionVia (RemoteErrorTLSException target) (createGrpcClient cfg')

closeGrpcClient ::
Members '[Embed IO, Polysemy.Error RemoteError] r =>
SrvTarget ->
GrpcClient ->
Sem r ()
closeGrpcClient target =
Polysemy.mapError handle
. Polysemy.fromEitherM
. runExceptT
. close
where
handle :: ClientError -> RemoteError
handle = RemoteErrorClientFailure target . grpcClientError Nothing

logRemoteErrors ::
Members '[Polysemy.Error RemoteError, TinyLog] r =>
Sem r x ->
Expand Down