diff --git a/.gitea/ISSUE_TEMPLATE/bug_report.md b/.gitea/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..a77fdfd --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,18 @@ +--- +name: Bug report +about: For reporting bugs in micro +title: "[BUG]" +labels: '' +assignees: '' + +--- + +**Describe the bug** + +1. What are you trying to do? +2. What did you expect to happen? +3. What happens instead? + +**How to reproduce the bug:** + +If possible, please include a minimal code snippet here. diff --git a/.gitea/ISSUE_TEMPLATE/feature-request---enhancement.md b/.gitea/ISSUE_TEMPLATE/feature-request---enhancement.md new file mode 100644 index 0000000..c722ea6 --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/feature-request---enhancement.md @@ -0,0 +1,17 @@ +--- +name: Feature request / Enhancement +about: If you have a need not served by micro +title: "[FEATURE]" +labels: '' +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Additional context** +Add any other context or screenshots about the feature request here. \ No newline at end of file diff --git a/.gitea/ISSUE_TEMPLATE/question.md b/.gitea/ISSUE_TEMPLATE/question.md new file mode 100644 index 0000000..3d65c72 --- /dev/null +++ b/.gitea/ISSUE_TEMPLATE/question.md @@ -0,0 +1,8 @@ +--- +name: Question +about: Ask a question about micro +title: '' +labels: '' +assignees: '' + +--- \ No newline at end of file diff --git a/.gitea/PULL_REQUEST_TEMPLATE.md b/.gitea/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..cba3cbc --- /dev/null +++ b/.gitea/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ +## Pull Request template +Please, go through these steps before clicking submit on this PR. + +1. Give a descriptive title to your PR. +2. Provide a description of your changes. +3. Make sure you have some relevant tests. +4. Put `closes #XXXX` in your comment to auto-close the issue that your PR fixes (if applicable). + +**PLEASE REMOVE THIS TEMPLATE BEFORE SUBMITTING** diff --git a/.gitea/autoapprove.yml b/.gitea/autoapprove.yml new file mode 100644 index 0000000..ef43e49 --- /dev/null +++ b/.gitea/autoapprove.yml @@ -0,0 +1,28 @@ +name: "autoapprove" + +on: + pull_request_target: + types: [assigned, opened, synchronize, reopened] + workflow_run: + workflows: ["prbuild"] + types: + - completed + +permissions: + pull-requests: write + contents: write + +jobs: + autoapprove: + runs-on: ubuntu-latest + steps: + - name: approve + run: [ "curl -o tea https://dl.gitea.com/tea/main/tea-main-linux-amd64", + "chmod +x ./tea", + "./tea login add --name unistack --token ${{ secrets.GITHUB_TOKEN }} --url https://git.unistack.org", + "./tea pr --repo ${{ github.event.repository.name }}" + ] + if: github.actor == 'vtolstov' + id: approve + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitea/workflows/job_lint.yml b/.gitea/workflows/job_lint.yml new file mode 100644 index 0000000..d97e747 --- /dev/null +++ b/.gitea/workflows/job_lint.yml @@ -0,0 +1,29 @@ +name: lint + +on: + pull_request: + types: [opened, reopened, synchronize] + branches: + - master + - v3 + - v4 + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: checkout code + uses: actions/checkout@v4 + with: + filter: 'blob:none' + - name: setup go + uses: actions/setup-go@v5 + with: + cache-dependency-path: "**/*.sum" + go-version: 'stable' + - name: setup deps + run: go get -v ./... + - name: run lint + uses: https://github.com/golangci/golangci-lint-action@v6 + with: + version: 'latest' diff --git a/.gitea/workflows/job_test.yml b/.gitea/workflows/job_test.yml new file mode 100644 index 0000000..f68cbca --- /dev/null +++ b/.gitea/workflows/job_test.yml @@ -0,0 +1,34 @@ +name: test + +on: + pull_request: + types: [opened, reopened, synchronize] + branches: + - master + - v3 + - v4 + push: + branches: + - master + - v3 + - v4 + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: checkout code + uses: actions/checkout@v4 + with: + filter: 'blob:none' + - name: setup go + uses: actions/setup-go@v5 + with: + cache-dependency-path: "**/*.sum" + go-version: 'stable' + - name: setup deps + run: go get -v ./... + - name: run test + env: + INTEGRATION_TESTS: yes + run: go test -mod readonly -v ./... diff --git a/.gitea/workflows/job_tests.yml b/.gitea/workflows/job_tests.yml new file mode 100644 index 0000000..e8984f1 --- /dev/null +++ b/.gitea/workflows/job_tests.yml @@ -0,0 +1,53 @@ +name: test + +on: + pull_request: + types: [opened, reopened, synchronize] + branches: + - master + - v3 + - v4 + push: + branches: + - master + - v3 + - v4 + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: checkout code + uses: actions/checkout@v4 + with: + filter: 'blob:none' + - name: checkout tests + uses: actions/checkout@v4 + with: + ref: master + filter: 'blob:none' + repository: unistack-org/micro-tests + path: micro-tests + - name: setup go + uses: actions/setup-go@v5 + with: + cache-dependency-path: "**/*.sum" + go-version: 'stable' + - name: setup go work + env: + GOWORK: /workspace/${{ github.repository_owner }}/go.work + run: | + go work init + go work use . + go work use micro-tests + - name: setup deps + env: + GOWORK: /workspace/${{ github.repository_owner }}/go.work + run: go get -v ./... + - name: run tests + env: + INTEGRATION_TESTS: yes + GOWORK: /workspace/${{ github.repository_owner }}/go.work + run: | + cd micro-tests + go test -mod readonly -v ./... || true diff --git a/go.mod b/go.mod index 645a8d1..530a99f 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module go.unistack.org/micro-broker-redis/v3 go 1.22 require ( - github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2 - github.com/redis/go-redis/v9 v9.6.2 - go.unistack.org/micro/v3 v3.10.97 + github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 + github.com/redis/go-redis/v9 v9.7.0 + go.unistack.org/micro/v3 v3.11.14 ) require ( @@ -13,5 +13,5 @@ require ( github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/google/go-cmp v0.5.9 // indirect go.unistack.org/micro-proto/v3 v3.4.1 // indirect - google.golang.org/protobuf v1.35.1 // indirect + google.golang.org/protobuf v1.35.2 // indirect ) diff --git a/go.sum b/go.sum index 225f4a0..5b83faa 100644 --- a/go.sum +++ b/go.sum @@ -10,11 +10,19 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2 h1:oBlErygFka9FAVfowzW7sRUfB7n31YX0aFzfaOflM3w= github.com/redis/go-redis/extra/rediscmd/v9 v9.6.2/go.mod h1:jYLUE5tC8UsFnpSclhEjZlFLMMtHH4jTfFMnshNWkoo= +github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0 h1:BIx9TNZH/Jsr4l1i7VVxnV0JPiwYj8qyrHyuL0fGZrk= +github.com/redis/go-redis/extra/rediscmd/v9 v9.7.0/go.mod h1:eTg/YQtGYAZD5r3DlGlJptJ45AHA+/G+2NPn30PKzik= github.com/redis/go-redis/v9 v9.6.2 h1:w0uvkRbc9KpgD98zcvo5IrVUsn0lXpRMuhNgiHDJzdk= github.com/redis/go-redis/v9 v9.6.2/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA= +github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E= +github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw= go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= go.unistack.org/micro/v3 v3.10.97 h1:8l7fv+i06/PjPrBBhRC/ZQkWGIOuHPg3jJN0vktYE78= go.unistack.org/micro/v3 v3.10.97/go.mod h1:YzMldzHN9Ei+zy5t/Psu7RUWDZwUfrNYiStSQtTz90g= +go.unistack.org/micro/v3 v3.11.14 h1:3e9T30Ih9cvqZTCD8inG1qsBWRk4x5ZinWuTiDFM4CE= +go.unistack.org/micro/v3 v3.11.14/go.mod h1:k++F5Ej4LIy3XnOW/oj3P7B97wp2t9yLSlqtUzMpatM= google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= +google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= diff --git a/redis.go b/redis.go index c9ff860..34bda25 100644 --- a/redis.go +++ b/redis.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "strings" + "sync/atomic" "time" "github.com/redis/go-redis/v9" @@ -152,9 +153,22 @@ func (s *Subscriber) Unsubscribe(ctx context.Context) error { // Broker implements broker.Broker interface type Broker struct { - opts broker.Options - cli redis.UniversalClient - done chan struct{} + opts broker.Options + cli redis.UniversalClient + done chan struct{} + connected *atomic.Uint32 +} + +func (b *Broker) Live() bool { + return b.connected.Load() == 1 +} + +func (b *Broker) Ready() bool { + return b.connected.Load() == 1 +} + +func (b *Broker) Health() bool { + return b.connected.Load() == 1 } // String returns the name of the broker implementation @@ -243,7 +257,11 @@ func (b *Broker) Subscribe(ctx context.Context, topic string, handler broker.Han return s, nil } -func (b *Broker) configure() error { +func (b *Broker) configure(opts ...broker.Option) error { + if b.connected.Load() == 1 && len(opts) == 0 { + return nil + } + redisOptions := DefaultOptions if b.opts.Context != nil { @@ -260,10 +278,6 @@ func (b *Broker) configure() error { redisOptions.TLSConfig = b.opts.TLSConfig } - if redisOptions == nil && b.cli != nil { - return nil - } - c := redis.NewUniversalClient(redisOptions) setTracing(c, b.opts.Tracer) @@ -274,6 +288,9 @@ func (b *Broker) configure() error { } func (b *Broker) Connect(ctx context.Context) error { + if b.connected.Load() == 1 { + return nil + } var err error if b.cli != nil { err = b.cli.Ping(ctx).Err() @@ -284,15 +301,10 @@ func (b *Broker) Connect(ctx context.Context) error { } func (b *Broker) Init(opts ...broker.Option) error { - for _, o := range opts { - o(&b.opts) - } - - err := b.configure() + err := b.configure(opts...) if err != nil { return err } - return nil } @@ -304,6 +316,9 @@ func (b *Broker) Client() redis.UniversalClient { } func (b *Broker) Disconnect(ctx context.Context) error { + if b.connected.Load() == 0 { + return nil + } var err error select { case <-b.done: @@ -318,5 +333,5 @@ func (b *Broker) Disconnect(ctx context.Context) error { } func NewBroker(opts ...broker.Option) *Broker { - return &Broker{opts: broker.NewOptions(opts...)} + return &Broker{connected: &atomic.Uint32{}, opts: broker.NewOptions(opts...)} }