Add default dialer #8

Merged
fyrchik merged 2 commits from dstepanov-yadro/multinet:feat/default_dialer into master 2024-09-04 19:51:22 +00:00
2 changed files with 60 additions and 0 deletions
Showing only changes of commit ee601a7b5f - Show all commits

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,

It looks like if balancer will be noop then there is no difference with net.Dial

It looks like if balancer will be `noop` then there is no difference with `net.Dial`
Subnets: []string{"0.0.0.0/0", "::/0"},
fyrchik marked this conversation as resolved Outdated

Does ::/0 or ::0/0work?

Does `::/0` or `::0/0`work?

yes, fixed

yes, fixed
})
if err != nil {
defaultDialerErr = fmt.Errorf("failed to initialize default dialier: %w", err)
}
}
// Dial dials provided network and address using default dialer.
fyrchik marked this conversation as resolved Outdated

s/defaule/default/

s/defaule/default/

fixed

fixed
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"
)
fyrchik marked this conversation as resolved Outdated

We add devenv requirement to the integration, how setting up a small server in tests?

We add devenv requirement to the integration, how setting up a small server in tests?

fixed

fixed
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())
aarifullin marked this conversation as resolved Outdated

err is already nil if it has just passed require.NoError(t, err)?

`err` is already `nil` if it has just passed `require.NoError(t, err)`?

Fixed

Fixed
}
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
}