diff options
author | Ethel Morgan <eth@ethulhu.co.uk> | 2020-06-28 23:18:58 +0100 |
---|---|---|
committer | Ethel Morgan <eth@ethulhu.co.uk> | 2020-06-28 23:18:58 +0100 |
commit | 1b799eb9f8cd415edd36a232ba8cca93de7730ed (patch) | |
tree | 4dc6cec7b0fb2fce4dda1663d5447a2381f145c8 | |
parent | 264ae5e425e3c6f90fbea88ad66852a0b5186428 (diff) |
add a default client ID of binary_hostname_pidv0.0.5
-rw-r--r-- | catbus.go | 11 |
1 files changed, 11 insertions, 0 deletions
@@ -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) +} |