Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-pubsub
Commits
375c4176
Commit
375c4176
authored
Feb 19, 2018
by
vyzo
Committed by
Steven Allen
Jun 12, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gossipsub publish
parent
b867200f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
131 additions
and
20 deletions
+131
-20
gossipsub.go
gossipsub.go
+115
-20
mcache.go
mcache.go
+16
-0
No files found.
gossipsub.go
View file @
375c4176
...
@@ -13,51 +13,142 @@ import (
...
@@ -13,51 +13,142 @@ import (
const
(
const
(
GossipSubID
=
protocol
.
ID
(
"/meshsub/1.0.0"
)
GossipSubID
=
protocol
.
ID
(
"/meshsub/1.0.0"
)
// overlay parameters
GossipSubD
=
6
GossipSubDlo
=
4
GossipSubDhi
=
12
)
)
func
NewGossipSub
(
ctx
context
.
Context
,
h
host
.
Host
,
opts
...
Option
)
(
*
PubSub
,
error
)
{
func
NewGossipSub
(
ctx
context
.
Context
,
h
host
.
Host
,
opts
...
Option
)
(
*
PubSub
,
error
)
{
rt
:=
&
GossipSubRouter
{}
rt
:=
&
GossipSubRouter
{
peers
:
make
(
map
[
peer
.
ID
]
protocol
.
ID
),
mesh
:
make
(
map
[
string
]
map
[
peer
.
ID
]
struct
{}),
fanout
:
make
(
map
[
string
]
map
[
peer
.
ID
]
struct
{}),
mcache
:
NewMessageCache
(
5
),
}
return
NewPubSub
(
ctx
,
h
,
rt
,
opts
...
)
return
NewPubSub
(
ctx
,
h
,
rt
,
opts
...
)
}
}
type
GossipSubRouter
struct
{
type
GossipSubRouter
struct
{
p
*
PubSub
p
*
PubSub
peers
map
[
peer
.
ID
]
protocol
.
ID
// peer protocols
mesh
map
[
string
]
map
[
peer
.
ID
]
struct
{}
// topic meshes
fanout
map
[
string
]
map
[
peer
.
ID
]
struct
{}
// topic fanout
mcache
*
MessageCache
}
}
func
(
f
s
*
GossipSubRouter
)
Protocols
()
[]
protocol
.
ID
{
func
(
g
s
*
GossipSubRouter
)
Protocols
()
[]
protocol
.
ID
{
return
[]
protocol
.
ID
{
GossipSubID
,
FloodSubID
}
return
[]
protocol
.
ID
{
GossipSubID
,
FloodSubID
}
}
}
func
(
f
s
*
GossipSubRouter
)
Attach
(
p
*
PubSub
)
{
func
(
g
s
*
GossipSubRouter
)
Attach
(
p
*
PubSub
)
{
f
s
.
p
=
p
g
s
.
p
=
p
go
f
s
.
heartbeatTimer
()
go
g
s
.
heartbeatTimer
()
}
}
func
(
f
s
*
GossipSubRouter
)
AddPeer
(
peer
.
ID
,
protocol
.
ID
)
{
func
(
g
s
*
GossipSubRouter
)
AddPeer
(
p
peer
.
ID
,
proto
protocol
.
ID
)
{
gs
.
peers
[
p
]
=
proto
}
}
func
(
fs
*
GossipSubRouter
)
RemovePeer
(
peer
.
ID
)
{
func
(
gs
*
GossipSubRouter
)
RemovePeer
(
p
peer
.
ID
)
{
delete
(
gs
.
peers
,
p
)
for
_
,
peers
:=
range
gs
.
mesh
{
delete
(
peers
,
p
)
}
for
_
,
peers
:=
range
gs
.
fanout
{
delete
(
peers
,
p
)
}
}
func
(
gs
*
GossipSubRouter
)
HandleRPC
(
rpc
*
RPC
)
{
// TODO
}
}
func
(
fs
*
GossipSubRouter
)
HandleRPC
(
rpc
*
RPC
)
{
func
(
gs
*
GossipSubRouter
)
Publish
(
from
peer
.
ID
,
msg
*
pb
.
Message
)
{
gs
.
mcache
.
Add
(
msg
)
}
tosend
:=
make
(
map
[
peer
.
ID
]
struct
{})
for
_
,
topic
:=
range
msg
.
GetTopicIDs
()
{
// any peers in the topic?
tmap
,
ok
:=
gs
.
p
.
topics
[
topic
]
if
!
ok
{
continue
}
func
(
fs
*
GossipSubRouter
)
Publish
(
from
peer
.
ID
,
msg
*
pb
.
Message
)
{
// floodsub peers
for
p
:=
range
tmap
{
if
gs
.
peers
[
p
]
==
FloodSubID
{
tosend
[
p
]
=
struct
{}{}
}
}
}
// gossipsub peers
gmap
,
ok
:=
gs
.
mesh
[
topic
]
if
ok
{
// direct peers in the mesh for topic
for
p
:=
range
gmap
{
tosend
[
p
]
=
struct
{}{}
}
}
else
{
// fanout peers, we are not in the mesh for topic
gmap
,
ok
=
gs
.
fanout
[
topic
]
if
!
ok
{
// we don't have any yet, pick some
var
peers
[]
peer
.
ID
for
p
:=
range
tmap
{
if
gs
.
peers
[
p
]
==
GossipSubID
{
peers
=
append
(
peers
,
p
)
}
}
if
len
(
peers
)
>
0
{
gmap
=
make
(
map
[
peer
.
ID
]
struct
{})
shufflePeers
(
peers
)
for
_
,
p
:=
range
peers
[
:
GossipSubD
]
{
gmap
[
p
]
=
struct
{}{}
}
gs
.
fanout
[
topic
]
=
gmap
}
}
}
for
p
:=
range
gmap
{
tosend
[
p
]
=
struct
{}{}
}
}
out
:=
rpcWithMessages
(
msg
)
for
pid
:=
range
tosend
{
if
pid
==
from
||
pid
==
peer
.
ID
(
msg
.
GetFrom
())
{
continue
}
func
(
fs
*
GossipSubRouter
)
Join
(
topic
string
)
{
mch
,
ok
:=
gs
.
p
.
peers
[
pid
]
if
!
ok
{
continue
}
select
{
case
mch
<-
out
:
default
:
log
.
Infof
(
"dropping message to peer %s: queue full"
,
pid
)
// Drop it. The peer is too slow.
}
}
}
}
func
(
fs
*
GossipSubRouter
)
Leave
(
topic
string
)
{
func
(
gs
*
GossipSubRouter
)
Join
(
topic
string
)
{
// TODO
}
func
(
gs
*
GossipSubRouter
)
Leave
(
topic
string
)
{
// TODO
}
}
func
(
f
s
*
GossipSubRouter
)
heartbeatTimer
()
{
func
(
g
s
*
GossipSubRouter
)
heartbeatTimer
()
{
ticker
:=
time
.
NewTicker
(
1
*
time
.
Second
)
ticker
:=
time
.
NewTicker
(
1
*
time
.
Second
)
defer
ticker
.
Stop
()
defer
ticker
.
Stop
()
...
@@ -65,16 +156,20 @@ func (fs *GossipSubRouter) heartbeatTimer() {
...
@@ -65,16 +156,20 @@ func (fs *GossipSubRouter) heartbeatTimer() {
select
{
select
{
case
<-
ticker
.
C
:
case
<-
ticker
.
C
:
select
{
select
{
case
f
s
.
p
.
eval
<-
f
s
.
heartbeat
:
case
g
s
.
p
.
eval
<-
g
s
.
heartbeat
:
case
<-
f
s
.
p
.
ctx
.
Done
()
:
case
<-
g
s
.
p
.
ctx
.
Done
()
:
return
return
}
}
case
<-
f
s
.
p
.
ctx
.
Done
()
:
case
<-
g
s
.
p
.
ctx
.
Done
()
:
return
return
}
}
}
}
}
}
func
(
fs
*
GossipSubRouter
)
heartbeat
()
{
func
(
gs
*
GossipSubRouter
)
heartbeat
()
{
// TODO
}
func
shufflePeers
(
peers
[]
peer
.
ID
)
{
// TODO
}
}
mcache.go
0 → 100644
View file @
375c4176
package
floodsub
import
(
pb
"github.com/libp2p/go-floodsub/pb"
)
func
NewMessageCache
(
win
int
)
*
MessageCache
{
return
&
MessageCache
{}
}
type
MessageCache
struct
{
}
func
(
mc
*
MessageCache
)
Add
(
msg
*
pb
.
Message
)
{
// TODO
}
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