summaryrefslogtreecommitdiff
path: root/plugins/websockets/rpc.go
blob: ef44884a949924543fe4716ee27e524c1273b8d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package websockets

import (
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/pkg/pubsub/message"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	"google.golang.org/protobuf/proto"
)

// rpc collectors struct
type rpc struct {
	plugin *Plugin
	log    logger.Logger
}

// Publish ... msg is a proto decoded payload
// see: pkg/pubsub/message.fbs
func (r *rpc) Publish(in *message.Messages, ok *bool) error {
	const op = errors.Op("broadcast_publish")

	// just return in case of nil message
	if in == nil {
		*ok = true
		return nil
	}

	r.log.Debug("message published", "msg", in.Messages)

	msgLen := len(in.GetMessages())

	for i := 0; i < msgLen; i++ {
		bb, err := proto.Marshal(in.GetMessages()[i])
		if err != nil {
			return errors.E(op, err)
		}

		err = r.plugin.Publish(bb)
		if err != nil {
			*ok = false
			return errors.E(op, err)
		}
	}

	*ok = true
	return nil
}

// PublishAsync ...
// see: pkg/pubsub/message.fbs
func (r *rpc) PublishAsync(in *message.Messages, ok *bool) error {
	const op = errors.Op("publish_async")

	// just return in case of nil message
	if in == nil {
		*ok = true
		return nil
	}

	r.log.Debug("message published", "msg", in.Messages)

	msgLen := len(in.GetMessages())

	for i := 0; i < msgLen; i++ {
		bb, err := proto.Marshal(in.GetMessages()[i])
		if err != nil {
			return errors.E(op, err)
		}

		r.plugin.PublishAsync(bb)
	}

	*ok = true
	return nil
}