Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move things outside of the lock in obsaddr #582

Merged
merged 2 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions p2p/protocol/identify/obsaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) {
func (oas *ObservedAddrSet) Add(observed, local, observer ma.Multiaddr,
direction net.Direction) {

now := time.Now()
observerString := observerGroup(observer)
localString := string(local.Bytes())
ob := observation{
seenTime: now,
connDirection: direction,
}

oas.Lock()
defer oas.Unlock()

Expand All @@ -128,14 +136,6 @@ func (oas *ObservedAddrSet) Add(observed, local, observer ma.Multiaddr,
oas.ttl = pstore.OwnObservedAddrTTL
}

now := time.Now()
observerString := observerGroup(observer)
localString := string(local.Bytes())
ob := observation{
seenTime: now,
connDirection: direction,
}

observedAddrs := oas.addrs[localString]
// check if observed address seen yet, if so, update it
for i, previousObserved := range observedAddrs {
Expand Down
50 changes: 50 additions & 0 deletions p2p/protocol/identify/obsaddr_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package identify

import (
"sync"
"testing"
"time"

detectrace "github.com/ipfs/go-detect-race"
net "github.com/libp2p/go-libp2p-net"
ma "github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -117,3 +119,51 @@ func TestObsAddrSet(t *testing.T) {
t.Error("addrs should have timed out")
}
}

func TestAddAddrsProfile(b *testing.T) {
if detectrace.WithRace() {
b.Skip("test too slow when the race detector is running")
}
m := func(s string) ma.Multiaddr {
m, err := ma.NewMultiaddr(s)
if err != nil {
b.Fatal(err)
}
return m
}
oas := &ObservedAddrSet{}

add := func(oas *ObservedAddrSet, observed, observer ma.Multiaddr) {
dummyLocal := m("/ip4/127.0.0.1/tcp/10086")
dummyDirection := net.DirOutbound

oas.Add(observed, dummyLocal, observer, dummyDirection)
}

a1 := m("/ip4/1.2.3.4/tcp/1231")
a2 := m("/ip4/1.2.3.4/tcp/1232")
a3 := m("/ip4/1.2.3.4/tcp/1233")
a4 := m("/ip4/1.2.3.4/tcp/1234")
a5 := m("/ip4/1.2.3.4/tcp/1235")

b1 := m("/ip4/1.2.3.6/tcp/1236")
b2 := m("/ip4/1.2.3.7/tcp/1237")
b3 := m("/ip4/1.2.3.8/tcp/1237")
b4 := m("/ip4/1.2.3.9/tcp/1237")
b5 := m("/ip4/1.2.3.10/tcp/1237")

_ = []ma.Multiaddr{a1, a2, a3, a4, a5, b1, b2, b3, b4, b5}

var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 10000; j++ {
add(oas, a1, b1)
}
}()
}

wg.Wait()
}