diff --git a/.github/workflows/autoapprove.yml b/.github/workflows/autoapprove.yml new file mode 100644 index 0000000..ebe28c9 --- /dev/null +++ b/.github/workflows/autoapprove.yml @@ -0,0 +1,20 @@ +name: "autoapprove" + +on: + pull_request_target: + types: [assigned, opened, synchronize, reopened] + +permissions: + pull-requests: write + contents: write + +jobs: + autoapprove: + runs-on: ubuntu-latest + steps: + - name: approve + uses: hmarr/auto-approve-action@v2 + if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]' + id: approve + with: + github-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml new file mode 100644 index 0000000..5ff3f69 --- /dev/null +++ b/.github/workflows/automerge.yml @@ -0,0 +1,21 @@ +name: "automerge" + +on: + pull_request_target: + types: [assigned, opened, synchronize, reopened] + +permissions: + pull-requests: write + contents: write + +jobs: + automerge: + runs-on: ubuntu-latest + if: github.actor == 'vtolstov' + steps: + - name: merge + id: merge + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.TOKEN}} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4182c04..d8002e9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,6 +3,7 @@ on: push: branches: - master + - v3 jobs: test: name: test @@ -13,7 +14,7 @@ jobs: with: go-version: 1.16 - name: checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: cache uses: actions/cache@v2 with: @@ -31,9 +32,9 @@ jobs: runs-on: ubuntu-latest steps: - name: checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: lint - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v3.1.0 continue-on-error: true with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index b50aad2..9b4d428 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -43,7 +43,7 @@ jobs: steps: - name: checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: setup uses: actions/setup-go@v2 with: diff --git a/.github/workflows/dependabot-automerge.yml b/.github/workflows/dependabot-automerge.yml index 26cf8ca..3681028 100644 --- a/.github/workflows/dependabot-automerge.yml +++ b/.github/workflows/dependabot-automerge.yml @@ -1,4 +1,4 @@ -name: "prautomerge" +name: "dependabot-automerge" on: pull_request_target: @@ -9,21 +9,17 @@ permissions: contents: write jobs: - dependabot: + automerge: runs-on: ubuntu-latest - if: ${{ github.actor == 'dependabot[bot]' }} + if: github.actor == 'dependabot[bot]' steps: - name: metadata id: metadata - uses: dependabot/fetch-metadata@v1.2.1 + uses: dependabot/fetch-metadata@v1.3.0 with: github-token: "${{ secrets.TOKEN }}" - - name: approve - run: gh pr review --approve "$PR_URL" - env: - PR_URL: ${{github.event.pull_request.html_url}} - GITHUB_TOKEN: ${{secrets.TOKEN}} - name: merge + id: merge if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}} run: gh pr merge --auto --merge "$PR_URL" env: diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index f807f24..ff10c46 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -3,6 +3,7 @@ on: pull_request: branches: - master + - v3 jobs: test: name: test @@ -13,7 +14,7 @@ jobs: with: go-version: 1.16 - name: checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: cache uses: actions/cache@v2 with: @@ -31,9 +32,9 @@ jobs: runs-on: ubuntu-latest steps: - name: checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: lint - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v3.1.0 continue-on-error: true with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. diff --git a/nats.go b/nats.go index c75a2c4..9e4277e 100644 --- a/nats.go +++ b/nats.go @@ -109,13 +109,15 @@ func (n *natsBroker) setAddrs(addrs []string) []string { } func (n *natsBroker) Connect(ctx context.Context) error { - n.Lock() - defer n.Unlock() - + n.RLock() if n.connected { + n.RUnlock() return nil } + n.RUnlock() + n.Lock() + defer n.Unlock() status := nats.CLOSED if n.conn != nil { status = n.conn.Status() @@ -133,7 +135,7 @@ func (n *natsBroker) Connect(ctx context.Context) error { c, err := opts.Connect() if err != nil { if n.opts.Logger.V(logger.WarnLevel) { - n.opts.Logger.Warnf(n.opts.Context, "Error connecting to broker: %v", err) + n.opts.Logger.Warnf(n.opts.Context, "error connecting to broker: %v", err) } return err @@ -145,12 +147,20 @@ func (n *natsBroker) Connect(ctx context.Context) error { } func (n *natsBroker) Disconnect(ctx context.Context) error { + n.RLock() + if !n.connected { + n.RUnlock() + return nil + } + n.RUnlock() + n.Lock() defer n.Unlock() - // drain the connection if specified if n.drain { - n.conn.Drain() + if err := n.conn.Drain(); err != nil { + return err + } n.closeCh <- nil }