Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
00fa6646
Commit
00fa6646
authored
8 years ago
by
Jeromy
Browse files
Options
Download
Email Patches
Plain Diff
use peerstore for protocol preferencce storage
parent
f21d06fb
master
2018-Q4-OKR
docs-improvements
feat/backoff-listing
feat/p2p-multiaddr
feat/pnet/working3
feat/protobuf
feat/relay-integrate
feat/udp
feat/update/go-reuseport
feature/standardize-readme
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-jccl6u
gx/update-nza0mn
jenkinsfile
kevina/fix-go-vet
multistream-ping
punching
revert-276-update-go-detect-race
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
v5.0.15
v5.0.14
v5.0.13
v5.0.12
v5.0.11
v5.0.10
v5.0.9
v5.0.8
v5.0.7
v5.0.6
v5.0.5
v5.0.4
v5.0.3
v5.0.2
v5.0.1
v5.0.0
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.5
v4.4.4
v4.4.3
v4.4.2
v4.4.1
v4.4.0
v4.3.12
v4.3.11
v4.3.10
v4.3.9
v4.3.8
v4.3.7
v4.3.6
v4.3.5
v4.3.4
v4.3.3
v4.3.2
v4.3.1
v4.3.0
v4.2.0
v4.1.0
v4.0.4
v4.0.3
v4.0.2
v4.0.1
v4.0.0
v3.6.0
v3.5.4
v3.5.3
v3.5.2
v3.5.1
v3.5.0
v3.4.3
v3.4.2
v3.4.1
v3.4.0
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
p2p/host/basic/basic_host.go
+24
-44
p2p/host/basic/basic_host.go
package.json
+10
-10
package.json
with
34 additions
and
54 deletions
+34
-54
p2p/host/basic/basic_host.go
View file @
00fa6646
...
...
@@ -2,7 +2,6 @@ package basichost
import
(
"io"
"sync"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
...
...
@@ -47,9 +46,6 @@ type BasicHost struct {
relay
*
relay
.
RelayService
natmgr
*
natManager
protoPrefs
map
[
peer
.
ID
]
map
[
protocol
.
ID
]
struct
{}
prefsLk
sync
.
Mutex
proc
goprocess
.
Process
bwc
metrics
.
Reporter
...
...
@@ -58,10 +54,9 @@ type BasicHost struct {
// New constructs and sets up a new *BasicHost with given Network
func
New
(
net
inet
.
Network
,
opts
...
interface
{})
*
BasicHost
{
h
:=
&
BasicHost
{
network
:
net
,
mux
:
msmux
.
NewMultistreamMuxer
(),
bwc
:
metrics
.
NewBandwidthCounter
(),
protoPrefs
:
make
(
map
[
peer
.
ID
]
map
[
protocol
.
ID
]
struct
{}),
network
:
net
,
mux
:
msmux
.
NewMultistreamMuxer
(),
bwc
:
metrics
.
NewBandwidthCounter
(),
}
h
.
proc
=
goprocess
.
WithTeardown
(
func
()
error
{
...
...
@@ -182,7 +177,11 @@ func (h *BasicHost) RemoveStreamHandler(pid protocol.ID) {
// to create one. If ProtocolID is "", writes no header.
// (Threadsafe)
func
(
h
*
BasicHost
)
NewStream
(
ctx
context
.
Context
,
p
peer
.
ID
,
pids
...
protocol
.
ID
)
(
inet
.
Stream
,
error
)
{
pref
:=
h
.
preferredProtocol
(
p
,
pids
)
pref
,
err
:=
h
.
preferredProtocol
(
p
,
pids
)
if
err
!=
nil
{
return
nil
,
err
}
if
pref
!=
""
{
return
h
.
newStream
(
ctx
,
p
,
pref
)
}
...
...
@@ -204,50 +203,31 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I
}
selpid
:=
protocol
.
ID
(
selected
)
s
.
SetProtocol
(
selpid
)
h
.
setPreferre
dProtocol
(
p
,
sel
pi
d
)
h
.
Peerstore
()
.
Ad
dProtocol
s
(
p
,
sel
ecte
d
)
return
mstream
.
WrapStream
(
s
,
h
.
bwc
),
nil
}
func
(
h
*
BasicHost
)
preferredProtocol
(
p
peer
.
ID
,
pids
[]
protocol
.
ID
)
protocol
.
ID
{
h
.
prefsLk
.
Lock
()
defer
h
.
prefsLk
.
Unlock
()
prefs
,
ok
:=
h
.
protoPrefs
[
p
]
if
!
ok
{
supported
,
err
:=
h
.
Peerstore
()
.
GetProtocols
(
p
)
if
err
!=
nil
{
log
.
Warningf
(
"error getting protocol for peer %s: %s"
,
p
,
err
)
return
""
}
prefs
=
make
(
map
[
protocol
.
ID
]
struct
{})
for
_
,
proto
:=
range
supported
{
prefs
[
protocol
.
ID
(
proto
)]
=
struct
{}{}
}
h
.
protoPrefs
[
p
]
=
prefs
func
pidsToStrings
(
pids
[]
protocol
.
ID
)
[]
string
{
out
:=
make
([]
string
,
len
(
pids
))
for
i
,
p
:=
range
pids
{
out
[
i
]
=
string
(
p
)
}
for
_
,
pid
:=
range
pids
{
if
_
,
ok
:=
prefs
[
pid
];
ok
{
return
pid
}
}
return
""
return
out
}
func
(
h
*
BasicHost
)
setPreferredProtocol
(
p
peer
.
ID
,
proto
protocol
.
ID
)
{
h
.
prefsLk
.
Lock
()
defer
h
.
prefsLk
.
Unlock
()
prefs
,
ok
:=
h
.
protoPrefs
[
p
]
if
!
ok
{
prefs
=
make
(
map
[
protocol
.
ID
]
struct
{})
h
.
protoPrefs
[
p
]
=
prefs
func
(
h
*
BasicHost
)
preferredProtocol
(
p
peer
.
ID
,
pids
[]
protocol
.
ID
)
(
protocol
.
ID
,
error
)
{
pidstrs
:=
pidsToStrings
(
pids
)
supported
,
err
:=
h
.
Peerstore
()
.
SupportsProtocols
(
p
,
pidstrs
...
)
if
err
!=
nil
{
return
""
,
err
}
prefs
[
proto
]
=
struct
{}{}
var
out
protocol
.
ID
if
len
(
supported
)
>
0
{
out
=
protocol
.
ID
(
supported
[
0
])
}
return
out
,
nil
}
func
(
h
*
BasicHost
)
newStream
(
ctx
context
.
Context
,
p
peer
.
ID
,
pid
protocol
.
ID
)
(
inet
.
Stream
,
error
)
{
...
...
This diff is collapsed.
Click to expand it.
package.json
View file @
00fa6646
...
...
@@ -54,9 +54,9 @@
"version"
:
"0.0.0"
},
{
"hash"
:
"Qm
NQynaz7qfriSUJkiEZUrm2Wen1u3Kj9goZzWtrPyu7XR
"
,
"hash"
:
"Qm
SpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52
"
,
"name"
:
"go-log"
,
"version"
:
"1.
1.2
"
"version"
:
"1.
2.0
"
},
{
"hash"
:
"QmPpRcbNUXauP3zWZ1NJMLWpe4QnmEHrd2ba2D3yqWznw7"
,
...
...
@@ -147,21 +147,21 @@
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qm
enZRWtBU6n19k2wYDEwGWSs4Dh472t8xPp9JyYJjXmLj
"
,
"hash"
:
"Qm
SVddpXhD2QziagxjdRs7eUeNypGugGxWqDsFeauv33XR
"
,
"name"
:
"go-libp2p-loggables"
,
"version"
:
"1.0.
8
"
"version"
:
"1.0.
9
"
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qmb
kwpCqdshmuq2T1RB78RzGiPczToJd5pC91r1qQnb3zn
"
,
"hash"
:
"Qmb
P93111oShRbdjoWvP3NZCUApTLkaPXgaNDaXZPfHQHR
"
,
"name"
:
"go-libp2p-secio"
,
"version"
:
"1.0.1
0
"
"version"
:
"1.0.1
1
"
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qme
zbDCPpw9hBtXdXHEajg6UEufALJLMtoQQ1YrZEMyMwf
"
,
"hash"
:
"Qme
p1A25MDNZxMno8oHZ5ForA5T2gw1EXAGdZzGCCpDD4n
"
,
"name"
:
"go-libp2p-transport"
,
"version"
:
"1.3.
3
"
"version"
:
"1.3.
4
"
},
{
"author"
:
"whyrusleeping"
,
...
...
@@ -183,9 +183,9 @@
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qm
cb7oW2zriXQSCn9GKKDbewm9qM2NF1mR551Zf1HfoQPZ
"
,
"hash"
:
"Qm
SZi9ygLohBUGyHMqE5N6eToPwqcg7bZQTULeVLFu7Q6d
"
,
"name"
:
"go-libp2p-peerstore"
,
"version"
:
"1.2.
1
"
"version"
:
"1.2.
2
"
}
],
"gxVersion"
:
"0.4.0"
,
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help