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

Feature: add multiple publish requests #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ func (p *Pipe) add(cmd Command) error {
return nil
}

type publishRequest struct {
func (p *Pipe) addMany(commands []Command) error {
p.mu.Lock()
defer p.mu.Unlock()
p.commands = append(p.commands, commands...)
return nil
}

type PublishRequest struct {
Channel string `json:"channel"`
Data json.RawMessage `json:"data"`
PublishOptions
Expand All @@ -40,7 +47,7 @@ func (p *Pipe) AddPublish(channel string, data []byte, opts ...PublishOption) er
}
cmd := Command{
Method: "publish",
Params: publishRequest{
Params: PublishRequest{
Channel: channel,
Data: data,
PublishOptions: *options,
Expand All @@ -49,6 +56,19 @@ func (p *Pipe) AddPublish(channel string, data []byte, opts ...PublishOption) er
return p.add(cmd)
}

// AddPublishRequests adds publish commands to client command buffer but not actually
// sends request to server until Pipe will be explicitly sent.
func (p *Pipe) AddPublishRequests(requests []PublishRequest) error {
var commands []Command
for _, request := range requests {
commands = append(commands, Command{
Method: "publish",
Params: request,
})
}
return p.addMany(commands)
}

type broadcastRequest struct {
Channels []string `json:"channels"`
Data json.RawMessage `json:"data"`
Expand Down