From fe5d0ce14cb3ed87b62081578148c3e9f1d617b9 Mon Sep 17 00:00:00 2001 From: Darell Tan Date: Wed, 9 Aug 2023 01:38:50 +0800 Subject: [PATCH] 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. https://github.com/golang/go/issues/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. --- bridge.go | 2 ++ go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bridge.go b/bridge.go index 31cc589..31baf06 100644 --- a/bridge.go +++ b/bridge.go @@ -15,6 +15,7 @@ import ( "encoding/json" "fmt" "log" + "net" "net/http" "reflect" "strings" @@ -163,6 +164,7 @@ func (br *Bridge) ConnectMQTT() error { SetUsername(br.Username). SetPassword(br.Password). SetClientID("hap-z2m"). + SetDialer(&net.Dialer{KeepAlive: -1}). SetKeepAlive(60 * time.Second). SetPingTimeout(2 * time.Second). SetConnectRetry(true) diff --git a/go.mod b/go.mod index 52ed4fc..9f3f78c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module hapz2m go 1.20 require ( - github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c + github.com/brutella/hap v0.0.27-0.20230808074808-04622f35422f github.com/eclipse/paho.mqtt.golang v1.4.2 ) diff --git a/go.sum b/go.sum index 9c1985a..38618f0 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/brutella/dnssd v1.2.7 h1:Uq2NgLzlUz5JWIzcug9xRU6v0UApHrlxbsREA5B1RrY= github.com/brutella/dnssd v1.2.7/go.mod h1:JoW2sJUrmVIef25G6lrLj7HS6Xdwh6q8WUIvMkkBYXs= -github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c h1:gLVgwrHzMZUn0d1SfAFvsCfHWF12pstyS1erpUE/GZ0= -github.com/brutella/hap v0.0.27-0.20230522083132-4ea1de40019c/go.mod h1:ilKzdnapk5SjRrhedSW1+IMlMCt5P4hR91jlIap33x4= +github.com/brutella/hap v0.0.27-0.20230808074808-04622f35422f h1:VTlmSBBN6+H1WojP55rLxvpivr/Fdm/0PmmvIk5dVF8= +github.com/brutella/hap v0.0.27-0.20230808074808-04622f35422f/go.mod h1:ilKzdnapk5SjRrhedSW1+IMlMCt5P4hR91jlIap33x4= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eclipse/paho.mqtt.golang v1.4.2 h1:66wOzfUHSSI1zamx7jR6yMEI5EuHnT1G6rNA5PM12m4=