Skip to content

Commit

Permalink
feat: Add initial connection retry on fail
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 15, 2023
1 parent 7c0ef29 commit 296af1b
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
17 changes: 1 addition & 16 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"log/slog"
"net"
"os"
"os/signal"
"sync"
Expand All @@ -12,8 +11,6 @@ import (
"github.com/gabe565/sponsorblockcast/internal/config"
"github.com/gabe565/sponsorblockcast/internal/device"
"github.com/spf13/cobra"
"github.com/spf13/viper"
castdns "github.com/vishen/go-chromecast/dns"
)

func NewCommand() *cobra.Command {
Expand All @@ -38,19 +35,7 @@ func run(cmd *cobra.Command, args []string) (err error) {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()

var iface *net.Interface
interfaceName := viper.GetString(config.InterfaceKey)
if interfaceName != "" {
iface, err = net.InterfaceByName(interfaceName)
if err != nil {
return err
}
slog.Info("Searching for devices...", "interface", interfaceName)
} else {
slog.Info("Searching for devices...")
}

entries, err := castdns.DiscoverCastDNSEntries(ctx, iface)
entries, err := device.DiscoverCastDNSEntries(ctx)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import "github.com/spf13/viper"
func Load() {
PausedIntervalValue = viper.GetDuration(PausedIntervalKey)
PlayingIntervalValue = viper.GetDuration(PlayingIntervalKey)
InterfaceValue = viper.GetString(InterfaceKey)
CategoriesValue = viper.GetStringSlice(CategoriesKey)
}
7 changes: 5 additions & 2 deletions internal/config/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"github.com/spf13/viper"
)

var InterfaceKey = "interface"
var (
InterfaceKey = "interface"
InterfaceValue string
)

func Interface(cmd *cobra.Command) {
cmd.PersistentFlags().StringP(InterfaceKey, "i", "", "Network interface to use for multicast dns discovery")
cmd.PersistentFlags().StringP(InterfaceKey, "i", InterfaceValue, "Network interface to use for multicast dns discovery")
if err := viper.BindPFlag(InterfaceKey, cmd.PersistentFlags().Lookup(InterfaceKey)); err != nil {
panic(err)
}
Expand Down
66 changes: 66 additions & 0 deletions internal/device/dns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package device

import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"time"

"github.com/gabe565/sponsorblockcast/internal/config"
castdns "github.com/vishen/go-chromecast/dns"
)

var ErrDeviceNotFound = errors.New("device not found")

func DiscoverCastDNSEntryByUuid(ctx context.Context, uuid string) (castdns.CastEntry, error) {
var iface *net.Interface
if config.InterfaceValue != "" {
var err error
iface, err = net.InterfaceByName(config.InterfaceValue)
if err != nil {
return castdns.CastEntry{}, err
}
}

ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

entries, err := castdns.DiscoverCastDNSEntries(ctx, iface)
if err != nil {
return castdns.CastEntry{}, err
}

for {
select {
case <-ctx.Done():
return castdns.CastEntry{}, fmt.Errorf("%w: %s", ErrDeviceNotFound, uuid)
case entry := <-entries:
if entry.UUID == uuid {
return entry, nil
}
}
}
}

func DiscoverCastDNSEntries(ctx context.Context) (<-chan castdns.CastEntry, error) {
var iface *net.Interface
if config.InterfaceValue != "" {
var err error
iface, err = net.InterfaceByName(config.InterfaceValue)
if err != nil {
return nil, err
}
slog.Info("Searching for devices...", "interface", config.InterfaceValue)
} else {
slog.Info("Searching for devices...")
}

entries, err := castdns.DiscoverCastDNSEntries(ctx, iface)
if err != nil {
return nil, err
}

return entries, nil
}
18 changes: 15 additions & 3 deletions internal/device/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@ func Watch(ctx context.Context, entry castdns.CastEntry) {

app := application.NewApplication()

if err := app.Start(entry.GetAddr(), entry.GetPort()); err != nil {
logger.Warn("Failed to start application", "error", err.Error())
return
tries := 0
for {
if err := app.Start(entry.GetAddr(), entry.GetPort()); err == nil {
break
} else {
if tries == 0 {
logger.Warn("Failed to connect to device. Retrying")
}
tries += 1
entry, err = DiscoverCastDNSEntryByUuid(ctx, entry.UUID)
if err != nil && tries >= 10 {
logger.Warn("Failed to start application", "error", err.Error())
return
}
}
}
defer func() {
_ = app.Close(false)
Expand Down

0 comments on commit 296af1b

Please sign in to comment.