GithubHelp home page GithubHelp logo

grpc-mqtt's Introduction

grpc-mqtt

Motivation

This library enables the use of gRPC over an MQTT connection. This can be particularly useful when you have a distributed fleet of gRPC servers behind firewalls, as the servers can be accessible over MQTT without needing to accept incoming connections.

Highlights

  • Makes gRPC calls over MQTT!
  • Client and RemoteClient code can be generated from .proto files
  • MQTT sessions can properly handle out-of-order messages
  • MQTT sessions will avoid re-processing duplicate requests

Overview

This library attempts to closely mirror the API of gRPC-haskell so that it can be easily swapped in and out with existing gRPC infrastructure. The basic flow of a request through this system: image

The two main components of this library are the modules Client and the RemoteClient.

Client

A connection to the MQTT broker can be created and used via withMQTTGRPCClient by providing an MQTTGRPCConfig.

Client functions for calling gRPC services over MQTT can be generated from your existing proto files with Template Haskell using mqttClientFuncs. The generated code requires the corresponding proto file to have also already been compiled using proto3-suite. See Test/ProtoClients.hs for an example.

General usage:

withMQTTGRPCClient logger myMQTTConfig $ \client -> do
  let AddHello mqttAdd mqttHelloSS = addHelloMqttClient client baseTopic
  result <- mqttAdd (MQTTNormalRequest (TwoInts 4 6) 2 [])
  ...

Here AddHello is a type that was generated by proto3-suite, and addHelloMqttClient is generated with mqttClientFuncs

RemoteClient

The RemoteClient performs the actual gRPC requests on behalf of the Client. Similarily to the Client, the RemoteClient code can be generated using mqttRemoteClientMethodMap. See Test/ProtoRemoteClients.hs for an example. The resulting MethodMap is a mapping from gRPC method names to a function for making that request. These maps can be combined if you have multiple gRPC servers running on the machine.

General usage:

withGRPCClient myGRPCClientConfig $ \grpcClient -> do
  methodMap <- addHelloRemoteClientMethodMap grpcClient
  runRemoteClient logger myMQTTConfig baseTopic methodMap

Using multiple servers:

methodMapAH <- addHelloRemoteClientMethodMap grpcClient1
methodMapMG <- multGoodbyeRemoteClientMethodMap grpcClient2
let methodMap = methodMapAH <> methodMapMG
runRemoteClient logger myMQTTConfig baseTopic methodMap

Batching

Typically, each message transmitted through GRPC method calls result in one or more MQTT packets published over MQTT. A packet size limit is configured in MQTTGRPCConfig.mqttMsgSizeLimit. If a message is larger than this limit, it will be split into multiple packets and then those packets are published.

The performance of streaming RPCs that transmit many small messages in a short time window can be improved, dramatically, by enabling batching. When batching is enabled, the sender accumulates many messages into one packet and then flushes them in a single publish operation. This reduces the MQTT protocol overhead and can result in better performace.

Batching can be enabled in one of the following ways:

  1. The mqttClientFuncs and mqttRemoteClientMethodMap template haskell functions accept a parameter that specifies whether batching should be enabled for the generated RPC methods. This is the recommended approach if you do not want to modify .proto files.
  2. A protocol buffer option hs_grpc_mqtt_batched_stream is available for use at the service or method level. Setting this to true/false will enable/disable batching respectively. The method level option has higher precedence than the service level option and the service level option has higher precedence than the template haskell parameter.

Example usage:

service AddHello {
  /* Enables batching for all methods in this service */
  option hs_grpc_mqtt_batched_stream = true;
  ...
}
service AddHello {
  /* Server Streaming method with batching */
  rpc HelloSSBatch(SSRqt) returns (stream SSRpy) {
    option hs_grpc_mqtt_batched_stream = true;
  }
}

Note that batching introduces an additional step between the client code triggering a send operation and the actual MQTT publish. Some messages accumulated in memory could get lost if the sender encounters an error or crashes before the messages are flushed. Also, the sender will hold these accumulated messages in memory for a long time if the messages are produced at a very low rate. The receiver will experience a delay in such cases because the sender does not publish anything till the limit is reached. It is not recommended to enable batching in such cases.

Building

This package uses Nix flakes to manage dependencies and provide a reproducible build environment.

To build the package:

nix build

To start a development environment:

nix develop

This starts a shell with required development tools - such as ghc and cabal - in the PATH. You can build and test the code with cabal.

grpc-mqtt's People

Contributors

friede80 avatar ixmatus avatar j6carey avatar kiara-riley avatar riz0id avatar rkaippully avatar tm-drtina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

grpc-mqtt's Issues

Remove `defaultBatchedStream` from `mqttRemoteClientMethodMap`

The function Network.GRPC.MQTT.TH.RemoteClient.mqttRemoteClientMethodMap is misleading. It takes a defaultBatchedStream parameter which is not used anywhere. The decision to use batching or not solely depends on the proto file options. This parameter should be removed.

