Browse Source

pass options to transport constructors

pull/1205/head
Marten Seemann 3 years ago
parent
commit
efb3b27038
  1. 15
      config/reflection_magic.go
  2. 4
      config/transport.go
  3. 20
      config/transport_test.go
  4. 4
      options.go

15
config/reflection_magic.go

@ -1,6 +1,7 @@
package config package config
import ( import (
"errors"
"fmt" "fmt"
"reflect" "reflect"
"runtime" "runtime"
@ -102,6 +103,7 @@ func makeConstructor(
tpt interface{}, tpt interface{},
tptType reflect.Type, tptType reflect.Type,
argTypes map[reflect.Type]constructor, argTypes map[reflect.Type]constructor,
opts ...interface{},
) (func(host.Host, *tptu.Upgrader, connmgr.ConnectionGater) (interface{}, error), error) { ) (func(host.Host, *tptu.Upgrader, connmgr.ConnectionGater) (interface{}, error), error) {
v := reflect.ValueOf(tpt) v := reflect.ValueOf(tpt)
// avoid panicing on nil/zero value. // avoid panicing on nil/zero value.
@ -123,17 +125,24 @@ func makeConstructor(
} }
return func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) (interface{}, error) { return func(h host.Host, u *tptu.Upgrader, cg connmgr.ConnectionGater) (interface{}, error) {
arguments := make([]reflect.Value, len(argConstructors)) arguments := make([]reflect.Value, 0, len(argConstructors)+len(opts))
for i, makeArg := range argConstructors { for i, makeArg := range argConstructors {
if arg := makeArg(h, u, cg); arg != nil { if arg := makeArg(h, u, cg); arg != nil {
arguments[i] = reflect.ValueOf(arg) arguments = append(arguments, reflect.ValueOf(arg))
} else { } else {
// ValueOf an un-typed nil yields a zero reflect // ValueOf an un-typed nil yields a zero reflect
// value. However, we _want_ the zero value of // value. However, we _want_ the zero value of
// the _type_. // the _type_.
arguments[i] = reflect.Zero(t.In(i)) arguments = append(arguments, reflect.Zero(t.In(i)))
} }
} }
for _, opt := range opts {
// don't panic on nil options
if opt == nil {
return nil, errors.New("expected a transport option, got nil")
}
arguments = append(arguments, reflect.ValueOf(opt))
}
return callConstructor(v, arguments) return callConstructor(v, arguments)
}, nil }, nil
} }

4
config/transport.go

@ -36,14 +36,14 @@ var transportArgTypes = argTypes
// //
// And returns a type implementing transport.Transport and, optionally, an error // And returns a type implementing transport.Transport and, optionally, an error
// (as the second argument). // (as the second argument).
func TransportConstructor(tpt interface{}) (TptC, error) { func TransportConstructor(tpt interface{}, opts ...interface{}) (TptC, error) {
// Already constructed? // Already constructed?
if t, ok := tpt.(transport.Transport); ok { if t, ok := tpt.(transport.Transport); ok {
return func(_ host.Host, _ *tptu.Upgrader, _ connmgr.ConnectionGater) (transport.Transport, error) { return func(_ host.Host, _ *tptu.Upgrader, _ connmgr.ConnectionGater) (transport.Transport, error) {
return t, nil return t, nil
}, nil }, nil
} }
ctor, err := makeConstructor(tpt, transportType, transportArgTypes) ctor, err := makeConstructor(tpt, transportType, transportArgTypes, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

20
config/transport_test.go

@ -5,11 +5,25 @@ import (
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport" "github.com/libp2p/go-libp2p-core/transport"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
"github.com/libp2p/go-tcp-transport"
"github.com/stretchr/testify/require"
) )
func TestTransportVariadicOptions(t *testing.T) { func TestTransportVariadicOptions(t *testing.T) {
_, err := TransportConstructor(func(_ peer.ID, _ ...int) transport.Transport { return nil }) _, err := TransportConstructor(func(_ peer.ID, _ ...int) transport.Transport { return nil })
if err != nil { require.NoError(t, err)
t.Fatal(err) }
}
func TestConstructorWithOpts(t *testing.T) {
var options []int
c, err := TransportConstructor(func(_ *tptu.Upgrader, opts ...int) transport.Transport {
options = opts
return tcp.NewTCPTransport(nil)
}, 42, 1337)
require.NoError(t, err)
_, err = c(nil, nil, nil)
require.NoError(t, err)
require.Equal(t, options, []int{42, 1337})
} }

4
options.go

@ -125,8 +125,8 @@ func Muxer(name string, tpt interface{}) Option {
// * Public Key // * Public Key
// * Address filter (filter.Filter) // * Address filter (filter.Filter)
// * Peerstore // * Peerstore
func Transport(tpt interface{}) Option { func Transport(tpt interface{}, opts ...interface{}) Option {
tptc, err := config.TransportConstructor(tpt) tptc, err := config.TransportConstructor(tpt, opts...)
err = traceError(err, 1) err = traceError(err, 1)
return func(cfg *Config) error { return func(cfg *Config) error {
if err != nil { if err != nil {

Loading…
Cancel
Save