Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Device options: allow to change MQTT connection keepalive value #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased
## Added
- Introduce the `KeepAlive` device option to configure the MQTT connection keepalive. Default to 30s,
which was the value used before.

## [0.90.2] - 2022-07-04
## Added
- Introduce the `DeviceOptions` struct to expose all possible configuration options for the device.
Expand Down
8 changes: 8 additions & 0 deletions device/device_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package device
import (
"crypto/x509"
"errors"
"time"

"gorm.io/gorm"
)
Expand Down Expand Up @@ -51,6 +52,12 @@ type DeviceOptions struct {
// MQTT client. It will use the PersistencyDir when it is activated.
UseMqttStore bool

// KeepAlive defines the amount of time that the MQTT client
// should wait before sending a PING request to the broker. This will
// allow the MQTT client to know that a connection has not been lost with Astarte.
// Defaults to 30 seconds.
KeepAlive time.Duration

// PersistencyDir is a known path that will be used by the SDK for MQTT
// persistency, for the default database if used, and for crypto storage unless
// CryptoDir is specified.
Expand Down Expand Up @@ -96,6 +103,7 @@ func NewDeviceOptions() DeviceOptions {
AutoReconnect: true,
ConnectRetry: true,
MaxRetries: 10,
KeepAlive: 30 * time.Second,
}
}

Expand Down
1 change: 1 addition & 0 deletions device/protocol_mqtt_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (d *Device) initializeMQTTClient() error {
opts.SetClientID(fmt.Sprintf("%s/%s", d.realm, d.deviceID))
opts.SetConnectTimeout(30 * time.Second)
opts.SetCleanSession(false)
opts.SetKeepAlive(d.opts.KeepAlive)

if d.opts.UseMqttStore {
s := mqtt.NewFileStore(filepath.Join(d.opts.PersistencyDir, "mqttstore"))
Expand Down