From 10c05aa28d98b2ca3decddf5d63f022318bfcaa9 Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Mon, 7 Nov 2022 11:13:27 +0100 Subject: [PATCH 1/2] fix: add filter option for advertise Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- client.go | 24 +++++++++++++++++------- options.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 options.go diff --git a/client.go b/client.go index c47c66f..bb04bfa 100644 --- a/client.go +++ b/client.go @@ -40,16 +40,20 @@ type RendezvousClient interface { DiscoverSubscribe(ctx context.Context, ns string) (<-chan peer.AddrInfo, error) } -func NewRendezvousPoint(host host.Host, p peer.ID) RendezvousPoint { +func NewRendezvousPoint(host host.Host, p peer.ID, opts ...RendezvousPointOption) RendezvousPoint { + cfg := defaultRendezvousPointConfig + cfg.apply(opts...) return &rendezvousPoint{ - host: host, - p: p, + addrFactory: cfg.AddrsFactory, + host: host, + p: p, } } type rendezvousPoint struct { - host host.Host - p peer.ID + addrFactory AddrsFactory + host host.Host + p peer.ID } func NewRendezvousClient(host host.Host, rp peer.ID, sync ...RendezvousSyncClient) RendezvousClient { @@ -75,7 +79,13 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti r := ggio.NewDelimitedReader(s, inet.MessageSizeMax) w := ggio.NewDelimitedWriter(s) - req := newRegisterMessage(ns, peer.AddrInfo{ID: rp.host.ID(), Addrs: rp.host.Addrs()}, ttl) + addrs := rp.addrFactory(rp.host.Addrs()) + if len(addrs) == 0 { + return 0, fmt.Errorf("no addrs available to advertise: %s", ns) + } + + log.Debugf("advertising on `%s` with: %v", ns, addrs) + req := newRegisterMessage(ns, peer.AddrInfo{ID: rp.host.ID(), Addrs: addrs}, ttl) err = w.WriteMsg(req) if err != nil { return 0, err @@ -88,7 +98,7 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti } if res.GetType() != pb.Message_REGISTER_RESPONSE { - return 0, fmt.Errorf("Unexpected response: %s", res.GetType().String()) + return 0, fmt.Errorf("unexpected response: %s", res.GetType().String()) } response := res.GetRegisterResponse() diff --git a/options.go b/options.go new file mode 100644 index 0000000..1581d7f --- /dev/null +++ b/options.go @@ -0,0 +1,32 @@ +package rendezvous + +import ( + ma "github.com/multiformats/go-multiaddr" +) + +type RendezvousPointOption func(cfg *rendezvousPointConfig) + +type AddrsFactory func(addrs []ma.Multiaddr) []ma.Multiaddr + +var DefaultAddrFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr { return addrs } + +var defaultRendezvousPointConfig = rendezvousPointConfig{ + AddrsFactory: DefaultAddrFactory, +} + +type rendezvousPointConfig struct { + AddrsFactory AddrsFactory +} + +func (cfg *rendezvousPointConfig) apply(opts ...RendezvousPointOption) { + for _, opt := range opts { + opt(cfg) + } +} + +// AddrsFactory configures libp2p to use the given address factory. +func ClientWithAddrsFactory(factory AddrsFactory) RendezvousPointOption { + return func(cfg *rendezvousPointConfig) { + cfg.AddrsFactory = factory + } +} From cc0893258cf4cf61542be83a3770997f07e4512c Mon Sep 17 00:00:00 2001 From: gfanton <8671905+gfanton@users.noreply.github.com> Date: Mon, 16 Jan 2023 16:07:43 +0100 Subject: [PATCH 2/2] fix: ci macos Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com> --- .github/workflows/go.yml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 1f2dcc1..deda1fb 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -44,6 +44,14 @@ jobs: uses: actions/setup-go@v2 with: go-version: ${{ matrix.golang }} + + # https://github.com/mattn/go-sqlite3/tree/v1.14.16#mac-osx + - name: install sqlight (macos) + if: matrix.os == 'macos-latest' + run: | + brew install sqlite3 + brew upgrade icu4c + - name: Cache Go modules uses: actions/cache@v1 with: @@ -58,5 +66,11 @@ jobs: go mod tidy -v git --no-pager diff go.mod go.sum git --no-pager diff --quiet go.mod go.sum - - name: Run tests with race - run: go test -v -tags "libsqlite3" -race ./... -test.timeout=10m + + - name: Run tests with race (macos) + if: matrix.os == 'macos-latest' + run: go test -v -tags "darwin" -race ./... -test.timeout=10m + + - name: Run tests with race (linux) + if: matrix.os == 'ubuntu-latest' + run: go test -v -race ./... -test.timeout=10m