-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(MessageQueue/Measuremets): New mq.SetupOptions, Quorum Queue…
… default, Batched Measurement inserts (#124) * chore: remove dead code * refactor mq to use setup funcs * Use QueueProcessors and quorum queues * Fixes * fix: tests * style: Fix golangci lint issues * fix: add logs and error logging
- Loading branch information
Showing
29 changed files
with
684 additions
and
1,192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package mq | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
var ErrMalformed = errors.New("delivery malformed") | ||
|
||
type ( | ||
ProcessorFunc = func(delivery amqp091.Delivery) error | ||
ProcessorFuncBuilder = func() ProcessorFunc | ||
) | ||
|
||
// StartQueueProcessor opens a basic consume channel with a queue and exchange topic binding. | ||
// Multiple workers will be started based on the prefetch count. Each worker will call the ProcessFuncBuilder, | ||
// which allows a closure per worker, ie an instantiation of a variable for that worker. | ||
// The processor function will be called for each message received from the queue. | ||
// In case of an error, the message will be requeued unless the error wraps mq.ErrMalformed | ||
// The processFunc parameter is a builder which will be called for | ||
func StartQueueProcessor(conn *AMQPConnection, queue, exchange, topic string, processFunc ProcessorFuncBuilder) { | ||
var wg sync.WaitGroup | ||
|
||
consume := conn.Consume(queue, WithDefaults(), WithTopicBinding(queue, exchange, topic)) | ||
|
||
for i := 0; i < DefaultPrefetch(); i++ { | ||
wg.Add(1) | ||
go func(id int) { | ||
defer wg.Done() | ||
process := processFunc() | ||
for delivery := range consume { | ||
err := process(delivery) | ||
if err != nil { | ||
fmt.Printf("Error: QueueProcessorFunc failed: %s\n", err.Error()) | ||
// Only requeue if err is not an ErrMalformed and it is not already redelivered | ||
requeue := !errors.Is(err, ErrMalformed) && !delivery.Redelivered | ||
if err := delivery.Nack(false, requeue); err != nil { | ||
fmt.Printf("Error: could not NAck delivery: %s\n", err.Error()) | ||
} | ||
continue | ||
} | ||
|
||
if err := delivery.Ack(false); err != nil { | ||
fmt.Printf("Error: could not Ack delivery: %s\n", err.Error()) | ||
} | ||
} | ||
}(i) | ||
} | ||
|
||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package mq | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/rabbitmq/amqp091-go" | ||
) | ||
|
||
var defaultPrefetchCount int = 50 | ||
|
||
func DefaultPrefetch() int { | ||
return defaultPrefetchCount | ||
} | ||
|
||
type SetupOpts struct { | ||
Queue string | ||
Exchange string | ||
Topic string | ||
} | ||
|
||
type SetupOption func(c *amqp091.Channel) error | ||
|
||
func setupChannel(c *amqp091.Channel, opts []SetupOption) error { | ||
for _, f := range opts { | ||
if err := f(c); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func WithDefaults() SetupOption { | ||
return func(c *amqp091.Channel) error { | ||
return c.Qos(defaultPrefetchCount, 0, false) | ||
} | ||
} | ||
|
||
func WithTopicBinding(queue, exchange, topic string) SetupOption { | ||
return func(c *amqp091.Channel) error { | ||
_, err := c.QueueDeclare(queue, true, false, false, false, amqp091.Table{ | ||
"x-queue-type": "quorum", | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error declaring amqp queue: %w", err) | ||
} | ||
err = c.ExchangeDeclare(exchange, "topic", true, false, false, false, nil) | ||
if err != nil { | ||
return fmt.Errorf("error declaring amqp exchange: %w", err) | ||
} | ||
err = c.QueueBind(queue, topic, exchange, false, nil) | ||
if err != nil { | ||
return fmt.Errorf("error binding amqp queue to exchange: %w", err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func WithExchange(exchange string) SetupOption { | ||
return func(c *amqp091.Channel) error { | ||
err := c.ExchangeDeclare(exchange, "topic", true, false, false, false, nil) | ||
if err != nil { | ||
return fmt.Errorf("error declaring amqp exchange: %w", err) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func init() { | ||
prefetchStr, ok := os.LookupEnv("AMQP_PREFETCH") | ||
if ok { | ||
prefetch, err := strconv.Atoi(prefetchStr) | ||
if err != nil { | ||
log.Fatalf("AMQP_PREFETCH env set but not a number: %s\n", err.Error()) | ||
} | ||
defaultPrefetchCount = prefetch | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.