-
Notifications
You must be signed in to change notification settings - Fork 11
/
queue.go
218 lines (193 loc) · 7.01 KB
/
queue.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
Copyright 2024 Blnk Finance Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package blnk
import (
"context"
"encoding/json"
"fmt"
"hash/fnv"
"log"
"time"
"github.com/jerry-enebeli/blnk/config"
"github.com/hibiken/asynq"
"github.com/jerry-enebeli/blnk/model"
)
const (
TRANSACTION_QUEUE = "new:transaction"
WEBHOOK_QUEUE = "new:webhoook"
INDEX_QUEUE = "new:index"
EXPIREDINFLIGHT_QUEUE = "new:inflight-expiry"
NumberOfQueues = 20
)
// Queue represents a queue for handling various tasks.
type Queue struct {
Client *asynq.Client
Inspector *asynq.Inspector
}
// TransactionTypePayload represents the payload for a transaction type.
type TransactionTypePayload struct {
Data model.Transaction
}
// NewQueue initializes a new Queue instance with the provided configuration.
//
// Parameters:
// - conf *config.Configuration: The configuration for the queue.
//
// Returns:
// - *Queue: A pointer to the newly created Queue instance.
func NewQueue(conf *config.Configuration) *Queue {
client := asynq.NewClient(asynq.RedisClientOpt{Addr: conf.Redis.Dns})
inspector := asynq.NewInspector(asynq.RedisClientOpt{Addr: conf.Redis.Dns})
return &Queue{
Client: client,
Inspector: inspector,
}
}
// queueInflightExpiry enqueues a task to handle inflight expiry for a transaction.
//
// Parameters:
// - transactionID string: The ID of the transaction.
// - expiresAt time.Time: The expiration time for the inflight status.
//
// Returns:
// - error: An error if the task could not be enqueued.
func (q *Queue) queueInflightExpiry(transactionID string, expiresAt time.Time) error {
IPayload, err := json.Marshal(transactionID)
if err != nil {
log.Fatal(err)
}
taskOptions := []asynq.Option{asynq.TaskID(transactionID), asynq.Queue(EXPIREDINFLIGHT_QUEUE), asynq.ProcessIn(time.Until(expiresAt))}
task := asynq.NewTask(EXPIREDINFLIGHT_QUEUE, IPayload, taskOptions...)
info, err := q.Client.Enqueue(task)
if err != nil {
log.Println(err, info)
return err
}
log.Printf(" [*] Successfully enqueued inflight expiry: %+v", transactionID)
return nil
}
// queueIndexData enqueues a task to index data in a specified collection.
//
// Parameters:
// - id string: The ID of the data to index.
// - collection string: The name of the collection to index the data in.
// - data interface{}: The data to be indexed.
//
// Returns:
// - error: An error if the task could not be enqueued.
func (q *Queue) queueIndexData(id string, collection string, data interface{}) error {
payload := map[string]interface{}{
"collection": collection,
"payload": data,
}
IPayload, err := json.Marshal(payload)
if err != nil {
log.Fatal(err)
}
taskOptions := []asynq.Option{asynq.Queue(INDEX_QUEUE)}
task := asynq.NewTask(INDEX_QUEUE, IPayload, taskOptions...)
info, err := q.Client.Enqueue(task)
if err != nil {
log.Println(err, info)
return err
}
log.Printf(" [*] Successfully enqueued index data: %+v", id)
return nil
}
// Enqueue enqueues a transaction to the Redis queue.
//
// Parameters:
// - ctx context.Context: The context for the operation.
// - transaction *model.Transaction: The transaction to be enqueued.
//
// Returns:
// - error: An error if the transaction could not be enqueued.
func (q *Queue) Enqueue(ctx context.Context, transaction *model.Transaction) error {
ctx, span := tracer.Start(ctx, "Adding Transaction To Redis Queue")
defer span.End()
payload, err := json.Marshal(transaction)
if err != nil {
log.Fatal(err)
}
info, err := q.Client.EnqueueContext(ctx, q.geTask(transaction, payload), asynq.MaxRetry(5))
if err != nil {
log.Println(err, info)
return err
}
log.Printf(" [*] Successfully enqueued transaction: %+v", transaction.Reference)
if !transaction.InflightExpiryDate.IsZero() {
fmt.Println(transaction.InflightExpiryDate)
return q.queueInflightExpiry(transaction.TransactionID, transaction.InflightExpiryDate)
}
return nil
}
// geTask generates a task for a transaction and assigns it to a specific queue based on the balance ID.
// It ensures that transactions are evenly distributed across multiple queues by hashing the balance ID.
// This approach helps to avoid race conditions on a balance by ensuring that all transactions related to the same balance
// are processed serially within the same queue, thereby maintaining accuracy and consistency.
//
// Parameters:
// - transaction *model.Transaction: The transaction for which to generate the task.
// - payload []byte: The payload for the task, typically the serialized transaction data.
//
// Returns:
// - *asynq.Task: The generated task ready to be enqueued.
func (q *Queue) geTask(transaction *model.Transaction, payload []byte) *asynq.Task {
// Hash the balance ID and use modulo to select a queue
queueIndex := hashBalanceID(transaction.Source) % NumberOfQueues
// Queue names are 1-based, so we add 1 to the index
queueName := fmt.Sprintf("%s_%d", TRANSACTION_QUEUE, queueIndex+1)
// Initialize task options with the task ID and the selected queue name
taskOptions := []asynq.Option{asynq.TaskID(transaction.TransactionID), asynq.Queue(queueName)}
// If the transaction is scheduled for a future time, add a processing delay
if !transaction.ScheduledFor.IsZero() {
taskOptions = append(taskOptions, asynq.ProcessIn(time.Until(transaction.ScheduledFor)))
}
// Create and return the new task with the specified options
return asynq.NewTask(queueName, payload, taskOptions...)
}
// hashBalanceID returns a consistent hash value for a string balance ID.
//
// Parameters:
// - balanceID string: The balance ID to hash.
//
// Returns:
// - int: The hash value of the balance ID.
func hashBalanceID(balanceID string) int {
hasher := fnv.New32a()
_, _ = hasher.Write([]byte(balanceID))
return int(hasher.Sum32())
}
// GetTransactionFromQueue retrieves a transaction from the queue by its ID.
//
// Parameters:
// - transactionID string: The ID of the transaction to retrieve.
//
// Returns:
// - *model.Transaction: A pointer to the Transaction model if found.
// - error: An error if the transaction could not be retrieved.
func (q *Queue) GetTransactionFromQueue(transactionID string) (*model.Transaction, error) {
// Iterate over all specific transaction queues
for i := 1; i <= NumberOfQueues; i++ {
queueName := fmt.Sprintf("%s_%d", TRANSACTION_QUEUE, i)
task, err := q.Inspector.GetTaskInfo(queueName, transactionID)
if err == nil && task != nil {
var txn model.Transaction
if err := json.Unmarshal(task.Payload, &txn); err != nil {
return nil, err
}
return &txn, nil
}
}
return nil, nil // Return nil if transaction is not found in any queue
}