Skip to content

Commit a800af2

Browse files
authored
feat: initial version goqu (#2)
1 parent 271c9dc commit a800af2

File tree

23 files changed

+1233
-0
lines changed

23 files changed

+1233
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
build:
12+
name: Build and test
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
go-version: ["oldstable", "stable"]
17+
env:
18+
VERBOSE: 1
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
- name: Set up Go
24+
uses: actions/setup-go@v5
25+
with:
26+
go-version: ${{ matrix.go-version }}
27+
- name: Run tests
28+
run: make test

.github/workflows/lint.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Linter
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
build:
12+
name: Build and test
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
go-version: ["oldstable", "stable"]
17+
env:
18+
VERBOSE: 1
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
- name: Set up Go
24+
uses: actions/setup-go@v5
25+
with:
26+
go-version: ${{ matrix.go-version }}
27+
- name: Apply Linter
28+
run: make lint

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919

2020
# Go workspace file
2121
go.work
22+
bin/

.golangci.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
linters-settings:
2+
gocyclo:
3+
min-complexity: 50
4+
dupl:
5+
threshold: 5
6+
goconst:
7+
min-len: 2
8+
min-occurrences: 2
9+
misspell:
10+
locale: US
11+
revive:
12+
confidence: 0.8
13+
lll:
14+
line-length: 160
15+
# tab width in spaces. Default to 1.
16+
tab-width: 1
17+
funlen:
18+
lines: 150
19+
statements: 80
20+
21+
linters:
22+
# please, do not use `enable-all`: it's deprecated and will be removed soon.
23+
# inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint
24+
disable-all: true
25+
enable:
26+
- errcheck
27+
- funlen
28+
- goconst
29+
- gocyclo
30+
- gosec
31+
- gosimple
32+
- govet
33+
- ineffassign
34+
- lll
35+
- misspell
36+
- revive
37+
- staticcheck
38+
- typecheck
39+
- unconvert
40+
- unparam
41+
- unused
42+
43+
# don't enable:
44+
# - gochecknoglobals
45+
# - gocognit
46+
# - godox
47+
# - maligned
48+
# - prealloc
49+
50+
run:
51+
skip-dirs:
52+
# - test/testdata_etc
53+
issues:
54+
exclude-rules:
55+
exclude-files:
56+
- ".*_test\\.go$"

Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Exporting bin folder to the path for makefile
2+
export PATH := $(PWD)/bin:$(PATH)
3+
# Default Shell
4+
export SHELL := bash
5+
# Type of OS: Linux or Darwin.
6+
export OSTYPE := $(shell uname -s | tr A-Z a-z)
7+
export ARCH := $(shell uname -m)
8+
9+
10+
11+
include ./misc/makefile/tools.Makefile
12+
13+
build: test
14+
@go build ./...
15+
16+
install-deps: gotestsum tparse ## Install Development Dependencies (localy).
17+
deps: $(GOTESTSUM) $(TPARSE) ## Checks for Global Development Dependencies.
18+
deps:
19+
@echo "Required Tools Are Available"
20+
21+
TESTS_ARGS := --format testname --jsonfile gotestsum.json.out
22+
TESTS_ARGS += --max-fails 2
23+
TESTS_ARGS += -- ./...
24+
TESTS_ARGS += -test.parallel 2
25+
TESTS_ARGS += -test.count 1
26+
TESTS_ARGS += -test.failfast
27+
TESTS_ARGS += -test.coverprofile coverage.out
28+
TESTS_ARGS += -test.timeout 60s
29+
TESTS_ARGS += -race
30+
run-tests: $(GOTESTSUM)
31+
@ gotestsum $(TESTS_ARGS) -short
32+
33+
test: run-tests $(TPARSE) ## Run Tests & parse details
34+
@cat gotestsum.json.out | $(TPARSE) -all -notests
35+
36+
37+
lint: $(GOLANGCI) ## Runs golangci-lint with predefined configuration
38+
@echo "Applying linter"
39+
golangci-lint version
40+
golangci-lint run -c .golangci.yaml ./...
41+
42+
.PHONY: lint lint-prepare clean build unittest

consumer.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package goqu
2+
3+
import "context"
4+
5+
// Consumer represents an entity that consumes messages from a queue.
6+
type Consumer interface {
7+
// Consume consumes messages from the queue and passes them to the provided handler.
8+
// It takes a context, an InboundMessageHandler, and a map of metadata as parameters.
9+
// It returns an error if there was a problem consuming the messages.
10+
Consume(ctx context.Context, handler InboundMessageHandler, meta map[string]interface{}) (err error)
11+
12+
// Stop stops the consumer from consuming messages.
13+
// It takes a context as a parameter and returns an error if there was a problem stopping the consumer.
14+
Stop(ctx context.Context) (err error)
15+
}
16+
17+
type InboundMessageHandler interface {
18+
HandleMessage(ctx context.Context, m InboundMessage) (err error)
19+
}
20+
21+
type InboundMessageHandlerFunc func(ctx context.Context, m InboundMessage) (err error)
22+
23+
func (mhf InboundMessageHandlerFunc) HandleMessage(ctx context.Context, m InboundMessage) (err error) {
24+
return mhf(ctx, m)
25+
}
26+
27+
type InboundMessageHandlerMiddlewareFunc func(next InboundMessageHandlerFunc) InboundMessageHandlerFunc
28+
29+
type InboundMessage struct {
30+
Message
31+
RetryCount int64 `json:"retryCount"`
32+
Metadata map[string]interface{} `json:"metadata"`
33+
// Ack is used for confirming the message. It will drop the message from the queue.
34+
Ack func(ctx context.Context) (err error) `json:"-"`
35+
// Nack is used for rejecting the message. It will requeue the message to be re-delivered again.
36+
Nack func(ctx context.Context) (err error) `json:"-"`
37+
// MoveToDeadLetterQueue is used for rejecting the message same with Nack, but instead of requeueing the message,
38+
// Read how to configure dead letter queue in each queue provider.
39+
// eg RabbitMQ: https://www.rabbitmq.com/docs/dlx
40+
MoveToDeadLetterQueue func(ctx context.Context) (err error) `json:"-"`
41+
// Requeue is used to put the message back to the tail of the queue after a delay.
42+
Requeue func(ctx context.Context, delayFn DelayFn) (err error) `json:"-"`
43+
}

consumer/option.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package consumer
2+
3+
import "github.com/bxcodec/goqu"
4+
5+
// Option represents the configuration options for the consumer.
6+
type Option struct {
7+
// BatchMessageSize specifies the maximum number of messages to be processed in a single batch.
8+
BatchMessageSize int
9+
// QueueName specifies the name of the queue to consume messages from.
10+
QueueName string
11+
// Middlewares is a list of middleware functions to be applied to the inbound message handler.
12+
Middlewares []goqu.InboundMessageHandlerMiddlewareFunc
13+
}
14+
15+
// OptionFunc is a function type that takes an `opt` parameter of type `*Option`.
16+
// It is used as an option for configuring behavior in the `Option` struct.
17+
type OptionFunc func(opt *Option)
18+
19+
// WithBatchMessageSize sets the batch message size option for the consumer.
20+
// It takes an integer value 'n' and returns an OptionFunc that sets the
21+
// BatchMessageSize field of the Option struct to 'n'.
22+
func WithBatchMessageSize(n int) OptionFunc {
23+
return func(opt *Option) {
24+
opt.BatchMessageSize = n
25+
}
26+
}
27+
28+
// WithQueueName sets the queue name for the consumer option.
29+
func WithQueueName(name string) OptionFunc {
30+
return func(opt *Option) {
31+
opt.QueueName = name
32+
}
33+
}
34+
35+
// WithMiddlewares is an OptionFunc that sets the provided middlewares for the consumer.
36+
// Middlewares are used to process inbound messages before they are handled by the consumer.
37+
// The middlewares are applied in the order they are provided.
38+
func WithMiddlewares(middlewares ...goqu.InboundMessageHandlerMiddlewareFunc) OptionFunc {
39+
return func(opt *Option) {
40+
opt.Middlewares = middlewares
41+
}
42+
}

0 commit comments

Comments
 (0)