Code Monkey home page Code Monkey logo

go-libp2p-pubsub-router's Introduction

go-libp2p-pubsub-router

GoDoc Coverage Status Build Status Discourse posts

A libp2p router that uses pubsub.

We currently only use this for IPNS over PubSub.

Documenation

See https://godoc.org/github.com/libp2p/go-libp2p-pubsub-router.

Contribute

Feel free to join in. All welcome. Open an issue!

This repository falls under the libp2p Code of Conduct.

Want to hack on libp2p?

License

MIT


The last gx published version of this module was: 0.5.18: QmaHVH3EqQD6DsE1yPgwfCThvFCwfkJ396uyrRSo3Ku1kH

go-libp2p-pubsub-router's People

Contributors

aschmahmann avatar dependabot-preview[bot] avatar dirkmc avatar galargh avatar guseggert avatar hannahhoward avatar hsanjuan avatar iand avatar kevina avatar kubuxu avatar libp2p-mgmt-read-write[bot] avatar magik6k avatar marten-seemann avatar nasdf avatar raulk avatar stebalien avatar vyzo avatar web-flow avatar web3-bot avatar whyrusleeping avatar yusefnapora avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-libp2p-pubsub-router's Issues

PutValue context not utilized or propagated into bootstrapping

PubsubValueStore.PutValue receives a context.Context but does not respect it. In particular, we have:

bootstrapPubsub(p.ctx, p.cr, p.host, topic)

which uses the PubsubValueStore's context instead of the PutValue context when calling the very expensive bootstrapPubsub function.

Additionally, bootstrapPubsub could be much less expensive if:

err := cr.Provide(ctx, rz, true)

was moved into the go func right below it.

After speaking with @Stebalien my current plan is to start by moving the Provide into the go func, and then look into moving bootstrapping into PubSub via the Discovery (https://github.com/libp2p/go-libp2p-discovery) interface, as indicated by:

// Note: rendezvous/boostrap should really be handled by the pubsub implementation itself!

@vyzo @raulk Any thoughts or issues with this?

handleSubscription should unregister itself on close

If for some reason the subscription dies or gets cancelled without being managed by PubsubValueStore.Cancel(key) then we can't resubscribe until we first call PubSubValueStore.Cancel(key).

func (p *PubsubValueStore) handleSubscription(sub *pubsub.Subscription, key string, cancel func()) {
defer sub.Cancel()
defer cancel()

If instead of just cancelling the subscription as above we triggered an internal cancellation function that would likely be better.

Local publishes should be validated

Currently, the local user can use this to send out an invalid record. This isn't a problem from a security standpoint (nobody will accept the record) but we still shouldn't allow it.

Review by vyzo

This is an extraction of the IPNS over pubsub module as a general-purpose ValueStore. It can be upgraded to a full-blown router with go-libp2p-routing-helpers (which, before you get scared away by all the code, are thoroughly tested).

Dont Require Full Access To PubSub Router And Use Minimal Interface Due To PubSub Topic Join Restrictions

Recent pubsub changes make it so that you can only join a topic once per instance of go-lib2p-pubsub.PubSub. If you provide a PubSub instance to this pubsub router, and then in a different goroutine try to join the same topic, you will get an error.

The latest changes to the way go-libp2p-pubsub (which personally I like) means you need to implement topic management yourself (as you must do now) or have go-libp2p-pubsub implement internal topic management, that ensures when you call Join on a topic, you dont actually attempt to create multiple topic handlers.

It is entirely plausible that one might want to also process messages coming in on the topic subscription both in this library, and other ones.

Personally speaking I have made the following changes to this library:

// MinimalPubsub allows us to provide the bare minimum pubsub functionality
// to the router, while still using the same pubsub instance in other goroutines.
// This is primarily done to allow callers to not have to worry about topic management
// due to topics only allowing a single "joiner".
type MinimalPubsub interface {
	RegisterTopicValidator(topic string, val pubsub.Validator, opts ...pubsub.ValidatorOpt) error
	Join(topic string, opts ...pubsub.TopicOpt) (*pubsub.Topic, error)
}

// NewPubsubValueStore constructs a new ValueStore that gets and receives records through pubsub. Ignore the `zap.Logger` argument, my fork uses the zap logging library directly
func NewPubsubValueStore(ctx context.Context, host host.Host, ps MinimalPubsub, validator record.Validator, logger *zap.Logger, opts ...Option) (*PubsubValueStore, error) {...}

Then in my custom ipfs node, I wrote a shim around go-libp2p-pubsub.PubSub that handles shared access to a pubsub topic by multiple goroutines (ie, go-libp2p-pubsub-router subscribed to a topic, and some arbitrary service using the same pubsub instance is performing processing and messages coming in on the topic)

PubsubValueStore should handle arbitrary merge functions

Currently the PubsubValueStore assumes Pubsub is distributing a single Key-Value pair which is updating by utilizing a best function that is passed in via the Validator interface.

This looks like:

bestVal, updated := best(currentVal, receivedVal)
if updated {
   data[key]=bestVal
   Publish(bestVal)
}

Instead it would be better to use an interface that allows for:

updatedState, delta := merge(currentState, receivedState)
if delta != nil {
   data[key]=updatedState
   Publish(delta)
}

Given that the PubsubValueStore assumes Get and Put of full data records (as opposed to deltas) it's probably fine to assume that the types of the state and deltas are the same. However, if we wanted we could also add a diff function that takes the states and computes a delta.

Some example merge functions:

  • Best (as we have now)
  • Union (if the record type is a set take the union of the set)
  • BestN (do a union, but only keep the best N records)

Having this (and landing #33) would make an implementation of the suggestion at libp2p/go-libp2p#694 (comment) fairly straightforward.

DHT providing never finishes

It uses a context with no deadline, which does not operate well with DHT providing:

goroutine 13313 [9m0s]:
github.com/libp2p/go-libp2p-kad-dht.(*IpfsDHT).Provide [0xc0001888f0, 0x1a534a0, 0xc0151884c0, 0xc002cf0ed0, 0x24, 0xc0003d2601, 0x0, 0x0]
	/root/gopath/pkg/mod/github.com/libp2p/[email protected]/routing.go:422
github.com/libp2p/go-libp2p-pubsub-router.bootstrapPubsub.func1 [0x7f2feb060d90, 0xc0001888f0, 0x1a534a0, 0xc0151884c0, 0xc002cf0ed0, 0x24, 0xc0181b8f50, 0x47]
	/root/gopath/pkg/mod/github.com/libp2p/[email protected]/pubsub.go:338
created by github.com/libp2p/go-libp2p-pubsub-router.bootstrapPubsub []
	/root/gopath/pkg/mod/github.com/libp2p/[email protected]/pubsub.go:337

Persist records

This router doesn't currently persist the records it finds to disk. We should consider doing that but this may depend on transaction support.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.