Skip to content

Commit

Permalink
Merge pull request #18 from Clarilab/start-consumer-separately
Browse files Browse the repository at this point in the history
feat: V2 - start consumers separately
  • Loading branch information
nicoandrewss authored Jun 19, 2024
2 parents 1114ffa + 6e6fff5 commit 98161b8
Show file tree
Hide file tree
Showing 18 changed files with 915 additions and 631 deletions.
15 changes: 3 additions & 12 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,21 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ">=1.21.0"
go-version: ">=1.22.4"

- name: Vet
run: make vet

# the official golanci-lint-action logs a lot of errors (https://github.com/golangci/golangci-lint-action/issues/135)
# therefore it's replaced with a manual approach
# - name: Lint
# uses: golangci/golangci-lint-action@v3

# there is an currently an issue with "enforce-repeated-arg-type-style".
# we should probably be able the latest version when golangci-lint v1.56.1 is released.
# https://github.com/golangci/golangci-lint/issues/4353

- name: Lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin
golangci-lint run --out-format=github-actions
- name: Vulnerability Check
uses: golang/govulncheck-action@v1
with:
go-package: ./...
go-version-input: ">=1.21.0"
go-version-input: ">=1.22.4"
check-latest: true

- name: Integration Tests
Expand Down
18 changes: 4 additions & 14 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@ linters:
enable-all: true
disable:
- contextcheck # Check whether the function uses a non-inherited context aka context.Background()
- deadcode # deprecated, replaced by unused
- depguard # not needed, does the same as gomodguard
- execinquery # sql linter, no need
- exhaustruct # very annoying, checks if all struct fields are filled
- exhaustivestruct # deprecated, same as exhaustruct
- ginkgolinter # not used libs
- godot # nervig für quasi keinen impact
- gofumpt # we don't use gofumpt
- goheader # unused
- golint # deprecated, replaced by revive
- ifshort # deprecated
- interfacer # deprecated
- lll # should be disabled in CI, as this is cosmetic only
- maligned # deprecated
- nakedret # no need
- nosnakecase # deprecated
- rowserrcheck # disabled because sql
- scopelint # deprecated
- structcheck # deprecated & disabled because of generics
- sqlclosecheck # disabled because of generics
- tagliatelle # requires ID instead of Id in jsonTags, which is annoying for our old structs
- varcheck # deprecated
linters-settings:
cyclop:
max-complexity: 20
Expand Down Expand Up @@ -69,7 +59,7 @@ linters-settings:
replacement: 'a[b:]'
- pattern: 'a[0:b]'
replacement: 'a[:b]'
gomnd:
mnd:
# List of function patterns to exclude from analysis.
# Values always ignored: `time.Date`,
# `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`,
Expand Down Expand Up @@ -134,7 +124,7 @@ linters-settings:
revive:
enable-all-rules: true
rules:
# Provided by gomnd linter
# Provided by mnd linter
- name: add-constant
disabled: true
- name: argument-limit
Expand Down Expand Up @@ -231,14 +221,14 @@ issues:
- funlen
- goconst
- gocyclo
- gomnd
- mnd
- gosec
- noctx
- wrapcheck
output:
sort-results: true
run:
skip-dirs:
exclude-dirs:
- cmd
- config
# tests: false
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ This library includes support for:

Supported Go Versions

This library supports the most recent Go, currently 1.21.1
This library supports the most recent Go, currently 1.22.4

## INSTALL

```bash
go get github.com/Clarilab/clarimq
go get github.com/Clarilab/clarimq/v2
```

## USAGE
Expand Down Expand Up @@ -174,8 +174,27 @@ consumer, err := clarimq.NewConsumer(conn, "my-queue", handler(),
if err != nil {
// handle error
}

err := consumer.Start()
if err != nil {
// handle error
}
```

The consumer can be set up to immediately start consuming messages from the broker by using the **WithConsumerOptionConsumeAfterCreation** option.
The consumer then does not need to be started with the **Start** method. An error will be returned when trying to start an already started/running consumer.
##### Example
```Go
consumer, err := clarimq.NewConsumer(conn, "my-queue", handler(),
clarimq.WithConsumerOptionConsumeAfterCreation(true),
// more options can be passed
)
if err != nil {
// handle error
}
```


The consumer can be used to declare exchanges, queues and queue-bindings:

##### Example
Expand Down
18 changes: 10 additions & 8 deletions binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,23 @@ func defaultBindingOptions() *BindingOptions {
}
}

func declareBindings(channel *amqp.Channel, queueName, exchangeName string, bindings []Binding) error {
func declareBindings(channelExec channelExec, queueName, exchangeName string, bindings []Binding) error {
const errMessage = "failed to declare binding: %w"

for _, binding := range bindings {
if !binding.Declare {
continue
}

if err := channel.QueueBind(
binding.defaultQueueNameOr(queueName),
binding.RoutingKey,
binding.defaultExchangeNameOr(exchangeName),
binding.NoWait,
amqp.Table(binding.Args),
); err != nil {
if err := channelExec(func(c *amqp.Channel) error {
return c.QueueBind(
binding.defaultQueueNameOr(queueName),
binding.RoutingKey,
binding.defaultExchangeNameOr(exchangeName),
binding.NoWait,
amqp.Table(binding.Args),
)
}); err != nil {
return fmt.Errorf(errMessage, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cache
import (
"sync"

"github.com/Clarilab/clarimq"
"github.com/Clarilab/clarimq/v2"
)

// BasicInMemoryCache is a basic in-memory cache implementation of the clarimq.PublishingCache interface.
Expand Down
Loading

0 comments on commit 98161b8

Please sign in to comment.