[#3] dialer: Add default dialer.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-10-30 12:34:02 +03:00
parent 5298ec4295
commit ee601a7b5f
2 changed files with 60 additions and 0 deletions

31
default.go Normal file
View 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
View 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
}