Add default dialer #8
7 changed files with 112 additions and 1 deletions
3
Makefile
3
Makefile
|
@ -4,3 +4,6 @@ integration-test:
|
|||
|
||||
test:
|
||||
go test -count=1 -v ./...
|
||||
|
||||
patch-example:
|
||||
gopatch -d -p ./multinet.patch ./testdata/patch*
|
10
README.md
10
README.md
|
@ -38,4 +38,12 @@ if err != nil {
|
|||
`Multidialer` exposes `UpdateInterface()` method for updating state of a single link.
|
||||
`NetlinkWatcher` can wrap `Multidialer` type and perform all updates automatically.
|
||||
|
||||
TODO: describe needed capabilities here.
|
||||
TODO: describe needed capabilities here.
|
||||
|
||||
## Patch
|
||||
|
||||
To perform refactoring (use `multinet.Dial` instead of `net.Dial`) using [gopatch](https://github.com/uber-go/gopatch):
|
||||
|
||||
```bash
|
||||
gopatch -p ./multinet.patch <project directory>
|
||||
```
|
31
default.go
Normal file
31
default.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package multinet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultDialer Multidialer
|
||||
defaultDialerErr error
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
defaultDialer, err = NewDialer(Config{
|
||||
Balancer: BalancerTypeRoundRobin,
|
||||
Subnets: []string{"0.0.0.0/0", "::/0"},
|
||||
})
|
||||
if err != nil {
|
||||
defaultDialerErr = fmt.Errorf("failed to initialize default dialier: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Dial dials provided network and address using default dialer.
|
||||
func Dial(network, address string) (net.Conn, error) {
|
||||
if defaultDialerErr != nil {
|
||||
return nil, defaultDialerErr
|
||||
}
|
||||
return defaultDialer.DialContext(context.Background(), network, address)
|
||||
}
|
29
default_test.go
Normal file
29
default_test.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
//go:build integration
|
||||
|
||||
package multinet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestDefaultDialer(t *testing.T) {
|
||||
srv := startHTTP(t)
|
||||
defer require.NoError(t, srv.Close())
|
||||
|
||||
conn, err := Dial("tcp", "localhost:8080")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, conn.Close())
|
||||
}
|
||||
|
||||
func startHTTP(t *testing.T) *http.Server {
|
||||
srv := &http.Server{Addr: ":8080"}
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Test stub") })
|
||||
go func() {
|
||||
require.ErrorIs(t, srv.ListenAndServe(), http.ErrServerClosed)
|
||||
}()
|
||||
return srv
|
||||
}
|
7
multinet.patch
Normal file
7
multinet.patch
Normal file
|
@ -0,0 +1,7 @@
|
|||
@@
|
||||
@@
|
||||
+import "git.frostfs.info/TrueCloudLab/multinet"
|
||||
-import "net"
|
||||
|
||||
-net.Dial(...)
|
||||
+multinet.Dial(...)
|
19
testdata/patch_0.go
vendored
Normal file
19
testdata/patch_0.go
vendored
Normal file
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
const addr = "s01.frostfs.devenv:8080"
|
||||
|
||||
func main() {
|
||||
_, err := net.Dial(getNetwork(), addr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getNetwork() string {
|
||||
return "tcp"
|
||||
}
|
14
testdata/patch_1.go
vendored
Normal file
14
testdata/patch_1.go
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ip := net.IPv4(192, 168, 0, 10)
|
||||
_, err := net.Dial("tcp", ip.String()+":8080")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue