-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
52 lines (42 loc) · 1.08 KB
/
message.go
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
package redis_queue
import (
"errors"
"github.com/redis/go-redis/v9"
)
type Message interface {
Payload() string
}
func NewCommonMessage(payload string) *CommonMessage {
return &CommonMessage{payload: payload}
}
func NewSafeMessage(payload string, queue *SafeQueue) *SafeMessage {
return &SafeMessage{payload: payload, queue: queue}
}
// CommonMessage is a simple message
type CommonMessage struct {
payload string
}
func (msg *CommonMessage) Payload() string {
return msg.payload
}
// SafeMessage is a message that can be acked.
// When you receive a message from SafePop, you can ack it.
// If you don't ack it, the message will be popped again after a while.
type SafeMessage struct {
payload string
queue *SafeQueue
}
func (msg *SafeMessage) Payload() string {
return msg.payload
}
func (msg *SafeMessage) Ack() error {
_, err := ack.Run(msg.queue.ctx, msg.queue.rdb, []string{msg.queue.AckName}, msg.payload).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
// Lua returns false, which means failed to ack
return ErrAckFailed
}
return err
}
return nil
}