prysm-pulse/shared/p2p/feed.go
terence tsao 1b5b8a57e0 Remove unused proto schemas (#3005)
* Update io_kubernetes_build commit hash to 1246899

* Update dependency build_bazel_rules_nodejs to v0.33.1

* Update dependency com_github_hashicorp_golang_lru to v0.5.1

* Update libp2p

* Update io_bazel_rules_k8s commit hash to e68d5d7

* Starting to remove old protos

* Bazel build proto passes

* Fixing pb version

* Cleaned up core package

* Fixing tests

* 6 tests failing

* Update proto bugs

* Fixed incorrect validator ordering proto

* Sync with master

* Update go-ssz commit

* Removed bad copies from v1alpha1 folder

* add json spec json to pb handler

* add nested proto example

* proto/testing test works

* fix refactoring build failures

* use merged ssz

* push latest changes

* used forked json encoding

* used forked json encoding

* fix warning

* fix build issues

* fix test and lint

* fix build

* lint
2019-07-22 10:03:57 -04:00

50 lines
1.6 KiB
Go

package p2p
import (
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/shared/event"
)
// Feed is a one to many subscription feed of the argument type.
//
// Messages received via p2p protocol are sent to subscribers by these event
// feeds. Message consumers should not use event feeds to reply to or broadcast
// messages. The p2p server will not relay them to peers. Rather, use the
// Send() or Broadcast() method on p2p.Server.
//
// Event feeds from p2p will always be of type p2p.Message. The message
// contains information about the sender, aka the peer, and the message payload
// itself.
//
// feed, err := ps.Feed(&ethpb.MyMessage{})
// ch := make(chan p2p.Message, 100) // Choose a reasonable buffer size!
// sub := feed.Subscribe(ch)
//
// // Wait until my message comes from a peer.
// msg := <- ch
// fmt.Printf("Message received: %v", msg.Data)
func (s *Server) Feed(msg proto.Message) Feed {
t := messageType(msg)
s.mutex.Lock()
defer s.mutex.Unlock()
if s.feeds[t] == nil {
s.feeds[t] = new(event.Feed)
}
return s.feeds[t]
}
// Feed implements one-to-many subscriptions where the carrier of events is a channel.
// Values sent to a Feed are delivered to all subscribed channels simultaneously.
//
// Feeds can only be used with a single type. The type is determined by the first Send or
// Subscribe operation. Subsequent calls to these methods panic if the type does not
// match.
//
// Implemented by https://github.com/ethereum/go-ethereum/blob/HEAD/event/feed.go
type Feed interface {
Subscribe(channel interface{}) event.Subscription
Send(value interface{}) (nsent int)
}