Skip to content

Commit fe5d0ce

Browse files
committed
Disable TCP keepalives explicitly
It looks like Go has adopted 15s TCP keepalives as a default for _all_ TCP connections, which is quite dumb if you ask me. golang/go#48622 For the HAP server's side, it degrades iOS battery life significantly by waking the device every 15s to respond to these packets. In the case as a normal MQTT client, it increases traffic on top of the 60s keepalive we've already set at the application layer. In both cases, the solution is to just explicitly disable TCP keepalives. Upgrade hap to the latest version that contains the fix brutella/hap#36.
1 parent 4eac1ba commit fe5d0ce

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

bridge.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"encoding/json"
1616
"fmt"
1717
"log"
18+
"net"
1819
"net/http"
1920
"reflect"
2021
"strings"
@@ -163,6 +164,7 @@ func (br *Bridge) ConnectMQTT() error {
163164
SetUsername(br.Username).
164165
SetPassword(br.Password).
165166
SetClientID("hap-z2m").
167+
SetDialer(&net.Dialer{KeepAlive: -1}).
166168
SetKeepAlive(60 * time.Second).
167169
SetPingTimeout(2 * time.Second).
168170
SetConnectRetry(true)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module hapz2m
33
go 1.20
44

55
require (
6-
github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c
6+
github.com/brutella/hap v0.0.27-0.20230808074808-04622f35422f
77
github.com/eclipse/paho.mqtt.golang v1.4.2
88
)
99

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/brutella/dnssd v1.2.7 h1:Uq2NgLzlUz5JWIzcug9xRU6v0UApHrlxbsREA5B1RrY=
22
github.com/brutella/dnssd v1.2.7/go.mod h1:JoW2sJUrmVIef25G6lrLj7HS6Xdwh6q8WUIvMkkBYXs=
3-
github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c h1:gLVgwrHzMZUn0d1SfAFvsCfHWF12pstyS1erpUE/GZ0=
4-
github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c/go.mod h1:ilKzdnapk5SjRrhedSW1+IMlMCt5P4hR91jlIap33x4=
3+
github.com/brutella/hap v0.0.27-0.20230808074808-04622f35422f h1:VTlmSBBN6+H1WojP55rLxvpivr/Fdm/0PmmvIk5dVF8=
4+
github.com/brutella/hap v0.0.27-0.20230808074808-04622f35422f/go.mod h1:ilKzdnapk5SjRrhedSW1+IMlMCt5P4hR91jlIap33x4=
55
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
66
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
77
github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=

0 commit comments

Comments
 (0)