0

We are trying to link data from a lidar to our serverless function running on OpenFaas subset Faasd. We are using the trigger to invoke this function when an object is within a certain distance. So we have the lidar publish data to the topic when an object is within a certain distance. Then we have the OpenFaas mqtt-connector to invoke the serverless function using triggers. The issue is that the mqtt-connector is being flooded by the publisher and is backed up within a second of an object being in the range.

We have tried changing the QOS (quality of service) all the way to 0, but even this change had no visible difference in the connector being backed up so quickly. We tried looking at ways to decrease the lidar polling frequency but didn't find much (We are using the LD-19 Lidar). So Lastly we are trying to find a way to increase the CPU Utilization of the connector in hopes that this can be sufficient, however we have only found ways to limit CPU Utilization.

This is our handler of the function being invoked:

import json
import paho.mqtt.client as mqtt

TOPIC = 'controller'
PORT = 1883
BROKER = "172.17.0.1" #mosquitto broker IP

def handle(event):
    message = event.decode()
    print("Received message: {}".format(message))
    msg = 'stop'
    client = mqtt.Client()

    client.connect(BROKER, PORT, 60)
    client.publish(TOPIC, msg)

return None
hybrid143
  • 1
  • 1
  • If things a backing up, then it's most likely that the code in your function is too slow, if it can not complete in the time between samples, then nothing is going to solve this other than reducing the time it takes to run. But since you've not shared the code we really can't help – hardillb Aug 08 '23 at 07:11
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 08 '23 at 14:01
  • Hi @hardillb , thanks for the reply I have updated the question including our handler file that is being invoked this is just our temporary as we are still testing, but we are publishing to another topic which is 'controller' to send commands. I am assuming most of the time is from connecting to the broker. – hybrid143 Aug 09 '23 at 05:16

1 Answers1

0

Creating a new MQTT client on each message is incredibly expensive, do it once and reuse it.

import json
import paho.mqtt.client as mqtt

TOPIC = 'controller'
PORT = 1883
BROKER = "172.17.0.1" #mosquitto broker IP
client = mqtt.Client()
client.connect(BROKER, PORT, 60)

def handle(event):
    message = event.decode()
    print("Received message: {}".format(message))
    msg = 'stop'
    
    client.publish(TOPIC, msg)

return None
hardillb
  • 54,545
  • 11
  • 67
  • 105