Skip to content

Commit

Permalink
Merge pull request #4822 from ipfs/fix/emit-once
Browse files Browse the repository at this point in the history
fix: use EmitOnce where only single response is to be sent
  • Loading branch information
whyrusleeping authored Mar 17, 2018
2 parents 7f1868a + ead6485 commit 958483f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
rootCmd := cmd2outputCmd("ipfs", root)
rootCmd.showOpts, _ = req.Options[flagsOptionName].(bool)
err := res.Emit(&rootCmd)
err := cmds.EmitOnce(res, &rootCmd)
if err != nil {
log.Error(err)
}
Expand Down
4 changes: 2 additions & 2 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ var filesStatCmd = &cmds.Command{
}

if !withLocal {
res.Emit(o)
cmds.EmitOnce(res, o)
return
}

Expand All @@ -161,7 +161,7 @@ var filesStatCmd = &cmds.Command{
o.Local = local
o.SizeLocal = sizeLocal

res.Emit(o)
cmds.EmitOnce(res, o)
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
Expand Down
38 changes: 30 additions & 8 deletions core/commands/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"fmt"
"io"
"net/http"
"sort"
"sync"
"time"

core "github.com/ipfs/go-ipfs/core"
e "github.com/ipfs/go-ipfs/core/commands/e"

floodsub "gx/ipfs/QmSFihvoND3eDaAYRCeLgLPt62yCPgMZs1NSZmKFEtJQQw/go-libp2p-floodsub"
pstore "gx/ipfs/QmXauCuJzmzapetmC6W4TuDJLL1yFFrVzSHoWv8YdbmnxH/go-libp2p-peerstore"
Expand Down Expand Up @@ -268,11 +270,26 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
return
}

for _, topic := range n.Floodsub.GetTopics() {
res.Emit(topic)
}
cmds.EmitOnce(res, stringList{n.Floodsub.GetTopics()})
},
Type: stringList{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(stringListEncoder),
},
Type: "",
}

func stringListEncoder(req *cmds.Request, w io.Writer, v interface{}) error {
list, ok := v.(*stringList)
if !ok {
return e.TypeErr(list, v)
}
for _, str := range list.Strings {
_, err := fmt.Fprintf(w, "%s\n", str)
if err != nil {
return err
}
}
return nil
}

var PubsubPeersCmd = &cmds.Command{
Expand Down Expand Up @@ -315,12 +332,17 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
topic = req.Arguments[0]
}

for _, peer := range n.Floodsub.ListPeers(topic) {
res.Emit(peer.Pretty())
peers := n.Floodsub.ListPeers(topic)
list := &stringList{make([]string, 0, len(peers))}

for _, peer := range peers {
list.Strings = append(list.Strings, peer.Pretty())
}
sort.Strings(list.Strings)
cmds.EmitOnce(res, list)
},
Type: "",
Type: stringList{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.Encoders[cmds.TextNewline],
cmds.Text: cmds.MakeEncoder(stringListEncoder),
},
}

0 comments on commit 958483f

Please sign in to comment.