Compare commits
2 commits
ee21dd73e0
...
f65c788d73
Author | SHA1 | Date | |
---|---|---|---|
f65c788d73 | |||
0e75be2ef2 |
2 changed files with 63 additions and 2 deletions
|
@ -3,6 +3,7 @@ package multinet
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
@ -18,6 +19,8 @@ const (
|
|||
BalancerTypeRoundRobin BalancerType = "roundrobin"
|
||||
)
|
||||
|
||||
var errNoSuitableNodeFound = errors.New("no suitale node found")
|
||||
|
||||
type balancer interface {
|
||||
DialContext(ctx context.Context, s *Subnet, network, address string) (net.Conn, error)
|
||||
}
|
||||
|
@ -39,7 +42,7 @@ func (r *roundRobin) DialContext(ctx context.Context, s *Subnet, network, addres
|
|||
dd.LocalAddr = ii.LocalAddr
|
||||
return r.d.dialContext(&dd, ctx, network, address)
|
||||
}
|
||||
return nil, errors.New("(*roundRobin).DialContext: no suitale node found")
|
||||
return nil, fmt.Errorf("(*roundRobin).DialContext: %w", errNoSuitableNodeFound)
|
||||
}
|
||||
|
||||
type firstEnabled struct {
|
||||
|
@ -57,5 +60,5 @@ func (r *firstEnabled) DialContext(ctx context.Context, s *Subnet, network, addr
|
|||
dd.LocalAddr = ii.LocalAddr
|
||||
return r.d.dialContext(&dd, ctx, network, address)
|
||||
}
|
||||
return nil, errors.New("(*firstEnabled).DialContext: no suitale node found")
|
||||
return nil, fmt.Errorf("(*firstEnabled).DialContext: %w", errNoSuitableNodeFound)
|
||||
}
|
||||
|
|
58
dialer_test.go
Normal file
58
dialer_test.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package multinet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestInterfacesDown(t *testing.T) {
|
||||
t.Run("noop balancer", func(t *testing.T) {
|
||||
d, err := NewDialer(Config{
|
||||
Subnets: []string{"10.11.12.0/24"},
|
||||
InterfaceSource: testDownInterfaces,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
conn, err := d.DialContext(context.Background(), "tcp", "10.11.12.254:8080")
|
||||
require.ErrorIs(t, err, errNoSuitableNodeFound)
|
||||
require.Nil(t, conn)
|
||||
})
|
||||
t.Run("round robin balancer", func(t *testing.T) {
|
||||
d, err := NewDialer(Config{
|
||||
Subnets: []string{"10.11.12.0/24"},
|
||||
InterfaceSource: testDownInterfaces,
|
||||
Balancer: BalancerTypeRoundRobin,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
conn, err := d.DialContext(context.Background(), "tcp", "10.11.12.254:8080")
|
||||
require.ErrorIs(t, err, errNoSuitableNodeFound)
|
||||
require.Nil(t, conn)
|
||||
})
|
||||
}
|
||||
|
||||
func testDownInterfaces() ([]Interface, error) {
|
||||
return []Interface{
|
||||
&testInterface{
|
||||
name: "data1",
|
||||
addrs: []net.Addr{
|
||||
&testAddr{
|
||||
network: "tcp",
|
||||
str: "10.11.12.101/24",
|
||||
},
|
||||
},
|
||||
down: true,
|
||||
},
|
||||
&testInterface{
|
||||
name: "data2",
|
||||
addrs: []net.Addr{
|
||||
&testAddr{
|
||||
network: "tcp",
|
||||
str: "10.11.12.102/24",
|
||||
},
|
||||
},
|
||||
down: true,
|
||||
},
|
||||
}, nil
|
||||
}
|
Loading…
Add table
Reference in a new issue