From 1b799eb9f8cd415edd36a232ba8cca93de7730ed Mon Sep 17 00:00:00 2001 From: Ethel Morgan Date: Sun, 28 Jun 2020 23:18:58 +0100 Subject: add a default client ID of binary_hostname_pid --- catbus.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/catbus.go b/catbus.go index 68dee1b..2a3a120 100644 --- a/catbus.go +++ b/catbus.go @@ -5,7 +5,10 @@ package catbus import ( + "fmt" "math/rand" + "os" + "path" "sync" "time" @@ -73,6 +76,7 @@ func NewClient(brokerURI string, options ClientOptions) Client { mqttOpts := mqtt.NewClientOptions() mqttOpts.AddBroker(brokerURI) mqttOpts.SetAutoReconnect(true) + mqttOpts.SetClientID(defaultClientID()) mqttOpts.SetOnConnectHandler(func(_ mqtt.Client) { client.stopAllTimers() client.startAllTimers() @@ -185,3 +189,10 @@ func messageFromMQTTMessage(msg mqtt.Message) Message { Topic: msg.Topic(), } } + +func defaultClientID() string { + binary := path.Base(os.Args[0]) + hostname, _ := os.Hostname() + pid := os.Getpid() + return fmt.Sprintf("%s_%s_%d", binary, hostname, pid) +} -- cgit v1.2.3