Skip to content

Commit ac45664

Browse files
authored
Merge pull request #7 from gfanton/feat/advertise-addr-factory
2 parents ea43c80 + cc08932 commit ac45664

File tree

3 files changed

+65
-9
lines changed

3 files changed

+65
-9
lines changed

.github/workflows/go.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ jobs:
4444
uses: actions/setup-go@v2
4545
with:
4646
go-version: ${{ matrix.golang }}
47+
48+
# https://github.com/mattn/go-sqlite3/tree/v1.14.16#mac-osx
49+
- name: install sqlight (macos)
50+
if: matrix.os == 'macos-latest'
51+
run: |
52+
brew install sqlite3
53+
brew upgrade icu4c
54+
4755
- name: Cache Go modules
4856
uses: actions/cache@v1
4957
with:
@@ -58,5 +66,11 @@ jobs:
5866
go mod tidy -v
5967
git --no-pager diff go.mod go.sum
6068
git --no-pager diff --quiet go.mod go.sum
61-
- name: Run tests with race
62-
run: go test -v -tags "libsqlite3" -race ./... -test.timeout=10m
69+
70+
- name: Run tests with race (macos)
71+
if: matrix.os == 'macos-latest'
72+
run: go test -v -tags "darwin" -race ./... -test.timeout=10m
73+
74+
- name: Run tests with race (linux)
75+
if: matrix.os == 'ubuntu-latest'
76+
run: go test -v -race ./... -test.timeout=10m

client.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,20 @@ type RendezvousClient interface {
4040
DiscoverSubscribe(ctx context.Context, ns string) (<-chan peer.AddrInfo, error)
4141
}
4242

43-
func NewRendezvousPoint(host host.Host, p peer.ID) RendezvousPoint {
43+
func NewRendezvousPoint(host host.Host, p peer.ID, opts ...RendezvousPointOption) RendezvousPoint {
44+
cfg := defaultRendezvousPointConfig
45+
cfg.apply(opts...)
4446
return &rendezvousPoint{
45-
host: host,
46-
p: p,
47+
addrFactory: cfg.AddrsFactory,
48+
host: host,
49+
p: p,
4750
}
4851
}
4952

5053
type rendezvousPoint struct {
51-
host host.Host
52-
p peer.ID
54+
addrFactory AddrsFactory
55+
host host.Host
56+
p peer.ID
5357
}
5458

5559
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
7579
r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
7680
w := ggio.NewDelimitedWriter(s)
7781

78-
req := newRegisterMessage(ns, peer.AddrInfo{ID: rp.host.ID(), Addrs: rp.host.Addrs()}, ttl)
82+
addrs := rp.addrFactory(rp.host.Addrs())
83+
if len(addrs) == 0 {
84+
return 0, fmt.Errorf("no addrs available to advertise: %s", ns)
85+
}
86+
87+
log.Debugf("advertising on `%s` with: %v", ns, addrs)
88+
req := newRegisterMessage(ns, peer.AddrInfo{ID: rp.host.ID(), Addrs: addrs}, ttl)
7989
err = w.WriteMsg(req)
8090
if err != nil {
8191
return 0, err
@@ -88,7 +98,7 @@ func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (ti
8898
}
8999

90100
if res.GetType() != pb.Message_REGISTER_RESPONSE {
91-
return 0, fmt.Errorf("Unexpected response: %s", res.GetType().String())
101+
return 0, fmt.Errorf("unexpected response: %s", res.GetType().String())
92102
}
93103

94104
response := res.GetRegisterResponse()

options.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package rendezvous
2+
3+
import (
4+
ma "github.com/multiformats/go-multiaddr"
5+
)
6+
7+
type RendezvousPointOption func(cfg *rendezvousPointConfig)
8+
9+
type AddrsFactory func(addrs []ma.Multiaddr) []ma.Multiaddr
10+
11+
var DefaultAddrFactory = func(addrs []ma.Multiaddr) []ma.Multiaddr { return addrs }
12+
13+
var defaultRendezvousPointConfig = rendezvousPointConfig{
14+
AddrsFactory: DefaultAddrFactory,
15+
}
16+
17+
type rendezvousPointConfig struct {
18+
AddrsFactory AddrsFactory
19+
}
20+
21+
func (cfg *rendezvousPointConfig) apply(opts ...RendezvousPointOption) {
22+
for _, opt := range opts {
23+
opt(cfg)
24+
}
25+
}
26+
27+
// AddrsFactory configures libp2p to use the given address factory.
28+
func ClientWithAddrsFactory(factory AddrsFactory) RendezvousPointOption {
29+
return func(cfg *rendezvousPointConfig) {
30+
cfg.AddrsFactory = factory
31+
}
32+
}

0 commit comments

Comments
 (0)