go-waku/cmd/waku/rpc/relay.go

164 lines
4.0 KiB
Go
Raw Normal View History

2021-11-06 10:49:47 +00:00
package rpc
import (
"fmt"
2021-11-06 10:49:47 +00:00
"net/http"
2021-11-18 14:20:58 +00:00
"sync"
2021-11-06 10:49:47 +00:00
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
"go.uber.org/zap"
2021-11-06 10:49:47 +00:00
)
type RelayService struct {
node *node.WakuNode
2021-11-18 14:20:58 +00:00
log *zap.Logger
2021-11-18 14:20:58 +00:00
messages map[string][]*pb.WakuMessage
cacheCapacity int
2021-11-18 14:20:58 +00:00
messagesMutex sync.RWMutex
2021-11-22 14:48:32 +00:00
runner *runnerService
2021-11-06 10:49:47 +00:00
}
type RelayMessageArgs struct {
Topic string `json:"topic,omitempty"`
Message *RPCWakuMessage `json:"message,omitempty"`
2021-11-06 10:49:47 +00:00
}
type TopicsArgs struct {
Topics []string `json:"topics,omitempty"`
}
2021-11-18 14:20:58 +00:00
type TopicArgs struct {
Topic string `json:"topic,omitempty"`
}
func NewRelayService(node *node.WakuNode, cacheCapacity int, log *zap.Logger) *RelayService {
2021-11-22 14:48:32 +00:00
s := &RelayService{
node: node,
cacheCapacity: cacheCapacity,
log: log.Named("relay"),
messages: make(map[string][]*pb.WakuMessage),
2021-11-18 14:20:58 +00:00
}
2021-11-22 14:48:32 +00:00
s.runner = newRunnerService(node.Broadcaster(), s.addEnvelope)
2022-06-13 18:30:35 +00:00
2021-11-22 14:48:32 +00:00
return s
2021-11-18 14:20:58 +00:00
}
func (r *RelayService) addEnvelope(envelope *protocol.Envelope) {
r.messagesMutex.Lock()
defer r.messagesMutex.Unlock()
if _, ok := r.messages[envelope.PubsubTopic()]; !ok {
return
}
// Keep a specific max number of messages per topic
if len(r.messages[envelope.PubsubTopic()]) >= r.cacheCapacity {
r.messages[envelope.PubsubTopic()] = r.messages[envelope.PubsubTopic()][1:]
}
2021-11-18 14:20:58 +00:00
r.messages[envelope.PubsubTopic()] = append(r.messages[envelope.PubsubTopic()], envelope.Message())
}
func (r *RelayService) Start() {
r.messagesMutex.Lock()
2022-06-13 18:30:35 +00:00
// Node may already be subscribed to some topics when Relay API handlers are installed. Let's add these
for _, topic := range r.node.Relay().Topics() {
r.log.Info("adding topic handler for existing subscription", zap.String("topic", topic))
r.messages[topic] = make([]*pb.WakuMessage, 0)
}
r.messagesMutex.Unlock()
2022-06-13 18:30:35 +00:00
2021-11-22 14:48:32 +00:00
r.runner.Start()
2021-11-18 14:20:58 +00:00
}
func (r *RelayService) Stop() {
2021-11-22 14:48:32 +00:00
r.runner.Stop()
2021-11-18 14:20:58 +00:00
}
2021-11-06 10:49:47 +00:00
func (r *RelayService) PostV1Message(req *http.Request, args *RelayMessageArgs, reply *SuccessReply) error {
var err error
2022-06-13 18:30:35 +00:00
if args.Topic == "" {
_, err = r.node.Relay().Publish(req.Context(), args.Message.toProto())
} else {
_, err = r.node.Relay().PublishToTopic(req.Context(), args.Message.toProto(), args.Topic)
}
2021-11-06 10:49:47 +00:00
if err != nil {
r.log.Error("publishing message", zap.Error(err))
2022-06-14 15:36:34 +00:00
return err
2021-11-06 10:49:47 +00:00
}
2022-06-14 15:36:34 +00:00
*reply = true
2021-11-06 10:49:47 +00:00
return nil
}
func (r *RelayService) PostV1Subscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
ctx := req.Context()
for _, topic := range args.Topics {
var err error
if topic == "" {
2022-06-13 18:30:35 +00:00
var sub *relay.Subscription
sub, err = r.node.Relay().Subscribe(ctx)
2023-05-05 09:49:15 +00:00
sub.Unsubscribe()
} else {
2022-06-13 18:30:35 +00:00
var sub *relay.Subscription
sub, err = r.node.Relay().SubscribeToTopic(ctx, topic)
if err != nil {
r.log.Error("subscribing to topic", zap.String("topic", topic), zap.Error(err))
return err
}
2023-05-05 09:49:15 +00:00
sub.Unsubscribe()
}
2021-11-06 10:49:47 +00:00
if err != nil {
r.log.Error("subscribing to topic", zap.String("topic", topic), zap.Error(err))
2022-06-14 15:36:34 +00:00
return err
2021-11-06 10:49:47 +00:00
}
2022-11-09 14:13:21 +00:00
r.messagesMutex.Lock()
2021-11-18 14:20:58 +00:00
r.messages[topic] = make([]*pb.WakuMessage, 0)
2022-11-09 14:13:21 +00:00
r.messagesMutex.Unlock()
2021-11-06 10:49:47 +00:00
}
2022-06-14 15:36:34 +00:00
*reply = true
2021-11-06 10:49:47 +00:00
return nil
}
func (r *RelayService) DeleteV1Subscription(req *http.Request, args *TopicsArgs, reply *SuccessReply) error {
ctx := req.Context()
for _, topic := range args.Topics {
2021-11-19 16:19:48 +00:00
err := r.node.Relay().Unsubscribe(ctx, topic)
2021-11-06 10:49:47 +00:00
if err != nil {
r.log.Error("unsubscribing from topic", zap.String("topic", topic), zap.Error(err))
2022-06-14 15:36:34 +00:00
return err
2021-11-06 10:49:47 +00:00
}
2021-11-18 14:20:58 +00:00
delete(r.messages, topic)
2021-11-06 10:49:47 +00:00
}
2022-06-14 15:36:34 +00:00
*reply = true
2021-11-06 10:49:47 +00:00
return nil
}
func (r *RelayService) GetV1Messages(req *http.Request, args *TopicArgs, reply *MessagesReply) error {
2021-11-18 14:20:58 +00:00
r.messagesMutex.Lock()
defer r.messagesMutex.Unlock()
if _, ok := r.messages[args.Topic]; !ok {
return fmt.Errorf("topic %s not subscribed", args.Topic)
}
for i := range r.messages[args.Topic] {
*reply = append(*reply, ProtoToRPC(r.messages[args.Topic][i]))
}
2022-06-13 18:30:35 +00:00
2021-11-18 14:20:58 +00:00
r.messages[args.Topic] = make([]*pb.WakuMessage, 0)
2022-06-13 18:30:35 +00:00
2021-11-18 14:20:58 +00:00
return nil
}