Non-deterministic test failures

On main, running cabal test I've seen a couple different failures.

Here are the results of 3 consecutive cabal tests. A fourth run passed all tests.

Running 1 test suites...
Test suite test: RUNNING...
Tests: grpc-mqtt
    <...>
    Test.Service.ClientStream
      Test.Service.ClientStream.Unbatched: FAIL (1.52s)
        test/Test/Service.hs:431:
        expected: Just (OneInt {oneIntResult = 15})
         but got: Just (OneInt {oneIntResult = 13})
      Batched:                             SKIP
    Test.Service.ServerStream
      Test.Service.ServerStream.Unbatched: SKIP
      Test.Service.ServerStream.Batched:   SKIP
    Test.Service.BiDiStream
      Test.Service.BiDiStream.Unbatched:   SKIP
      Batched:                             SKIP
    Test.Service.Errors
      Test.Service.Errors.Timeout:         SKIP
      Test.Service.Errors.Missing:         SKIP
      Test.Service.Errors.Malform:         SKIP

9 out of 39 tests failed (13.95s)
Test suite test: FAIL
Running 1 test suites...
Test suite test: RUNNING...
Tests: grpc-mqtt
  <...>
  Test.Service
    Normal
      LongBytes:                           FAIL
        Exception: MQTTException "disconnected"
      Call:                                SKIP
    Test.Service.ClientStream
      Test.Service.ClientStream.Unbatched: SKIP
      Batched:                             SKIP
    Test.Service.ServerStream
      Test.Service.ServerStream.Unbatched: SKIP
      Test.Service.ServerStream.Batched:   SKIP
    Test.Service.BiDiStream
      Test.Service.BiDiStream.Unbatched:   SKIP
      Batched:                             SKIP
    Test.Service.Errors
      Test.Service.Errors.Timeout:         SKIP
      Test.Service.Errors.Missing:         SKIP
      Test.Service.Errors.Malform:         SKIP

11 out of 39 tests failed (20.43s)
Test suite test: FAIL
Running 1 test suites...
Test suite test: RUNNING...
Tests: grpc-mqtt
  <..>
  Test.Service
    Normal
      LongBytes:                           malloc_consolidate(): unaligned fastbin chunk detected
Test suite test: FAIL

Tests fail to compile with `proto3-suite` >= 0.5.0

Brought up in #22

When using proto3-suite >= 0.5.0

Building test suite 'test' for grpc-mqtt-0.1.0.0..
[ 1 of 16] Compiling Proto.Message    ( gen/test/Proto/Message.hs, /home/matt/Code/grpc-mqtt/dist-newstyle/build/x86_64-linux/ghc-8.10.4/grpc-mqtt-0.1.0.0/t/test/build/test/test-tmp/Proto/Message.o, /home/matt/Code/grpc-mqtt/dist-newstyle/build/x86_64-linux/ghc-8.10.4/grpc-mqtt-0.1.0.0/t/test/build/test/test-tmp/Proto/Message.dyn_o ) [Proto3.Suite.Types changed]
[ 2 of 16] Compiling Proto.Service    ( gen/test/Proto/Service.hs, /home/matt/Code/grpc-mqtt/dist-newstyle/build/x86_64-linux/ghc-8.10.4/grpc-mqtt-0.1.0.0/t/test/build/test/test-tmp/Proto/Service.o, /home/matt/Code/grpc-mqtt/dist-newstyle/build/x86_64-linux/ghc-8.10.4/grpc-mqtt-0.1.0.0/t/test/build/test/test-tmp/Proto/Service.dyn_o ) [Network.GRPC.HighLevel.Server.Unregistered changed]

gen/test/Proto/Service.hs:143:30: error:
    Not in scope: ‘optMaxMetadataSize’
    |
143 |                              optMaxMetadataSize = serverMaxMetadataSize})
    |                              ^^^^^^^^^^^^^^^^^^

This looks like its related to this change in grpc-haskell: awakesecurity/gRPC-haskell@1127770

Update `README.md` protobuf option section.

The documentation on grpc-mqtt's custom protobuf options in the README.md:

  • The boolean option haskell.grpc.mqtt.batched_stream.
  • The boolean option haskell.grpc.mqtt.batched_stream_service.
  • The boolean option haskell.grpc.mqtt.batched_stream_file.
  • The CLevel option haskell.grpc.mqtt.client_clevel.
  • The CLevel option haskell.grpc.mqtt.client_clevel_service.
  • The CLevel option haskell.grpc.mqtt.client_clevel_file.
  • The CLevel option haskell.grpc.mqtt.server_clevel.
  • The CLevel option haskell.grpc.mqtt.server_clevel_service.
  • The CLevel option haskell.grpc.mqtt.server_clevel_file.

I plan on opening a PR for this, this issue is just to keep a reminder for myself.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.