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

refactor to use multiple feeds #2

Closed
wants to merge 1 commit into from
Closed

Conversation

whyrusleeping
Copy link
Contributor

No description provided.

@keks
Copy link
Contributor

keks commented Oct 14, 2016

I'm porting Orbit to Go and this would be really helpful. Is there a reason this wasn't merged?

@keks
Copy link
Contributor

keks commented Oct 16, 2016

I see the code severely diverged. I'll try to make it mergable tomorrow!

@whyrusleeping
Copy link
Contributor Author

@keks we've decided against doing things this way. Although i'm not against being able to open multiple subsriptions for the same topic, its a little difficult to get working correctly (where the closing of subscriptions get a little sticky)

@keks
Copy link
Contributor

keks commented Oct 17, 2016

Can we chat about this tomorrow? I don't think it really makes sense this way so I'd like to find a good solution for this.

Edit: I see I misread your post, sorry. I think being able to do that is super important and I would like to know your concerns about this solution so I can figure something out that satisfies all conditions. Will you be on IRC tomorrow (Tuesday) at 17:00 UTC? If not, please propose some later time.

Thanks!

@whyrusleeping
Copy link
Contributor Author

@keks i'll likely be online at that time. Thats 9am for me, so its a tad early, but i've been waking up earlier lately and it should be fine.

@keks
Copy link
Contributor

keks commented Oct 19, 2016

Here are my thoughts. What do you think about this?

Note: The version I currently use is the feat/feed-refactor branch, rebased on top of master. you can find it here.

Currently Subscribe takes a context.Context and uses that for cancelling the subscription. That means the caller first needs to prepare a cancellable context and pass that to Subscribe. Usually cancellable contexts are used to actually cancel the called, blocking function. Subscribe doesn't block though.

I propose that Subscribe doesn't return a <-chan *Message but a Subscription like this:

type Subscription struct {
  toptic string
  ch <-chan *Message
  cancelCh chan *Subscription
  err error
}

func (sub *Subscription) Topic() string {
  return sub.topic
}

func (sub *Subscription) Next() (*Message, error) {
  msg, ok := <- sub.ch

  if !ok {
    return msg, err
  }

  return msg, nil
}

func (sub *Subscription) Cancel() {
  sub.cancelCh <- sub
}

That way you could use

sub, err := p.Subscribe(topic)
if err != nil { ... }
defer sub.Cancel()

as an idiom.

p.Subscribe works like it did before, except it returns a *Subscription. However, p.GetFeed would look a bit different:

func (p *PubSub) GetFeed(topic string) (*Subscription, error) {
    out := make(chan *Subscription, 1)
    p.addFeedHook <- &addFeedReq{
        topic: topic,
        resp:  out,
    }

    resp := <-out
    if resp == nil {
        return nil, fmt.Errorf("not subscribed to topic %s", topic)
    }
    return resp, nil
}

and the process loop would be changed like this:

      case sub := <- p.cancelCh:
        subs, ok := p.myTopics[req.topic]
        if !ok {
          continue
        }

        delete(subs, sub)
        if len(subs) == 0 { ... } // remove lower level subscription
      case req := <-p.addFeedHook:
        subs, ok := p.myTopics[req.topic]
        sub := &Subscription{
          ch: make(chan *Message),
          topic: req.topic,
          cancelCh: p.cancelCh,
        }
        if ok {
          subs[sub] = stuct{}{}
        }

        req.resp <- sub
      case ... //and so on

Note that I added cancelCh to PubSub and changed p.myTopics from map[string][]chan Message to map[string]map[*Subscription]struct{}. map[foo]struct{} is a common way to implement sets and you don't need to do the cleanup stuff you do here; instead you can simple delete the *Subscription.
This obviously requires changes all over the file, but well, that's life.

Wow, I guess I just could have changed all of this directly in the code, but I hope I get my intentions across better this way. If you sign off on this copypasta will be my friend :)

@whyrusleeping
Copy link
Contributor Author

@keks that looks pretty good to me. Good job on writing the whole PR in this PR :P

@keks
Copy link
Contributor

keks commented Dec 1, 2016

@whyrusleeping this can be closed as #10 is merged.

@whyrusleeping whyrusleeping deleted the feat/feed-refactor branch December 12, 2016 01:25
vyzo pushed a commit that referenced this pull request Dec 1, 2022
* Update go.mod

* Refactor GossipSub Construction  (#1)

* Enables non-atomic validation for peer scoring parameters (#499)

* decouples topic scoring parameters

* adds skiping atomic validation for topic parameters

* cleans up

* adds skip atomic validation to peer score threshold

* adds skip atomic validation for peer parameters

* adds test for non-atomic validation

* adds tests for peer score

* adds tests for peer score thresholds

* refactors tests

* chore: Update .github/workflows/stale.yml [skip ci]

* adds with gossipsub tracker

Co-authored-by: libp2p-mgmt-read-write[bot] <104492852+libp2p-mgmt-read-write[bot]@users.noreply.github.com>

* decouples options

* fixes conflict

* reverts back module

* fixes peer score helper

* Adds send control message to gossipsub router (#2)

* adjusts libp2p version (#3)

* Update go.mod (#4)

* adds app specific rpc handler

* Create ci.yml (#5)

* Create Makefile (#7)

* Revert "Merge branch 'yahya/gossipsub-router-interface'" (#6)

This reverts commit 1c91995.

* Update ci.yml (#9)

* Revert "Merge branch 'master' into yahya/adds-rpc-inspector"

This reverts commit 352d747.

* Revert "Merge remote-tracking branch 'origin/yahya/adds-rpc-inspector' into yahya/adds-rpc-inspector"

This reverts commit 586c5cb.

* Revert "Merge branch 'master' into yahya/adds-rpc-inspector"

This reverts commit 2e13ee8.

* moves app specific inspector to pubsub

* removes option from gossipsub

* moves app specific rpc inspector up

* refactors app specific to return an error

Co-authored-by: libp2p-mgmt-read-write[bot] <104492852+libp2p-mgmt-read-write[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants