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

fix: losing registered mDNS hosts #137

Merged
merged 5 commits into from
Feb 3, 2025
Merged
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
2 changes: 1 addition & 1 deletion services/platform/daemon/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/home-cloud-io/core/api v0.8.2
github.com/mackerelio/go-osstat v0.2.5
github.com/pion/stun/v2 v2.0.0
github.com/steady-bytes/draft/pkg/chassis v0.4.1
github.com/steady-bytes/draft/pkg/chassis v0.4.2
github.com/steady-bytes/draft/pkg/loggers v0.2.3
golang.org/x/crypto v0.28.0
golang.org/x/mod v0.18.0
Expand Down
4 changes: 2 additions & 2 deletions services/platform/daemon/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ=
github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk=
github.com/steady-bytes/draft/api v1.0.0 h1:QIvboAJgU0fBOV6BKChLMhHYIjBTR53+LJaR3w3g0NY=
github.com/steady-bytes/draft/api v1.0.0/go.mod h1:mlwxjvRiqvwySGdzVmF8voFhMffWq2F7dyd+xt6kENA=
github.com/steady-bytes/draft/pkg/chassis v0.4.1 h1:ZJLaNUyxnhm8vAzvNYDus+xUarczBGNQOtwi12m2EJM=
github.com/steady-bytes/draft/pkg/chassis v0.4.1/go.mod h1:Ee6UcaJ5rJbElW7t0pgryhir3qb12rmHhHKiyrTrdxM=
github.com/steady-bytes/draft/pkg/chassis v0.4.2 h1:I7YhVcYK5hrukv02PdcolQHnsWgW0kW63p4wcaNNlNA=
github.com/steady-bytes/draft/pkg/chassis v0.4.2/go.mod h1:Ee6UcaJ5rJbElW7t0pgryhir3qb12rmHhHKiyrTrdxM=
github.com/steady-bytes/draft/pkg/loggers v0.2.3 h1:tZadHH8f9fo+tRHVp3BaJlVYvKlrlX8Hd6LxncUVgAM=
github.com/steady-bytes/draft/pkg/loggers v0.2.3/go.mod h1:nXeOQ6lXhsVWHzRqVcJz0JIeSW75ORVN+0izJwwnH+Y=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
29 changes: 16 additions & 13 deletions services/platform/daemon/host/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,18 @@ func (p *dnsPublisher) Start() {
// start with initial set of hosts from config
hostnames := config.GetStringSlice(hostnamesConfigKey)
for _, hostname := range hostnames {
p.AddHost(ctx, hostname)
p.register(ctx, hostname)
}
}

func (p *dnsPublisher) AddHost(ctx context.Context, hostname string) {
c, cancel := context.WithCancel(ctx)
fqdn := p.buildFQDN(hostname)
logger := p.logger.WithField("fqdn", fqdn)

logger.Info("adding host to mDNS")

p.cancels[hostname] = cancel
go publish(c, logger, fqdn, p.address)

p.logger.Info("adding host to mDNS")
p.register(ctx, hostname)
err := setHostnames(p.cancels)
if err != nil {
logger.WithError(err).Error("failed to save hostname to config")
p.logger.WithError(err).Error("failed to save hostname to config")
}

logger.Info("host added to mDNS")
p.logger.Info("host added to mDNS")
}

func (p *dnsPublisher) RemoveHost(hostname string) error {
Expand Down Expand Up @@ -108,6 +100,17 @@ func (p *dnsPublisher) buildFQDN(hostname string) string {
return fmt.Sprintf("%s.%s", hostname, p.domain)
}

func (p *dnsPublisher) register(ctx context.Context, hostname string) {
// save cancelable context
c, cancel := context.WithCancel(ctx)
fqdn := p.buildFQDN(hostname)
logger := p.logger.WithField("fqdn", fqdn)
p.cancels[hostname] = cancel

logger.Info("publishing hostname to mDNS")
go publish(c, logger, fqdn, p.address)
}

func publish(ctx context.Context, logger chassis.Logger, fqdn, address string) {
for {
// if the context is cancelled just return
Expand Down