summaryrefslogtreecommitdiff
path: root/plugins/temporal/workflow/message_queue.go
blob: 50949897dcb44083849db6180c98d011a25cc2dd (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
package workflow

import (
	rrt "github.com/spiral/roadrunner/v2/plugins/temporal/protocol"
	"go.temporal.io/api/common/v1"
	"go.temporal.io/api/failure/v1"
)

type messageQueue struct {
	seqID func() uint64
	queue []rrt.Message
}

func newMessageQueue(sedID func() uint64) *messageQueue {
	return &messageQueue{
		seqID: sedID,
		queue: make([]rrt.Message, 0, 5),
	}
}

func (mq *messageQueue) flush() {
	mq.queue = mq.queue[0:0]
}

func (mq *messageQueue) allocateMessage(
	cmd interface{},
	payloads *common.Payloads,
) (id uint64, msg rrt.Message, err error) {
	msg = rrt.Message{
		ID:       mq.seqID(),
		Command:  cmd,
		Payloads: payloads,
	}

	return msg.ID, msg, nil
}

func (mq *messageQueue) pushCommand(cmd interface{}, payloads *common.Payloads) (id uint64, err error) {
	id, msg, err := mq.allocateMessage(cmd, payloads)
	if err != nil {
		return 0, err
	}

	mq.queue = append(mq.queue, msg)

	return id, nil
}

func (mq *messageQueue) pushResponse(id uint64, payloads *common.Payloads) {
	mq.queue = append(mq.queue, rrt.Message{ID: id, Payloads: payloads})
}

func (mq *messageQueue) pushError(id uint64, failure *failure.Failure) {
	mq.queue = append(mq.queue, rrt.Message{ID: id, Failure: failure})
}