Skip to content

Commit

Permalink
Don't panic at startup when duplicate peers are configured
Browse files Browse the repository at this point in the history
Fixes #1077
  • Loading branch information
neilalexander committed Oct 28, 2023
1 parent 7f9d4f3 commit d72ec5f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/core/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ func (c *Core) _applyOption(opt SetupOption) (err error) {
if err != nil {
return fmt.Errorf("unable to parse peering URI: %w", err)
}
return c.links.add(u, v.SourceInterface, linkTypePersistent)
err = c.links.add(u, v.SourceInterface, linkTypePersistent)
switch err {
case ErrLinkAlreadyConfigured:
// Don't return this error, otherwise we'll panic at startup
// if there are multiple of the same peer configured
return nil
default:
return err
}
case ListenAddress:
c.config._listeners[v] = struct{}{}
case NodeInfo:
Expand Down
22 changes: 22 additions & 0 deletions src/core/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core

import (
"testing"

"github.com/yggdrasil-network/yggdrasil-go/src/config"
)

func TestDuplicatePeer(t *testing.T) {
cfg := config.GenerateConfig()
c, err := New(cfg.Certificate, nil)
if err != nil {
t.Fatal(err)
}

// Add the same peer multiple times. This should not return an error.
for i := 0; i < 5; i++ {
if err := c._applyOption(Peer{"tcp://1.2.3.4:4321", ""}); err != nil {
t.Fatalf("Adding peer failed on iteration %d", i)
}
}
}

0 comments on commit d72ec5f

Please sign in to comment.