Compare commits
117 Commits
Author | SHA1 | Date | |
---|---|---|---|
ac454693a6 | |||
f4f8793686 | |||
ec4922ad8b | |||
eaea14e5a8 | |||
f6f7139d2f | |||
51e4118dfc | |||
aaf8c43e04 | |||
cfecb4afd0 | |||
ae4ae64694 | |||
2ca4e5c74e | |||
2809ebcaeb | |||
3f04403319 | |||
8edfc11eef | |||
cb5ef75e09 | |||
d5bd105cc6 | |||
1ba02ed8ad | |||
7c5da60556 | |||
07fa36d704 | |||
849bbd7a09 | |||
f461bb6876 | |||
201d22d1c4 | |||
ff6a272594 | |||
1018abe7bd | |||
398c3c81cb | |||
|
786d03b521 | ||
937c9d5720 | |||
|
951fba55fa | ||
|
ec5238ed14 | ||
d8f44a924e | |||
|
ffe9e5d952 | ||
8c362fd6ae | |||
90365a455c | |||
6a218ca7b2 | |||
ba9b88c650 | |||
aedd60ea87 | |||
ea2ac477be | |||
c1fa2f639d | |||
8e3f2c67d7 | |||
e66194695e | |||
894d6f4f20 | |||
d404fa31ab | |||
88777a29ad | |||
23c2903c21 | |||
8fcc23f639 | |||
25dda1f34c | |||
fe66086c40 | |||
7329bc23bc | |||
c240631cdb | |||
|
6a68533824 | ||
058b6354c0 | |||
1f4cf11afe | |||
39177da1d0 | |||
d559db4050 | |||
aa946c469a | |||
9c4d88bb69 | |||
56288f46b1 | |||
81dcef8b28 | |||
ec7a22b2dc | |||
d2ac0c1360 | |||
69dd8c4eea | |||
27a6a923cd | |||
0a395235d6 | |||
23f0ad0f2f | |||
4fcbe0a770 | |||
28c9865121 | |||
697413d829 | |||
8a64e8c5cc | |||
2c8ca8d14f | |||
769ac6322f | |||
52318d68b8 | |||
5c4332ffc4 | |||
3a86d4c0f4 | |||
8bbcc30d04 | |||
221b248b1f | |||
a9b13378f3 | |||
adb5c9bfc9 | |||
|
92bf44e543 | ||
e48ded17ee | |||
|
7753ecd14f | ||
7766fbf312 | |||
|
6916724a8b | ||
e584100c18 | |||
|
63cc0c1cc6 | ||
396e71e9f7 | |||
|
4709eaf769 | ||
366bdea3d0 | |||
|
947fca7cd5 | ||
97c55fa16b | |||
|
c845a00697 | ||
|
dbee3ac120 | ||
|
740926d262 | ||
|
03d78ef2ff | ||
|
90063ef841 | ||
7faf1dfbc8 | |||
|
5a80d798c6 | ||
0323c5fccc | |||
|
80e7f6b19f | ||
9a4cc96640 | |||
|
4a67d63e4e | ||
f69d96756e | |||
|
67dbeb2c57 | ||
328d15311b | |||
|
2899b963c4 | ||
|
78f07ac959 | ||
dc1ebd45be | |||
|
6925f0291d | ||
5d8a72fa86 | |||
|
f7096e71cf | ||
|
ab10ca9658 | ||
|
bb269b5879 | ||
24a6b4045e | |||
|
09b1b526b8 | ||
af9bca00a1 | |||
|
1c2cc611ef | ||
|
d6e18b5163 | ||
|
37d571076f | ||
|
ddba046c77 |
29
.gitea/workflows/job_lint.yml
Normal file
29
.gitea/workflows/job_lint.yml
Normal file
@@ -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'
|
34
.gitea/workflows/job_test.yml
Normal file
34
.gitea/workflows/job_test.yml
Normal file
@@ -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 ./...
|
53
.gitea/workflows/job_tests.yml
Normal file
53
.gitea/workflows/job_tests.yml
Normal file
@@ -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
|
19
.github/dependabot.yml
vendored
19
.github/dependabot.yml
vendored
@@ -1,19 +0,0 @@
|
|||||||
# To get started with Dependabot version updates, you'll need to specify which
|
|
||||||
# package ecosystems to update and where the package manifests are located.
|
|
||||||
# Please see the documentation for all configuration options:
|
|
||||||
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
|
||||||
|
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
|
|
||||||
# Maintain dependencies for GitHub Actions
|
|
||||||
- package-ecosystem: "github-actions"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
||||||
|
|
||||||
# Maintain dependencies for Golang
|
|
||||||
- package-ecosystem: "gomod"
|
|
||||||
directory: "/"
|
|
||||||
schedule:
|
|
||||||
interval: "daily"
|
|
20
.github/workflows/autoapprove.yml
vendored
20
.github/workflows/autoapprove.yml
vendored
@@ -1,20 +0,0 @@
|
|||||||
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 }}
|
|
21
.github/workflows/automerge.yml
vendored
21
.github/workflows/automerge.yml
vendored
@@ -1,21 +0,0 @@
|
|||||||
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}}
|
|
47
.github/workflows/build.yml
vendored
47
.github/workflows/build.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: build
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.2.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.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
78
.github/workflows/codeql-analysis.yml
vendored
78
.github/workflows/codeql-analysis.yml
vendored
@@ -1,78 +0,0 @@
|
|||||||
# For most projects, this workflow file will not need changing; you simply need
|
|
||||||
# to commit it to your repository.
|
|
||||||
#
|
|
||||||
# You may wish to alter this file to override the set of languages analyzed,
|
|
||||||
# or to provide custom queries or build logic.
|
|
||||||
#
|
|
||||||
# ******** NOTE ********
|
|
||||||
# We have attempted to detect the languages in your repository. Please check
|
|
||||||
# the `language` matrix defined below to confirm you have the correct set of
|
|
||||||
# supported CodeQL languages.
|
|
||||||
#
|
|
||||||
name: "codeql"
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_run:
|
|
||||||
workflows: ["prbuild"]
|
|
||||||
types:
|
|
||||||
- completed
|
|
||||||
push:
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
pull_request:
|
|
||||||
# The branches below must be a subset of the branches above
|
|
||||||
branches: [ master, v3 ]
|
|
||||||
schedule:
|
|
||||||
- cron: '34 1 * * 0'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
analyze:
|
|
||||||
name: analyze
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
actions: read
|
|
||||||
contents: read
|
|
||||||
security-events: write
|
|
||||||
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
language: [ 'go' ]
|
|
||||||
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
|
|
||||||
# Learn more:
|
|
||||||
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
|
||||||
- name: init
|
|
||||||
uses: github/codeql-action/init@v2
|
|
||||||
with:
|
|
||||||
languages: ${{ matrix.language }}
|
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
||||||
# By default, queries listed here will override any specified in a config file.
|
|
||||||
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
||||||
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
|
||||||
- name: autobuild
|
|
||||||
uses: github/codeql-action/autobuild@v2
|
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
|
||||||
# 📚 https://git.io/JvXDl
|
|
||||||
|
|
||||||
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
||||||
# and modify them (or add more) to build your code if your project
|
|
||||||
# uses a compiled language
|
|
||||||
|
|
||||||
#- run: |
|
|
||||||
# make bootstrap
|
|
||||||
# make release
|
|
||||||
|
|
||||||
- name: analyze
|
|
||||||
uses: github/codeql-action/analyze@v2
|
|
27
.github/workflows/dependabot-automerge.yml
vendored
27
.github/workflows/dependabot-automerge.yml
vendored
@@ -1,27 +0,0 @@
|
|||||||
name: "dependabot-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 == 'dependabot[bot]'
|
|
||||||
steps:
|
|
||||||
- name: metadata
|
|
||||||
id: metadata
|
|
||||||
uses: dependabot/fetch-metadata@v1.3.4
|
|
||||||
with:
|
|
||||||
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:
|
|
||||||
PR_URL: ${{github.event.pull_request.html_url}}
|
|
||||||
GITHUB_TOKEN: ${{secrets.TOKEN}}
|
|
47
.github/workflows/pr.yml
vendored
47
.github/workflows/pr.yml
vendored
@@ -1,47 +0,0 @@
|
|||||||
name: prbuild
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
- v3
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
name: test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: setup
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.17
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: cache
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: ${{ runner.os }}-go-
|
|
||||||
- name: deps
|
|
||||||
run: go get -v -t -d ./...
|
|
||||||
- name: test
|
|
||||||
env:
|
|
||||||
INTEGRATION_TESTS: yes
|
|
||||||
run: go test -mod readonly -v ./...
|
|
||||||
lint:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- name: lint
|
|
||||||
uses: golangci/golangci-lint-action@v3.2.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.
|
|
||||||
version: v1.30
|
|
||||||
# Optional: working directory, useful for monorepos
|
|
||||||
# working-directory: somedir
|
|
||||||
# Optional: golangci-lint command line arguments.
|
|
||||||
# args: --issues-exit-code=0
|
|
||||||
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
|
||||||
# only-new-issues: true
|
|
6
.gitignore
vendored
6
.gitignore
vendored
@@ -13,3 +13,9 @@
|
|||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
# vendor/
|
||||||
|
|
||||||
|
# General
|
||||||
|
.DS_Store
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
bin/
|
||||||
|
21
.golangci.yml
Normal file
21
.golangci.yml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
run:
|
||||||
|
concurrency: 8
|
||||||
|
deadline: 5m
|
||||||
|
issues-exit-code: 1
|
||||||
|
tests: true
|
||||||
|
|
||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- staticcheck
|
||||||
|
- unused
|
||||||
|
- gosimple
|
||||||
|
- govet
|
||||||
|
- prealloc
|
||||||
|
- unconvert
|
||||||
|
- nakedret
|
||||||
|
|
||||||
|
linters-settings:
|
||||||
|
govet:
|
||||||
|
check-all: true
|
||||||
|
enable:
|
||||||
|
- fieldalignment
|
14
README.md
14
README.md
@@ -1,9 +1,9 @@
|
|||||||
# micro-broker-kgo
|
# broker-kgo
|
||||||
yet another micro kafka broker alternative
|
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
* dont always append options from context on Init and New
|
|
||||||
* add SubscriberOptions(...kgo.Opt)
|
1) экспортируем текущий оффсет для каждой партиции в группе
|
||||||
* add ServerSubscribeOptions(...kgo.Opt)
|
2) экспортируем лаг для группы
|
||||||
* check PublisherOptions(...kgo.Opt)
|
3) мониторим
|
||||||
* check ClientPublisherOptions(...kgo.Opt)
|
1) если есть лаг больше нуля
|
||||||
|
2) если дельта оффсета за нужное нам время не
|
69
broker.go
Normal file
69
broker.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package kgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
|
"go.unistack.org/micro/v4/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type hookEvent struct {
|
||||||
|
log logger.Logger
|
||||||
|
fatalOnError bool
|
||||||
|
connected *atomic.Uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ kgo.HookBrokerConnect = &hookEvent{}
|
||||||
|
_ kgo.HookBrokerDisconnect = &hookEvent{}
|
||||||
|
_ kgo.HookBrokerRead = &hookEvent{}
|
||||||
|
_ kgo.HookBrokerWrite = &hookEvent{}
|
||||||
|
_ kgo.HookGroupManageError = &hookEvent{}
|
||||||
|
_ kgo.HookProduceRecordUnbuffered = &hookEvent{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (m *hookEvent) OnGroupManageError(err error) {
|
||||||
|
if err != nil {
|
||||||
|
m.connected.Store(0)
|
||||||
|
if m.fatalOnError {
|
||||||
|
m.log.Fatal(context.TODO(), "kgo.OnGroupManageError", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *hookEvent) OnBrokerConnect(_ kgo.BrokerMetadata, _ time.Duration, _ net.Conn, err error) {
|
||||||
|
if err != nil {
|
||||||
|
m.connected.Store(0)
|
||||||
|
if m.fatalOnError {
|
||||||
|
m.log.Fatal(context.TODO(), "kgo.OnBrokerConnect", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *hookEvent) OnBrokerDisconnect(_ kgo.BrokerMetadata, _ net.Conn) {
|
||||||
|
m.connected.Store(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *hookEvent) OnBrokerWrite(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
|
||||||
|
if err != nil {
|
||||||
|
m.connected.Store(0)
|
||||||
|
if m.fatalOnError {
|
||||||
|
m.log.Fatal(context.TODO(), "kgo.OnBrokerWrite", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *hookEvent) OnBrokerRead(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
|
||||||
|
if err != nil {
|
||||||
|
m.connected.Store(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *hookEvent) OnProduceRecordUnbuffered(_ *kgo.Record, err error) {
|
||||||
|
if err != nil {
|
||||||
|
m.connected.Store(0)
|
||||||
|
}
|
||||||
|
}
|
93
carrier.go
Normal file
93
carrier.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package kgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
|
"go.unistack.org/micro/v4/metadata"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RecordCarrier injects and extracts traces from a kgo.Record.
|
||||||
|
//
|
||||||
|
// This type exists to satisfy the otel/propagation.TextMapCarrier interface.
|
||||||
|
type RecordCarrier struct {
|
||||||
|
record *kgo.Record
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRecordCarrier creates a new RecordCarrier.
|
||||||
|
func NewRecordCarrier(record *kgo.Record) RecordCarrier {
|
||||||
|
return RecordCarrier{record: record}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get retrieves a single value for a given key if it exists.
|
||||||
|
func (c RecordCarrier) Get(key string) string {
|
||||||
|
for _, h := range c.record.Headers {
|
||||||
|
if h.Key == key {
|
||||||
|
return string(h.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set sets a header.
|
||||||
|
func (c RecordCarrier) Set(key, val string) {
|
||||||
|
// Check if key already exists.
|
||||||
|
for i, h := range c.record.Headers {
|
||||||
|
if h.Key == key {
|
||||||
|
// Key exist, update the value.
|
||||||
|
c.record.Headers[i].Value = []byte(val)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Key does not exist, append new header.
|
||||||
|
c.record.Headers = append(c.record.Headers, kgo.RecordHeader{
|
||||||
|
Key: key,
|
||||||
|
Value: []byte(val),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keys returns a slice of all key identifiers in the carrier.
|
||||||
|
func (c RecordCarrier) Keys() []string {
|
||||||
|
out := make([]string, len(c.record.Headers))
|
||||||
|
for i, h := range c.record.Headers {
|
||||||
|
out[i] = h.Key
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func setHeaders(r *kgo.Record, md metadata.Metadata, exclude ...string) {
|
||||||
|
seen := make(map[string]struct{})
|
||||||
|
|
||||||
|
loop:
|
||||||
|
for k, v := range md {
|
||||||
|
k = http.CanonicalHeaderKey(k)
|
||||||
|
|
||||||
|
if _, ok := seen[k]; ok {
|
||||||
|
continue loop
|
||||||
|
}
|
||||||
|
|
||||||
|
if slices.ContainsFunc(exclude, func(s string) bool {
|
||||||
|
return strings.EqualFold(s, k)
|
||||||
|
}) {
|
||||||
|
continue loop
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(r.Headers); i++ {
|
||||||
|
if strings.EqualFold(r.Headers[i].Key, k) {
|
||||||
|
// Key exist, update the value.
|
||||||
|
r.Headers[i].Value = []byte(strings.Join(v, ","))
|
||||||
|
continue loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Key does not exist, append new header.
|
||||||
|
r.Headers = append(r.Headers, kgo.RecordHeader{
|
||||||
|
Key: k,
|
||||||
|
Value: []byte(strings.Join(v, ",")),
|
||||||
|
})
|
||||||
|
|
||||||
|
seen[k] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
29
go.mod
29
go.mod
@@ -1,19 +1,24 @@
|
|||||||
module go.unistack.org/micro-broker-kgo/v3
|
module go.unistack.org/micro-broker-kgo/v4
|
||||||
|
|
||||||
go 1.17
|
go 1.23.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/twmb/franz-go v1.8.0
|
github.com/twmb/franz-go v1.18.1
|
||||||
github.com/twmb/franz-go/pkg/kmsg v1.2.0
|
github.com/twmb/franz-go/pkg/kadm v1.15.0
|
||||||
go.unistack.org/micro/v3 v3.9.11
|
github.com/twmb/franz-go/pkg/kmsg v1.9.0
|
||||||
|
go.opentelemetry.io/otel v1.34.0
|
||||||
|
go.unistack.org/micro/v4 v4.1.4
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/ash3in/uuidv8 v1.2.0 // indirect
|
||||||
github.com/google/gnostic v0.6.8 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/klauspost/compress v1.15.9 // indirect
|
github.com/klauspost/compress v1.18.0 // indirect
|
||||||
github.com/pierrec/lz4/v4 v4.1.15 // indirect
|
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||||
go.unistack.org/micro-proto/v3 v3.2.7 // indirect
|
github.com/pierrec/lz4/v4 v4.1.22 // indirect
|
||||||
google.golang.org/protobuf v1.28.0 // indirect
|
github.com/spf13/cast v1.7.1 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.0 // indirect
|
go.unistack.org/micro-proto/v4 v4.1.0 // indirect
|
||||||
|
golang.org/x/crypto v0.36.0 // indirect
|
||||||
|
google.golang.org/protobuf v1.36.5 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
216
go.sum
216
go.sum
@@ -1,171 +1,51 @@
|
|||||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
||||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
||||||
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||||
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||||
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
|
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
|
||||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
|
||||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
|
||||||
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
|
|
||||||
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
|
|
||||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
|
||||||
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=
|
|
||||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
|
||||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
|
||||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
|
||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
|
||||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
|
||||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
|
||||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
|
||||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
|
||||||
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
|
||||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
|
||||||
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
|
||||||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
|
||||||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
|
||||||
github.com/google/gnostic v0.6.6/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
|
||||||
github.com/google/gnostic v0.6.8 h1:bT56GPYBWh1tvBuBEd94qcS3+60b+y0HQur0ITkGuCk=
|
|
||||||
github.com/google/gnostic v0.6.8/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
|
||||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
|
||||||
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
|
|
||||||
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
|
|
||||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
|
||||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
|
|
||||||
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
|
|
||||||
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
|
|
||||||
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
|
|
||||||
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
|
||||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
|
||||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
|
||||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
|
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
||||||
github.com/pierrec/lz4/v4 v4.1.15 h1:MO0/ucJhngq7299dKLwIMtgTfbkoSPF6AoMYDd8Q4q0=
|
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
||||||
github.com/pierrec/lz4/v4 v4.1.15/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
|
||||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/silas/dag v0.0.0-20211117232152-9d50aa809f35/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I=
|
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||||
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||||
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
|
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/twmb/franz-go v1.8.0 h1:58lABTO3YK179YmF+z/3EqL17ZbqhrmEDEdVHcd5tdA=
|
github.com/twmb/franz-go v1.18.1 h1:D75xxCDyvTqBSiImFx2lkPduE39jz1vaD7+FNc+vMkc=
|
||||||
github.com/twmb/franz-go v1.8.0/go.mod h1:PMze0jNfNghhih2XHbkmTFykbMF5sJqmNJB31DOOzro=
|
github.com/twmb/franz-go v1.18.1/go.mod h1:Uzo77TarcLTUZeLuGq+9lNpSkfZI+JErv7YJhlDjs9M=
|
||||||
github.com/twmb/franz-go/pkg/kmsg v1.2.0 h1:jYWh2qFw5lDbNv5Gvu/sMKagzICxuA5L6m1W2Oe7XUo=
|
github.com/twmb/franz-go/pkg/kadm v1.15.0 h1:Yo3NAPfcsx3Gg9/hdhq4vmwO77TqRRkvpUcGWzjworc=
|
||||||
github.com/twmb/franz-go/pkg/kmsg v1.2.0/go.mod h1:SxG/xJKhgPu25SamAq0rrucfp7lbzCpEXOC+vH/ELrY=
|
github.com/twmb/franz-go/pkg/kadm v1.15.0/go.mod h1:MUdcUtnf9ph4SFBLLA/XxE29rvLhWYLM9Ygb8dfSCvw=
|
||||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
github.com/twmb/franz-go/pkg/kmsg v1.9.0 h1:JojYUph2TKAau6SBtErXpXGC7E3gg4vGZMv9xFU/B6M=
|
||||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
github.com/twmb/franz-go/pkg/kmsg v1.9.0/go.mod h1:CMbfazviCyY6HM0SXuG5t9vOwYDHRCSrJJyBAe5paqg=
|
||||||
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
go.opentelemetry.io/otel v1.34.0 h1:zRLXxLCgL1WyKsPVrgbSdMN4c0FMkDAskSTQP+0hdUY=
|
||||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
go.opentelemetry.io/otel v1.34.0/go.mod h1:OWFPOQ+h4G8xpyjgqo4SxJYdDQ/qmRH+wivy7zzx9oI=
|
||||||
go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7TlsxjusWs4=
|
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
|
||||||
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
|
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||||
go.unistack.org/micro/v3 v3.9.11 h1:dsZVss3nvfByL1ZDJNnUVQB1N8w6qn4pr9vIkeBiii8=
|
go.unistack.org/micro/v4 v4.1.4 h1:gyjVPP9g7UAFZlhEbla9Ih/BeKJfBLAbN/NvAhJCpKU=
|
||||||
go.unistack.org/micro/v3 v3.9.11/go.mod h1:LrBm9Fsf4MbY8DlUbdwRTYJosMkN8wrtOQHoYBlMkz8=
|
go.unistack.org/micro/v4 v4.1.4/go.mod h1:lr3oYED8Ay1vjK68QqRw30QOtdk/ffpZqMFDasOUhKw=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
|
||||||
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
|
||||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
|
||||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
|
||||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
|
||||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
|
||||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
|
||||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
|
||||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
|
||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
|
||||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
|
||||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
|
||||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
|
||||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
|
||||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
|
||||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
|
||||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
|
||||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
|
||||||
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
|
||||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
|
||||||
google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc=
|
|
||||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
|
||||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
|
||||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
|
||||||
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
|
||||||
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
|
|
||||||
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
|
|
||||||
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
|
|
||||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
|
||||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
|
||||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
|
||||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
|
||||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
|
||||||
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
|
||||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
|
||||||
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
|
||||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
|
||||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
|
||||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
|
||||||
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
|
|
||||||
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
|
||||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
|
||||||
|
715
kgo.go
715
kgo.go
@@ -1,204 +1,292 @@
|
|||||||
// Package kgo provides a kafka broker using kgo
|
// Package kgo provides a kafka broker using kgo
|
||||||
package kgo // import "go.unistack.org/micro-broker-kgo/v3"
|
package kgo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand/v2"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/twmb/franz-go/pkg/kerr"
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
kgo "github.com/twmb/franz-go/pkg/kgo"
|
|
||||||
"github.com/twmb/franz-go/pkg/kmsg"
|
"github.com/twmb/franz-go/pkg/kmsg"
|
||||||
"github.com/twmb/franz-go/pkg/kversion"
|
"go.unistack.org/micro/v4/broker"
|
||||||
"go.unistack.org/micro/v3/broker"
|
"go.unistack.org/micro/v4/codec"
|
||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v4/logger"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"go.unistack.org/micro/v4/metadata"
|
||||||
"go.unistack.org/micro/v3/util/id"
|
"go.unistack.org/micro/v4/options"
|
||||||
mrand "go.unistack.org/micro/v3/util/rand"
|
"go.unistack.org/micro/v4/semconv"
|
||||||
|
"go.unistack.org/micro/v4/tracer"
|
||||||
|
"go.unistack.org/micro/v4/util/id"
|
||||||
|
mrand "go.unistack.org/micro/v4/util/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ broker.Broker = &kBroker{}
|
var _ broker.Broker = (*Broker)(nil)
|
||||||
|
|
||||||
|
var messagePool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return &kgoMessage{}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrLostMessage = errors.New("message not marked for offsets commit and will be lost in next iteration")
|
||||||
|
|
||||||
|
var DefaultRetryBackoffFn = func() func(int) time.Duration {
|
||||||
|
var rngMu sync.Mutex
|
||||||
|
return func(fails int) time.Duration {
|
||||||
|
const (
|
||||||
|
min = 100 * time.Millisecond
|
||||||
|
max = time.Second
|
||||||
|
)
|
||||||
|
if fails <= 0 {
|
||||||
|
return min
|
||||||
|
}
|
||||||
|
if fails > 10 {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
|
||||||
|
backoff := min * time.Duration(1<<(fails-1))
|
||||||
|
|
||||||
|
rngMu.Lock()
|
||||||
|
jitter := 0.8 + 0.4*rand.Float64()
|
||||||
|
rngMu.Unlock()
|
||||||
|
|
||||||
|
backoff = time.Duration(float64(backoff) * jitter)
|
||||||
|
|
||||||
|
if backoff > max {
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
return backoff
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
type Broker struct {
|
||||||
|
funcPublish broker.FuncPublish
|
||||||
|
funcSubscribe broker.FuncSubscribe
|
||||||
|
c *kgo.Client
|
||||||
|
connected *atomic.Uint32
|
||||||
|
|
||||||
|
kopts []kgo.Opt
|
||||||
|
subs []*Subscriber
|
||||||
|
|
||||||
type kBroker struct {
|
|
||||||
writer *kgo.Client // used only to push messages
|
|
||||||
kopts []kgo.Opt
|
|
||||||
connected bool
|
|
||||||
init bool
|
|
||||||
sync.RWMutex
|
|
||||||
opts broker.Options
|
opts broker.Options
|
||||||
subs []*subscriber
|
|
||||||
}
|
|
||||||
|
|
||||||
type subscriber struct {
|
|
||||||
reader *kgo.Client // used only to pull messages
|
|
||||||
topic string
|
|
||||||
opts broker.SubscribeOptions
|
|
||||||
kopts broker.Options
|
|
||||||
handler broker.Handler
|
|
||||||
batchhandler broker.BatchHandler
|
|
||||||
closed bool
|
|
||||||
done chan struct{}
|
|
||||||
consumers map[string]map[int32]worker
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
|
init bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type publication struct {
|
func (r *Broker) Live() bool {
|
||||||
topic string
|
return r.connected.Load() == 1
|
||||||
err error
|
|
||||||
sync.RWMutex
|
|
||||||
msg *broker.Message
|
|
||||||
ack bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *publication) Topic() string {
|
func (r *Broker) Ready() bool {
|
||||||
return p.topic
|
return r.connected.Load() == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *publication) Message() *broker.Message {
|
func (r *Broker) Health() bool {
|
||||||
return p.msg
|
return r.connected.Load() == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *publication) Ack() error {
|
func (k *Broker) Address() string {
|
||||||
p.ack = true
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *publication) Error() error {
|
|
||||||
return p.err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *publication) SetError(err error) {
|
|
||||||
p.err = err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) Options() broker.SubscribeOptions {
|
|
||||||
return s.opts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) Topic() string {
|
|
||||||
return s.topic
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) Unsubscribe(ctx context.Context) error {
|
|
||||||
if s.closed {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return ctx.Err()
|
|
||||||
default:
|
|
||||||
close(s.done)
|
|
||||||
s.closed = true
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *kBroker) Address() string {
|
|
||||||
return strings.Join(k.opts.Addrs, ",")
|
return strings.Join(k.opts.Addrs, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) Name() string {
|
func (k *Broker) Name() string {
|
||||||
return k.opts.Name
|
return k.opts.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) Connect(ctx context.Context) error {
|
func (k *Broker) Client() *kgo.Client {
|
||||||
k.RLock()
|
return k.c
|
||||||
if k.connected {
|
}
|
||||||
k.RUnlock()
|
|
||||||
|
type kgoMessage struct {
|
||||||
|
c codec.Codec
|
||||||
|
topic string
|
||||||
|
ctx context.Context
|
||||||
|
body []byte
|
||||||
|
hdr metadata.Metadata
|
||||||
|
opts broker.PublishOptions
|
||||||
|
ack bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *kgoMessage) Ack() error {
|
||||||
|
m.ack = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *kgoMessage) Body() []byte {
|
||||||
|
return m.body
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *kgoMessage) Header() metadata.Metadata {
|
||||||
|
return m.hdr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *kgoMessage) Context() context.Context {
|
||||||
|
return m.ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *kgoMessage) Topic() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *kgoMessage) Unmarshal(dst interface{}, opts ...codec.Option) error {
|
||||||
|
return m.c.Unmarshal(m.body, dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Broker) newCodec(ct string) (codec.Codec, error) {
|
||||||
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
|
ct = ct[:idx]
|
||||||
|
}
|
||||||
|
b.RLock()
|
||||||
|
c, ok := b.opts.Codecs[ct]
|
||||||
|
b.RUnlock()
|
||||||
|
if ok {
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
return nil, codec.ErrUnknownContentType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Broker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...broker.PublishOption) (broker.Message, error) {
|
||||||
|
options := broker.NewPublishOptions(opts...)
|
||||||
|
m := &kgoMessage{ctx: ctx, hdr: hdr, opts: options}
|
||||||
|
c, err := b.newCodec(m.opts.ContentType)
|
||||||
|
if err == nil {
|
||||||
|
m.body, err = c.Marshal(body)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *Broker) connect(ctx context.Context, opts ...kgo.Opt) (*kgo.Client, *hookTracer, error) {
|
||||||
|
var c *kgo.Client
|
||||||
|
var err error
|
||||||
|
|
||||||
|
sp, _ := tracer.SpanFromContext(ctx)
|
||||||
|
|
||||||
|
clientID := "kgo"
|
||||||
|
group := ""
|
||||||
|
if k.opts.Context != nil {
|
||||||
|
if id, ok := k.opts.Context.Value(clientIDKey{}).(string); ok {
|
||||||
|
clientID = id
|
||||||
|
}
|
||||||
|
if id, ok := k.opts.Context.Value(groupKey{}).(string); ok {
|
||||||
|
group = id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var fatalOnError bool
|
||||||
|
if k.opts.Context != nil {
|
||||||
|
if v, ok := k.opts.Context.Value(fatalOnErrorKey{}).(bool); ok && v {
|
||||||
|
fatalOnError = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
htracer := &hookTracer{group: group, clientID: clientID, tracer: k.opts.Tracer}
|
||||||
|
opts = append(opts,
|
||||||
|
kgo.WithHooks(&hookMeter{meter: k.opts.Meter}),
|
||||||
|
kgo.WithHooks(htracer),
|
||||||
|
kgo.WithHooks(&hookEvent{log: k.opts.Logger, fatalOnError: fatalOnError, connected: k.connected}),
|
||||||
|
)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
if sp != nil {
|
||||||
|
sp.SetStatus(tracer.SpanStatusError, ctx.Err().Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, nil, ctx.Err()
|
||||||
|
default:
|
||||||
|
c, err = kgo.NewClient(opts...)
|
||||||
|
if err == nil {
|
||||||
|
err = c.Ping(ctx) // check connectivity to cluster
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
if sp != nil {
|
||||||
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
}
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
k.connected.Store(1)
|
||||||
|
return c, htracer, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *Broker) Connect(ctx context.Context) error {
|
||||||
|
if k.connected.Load() == 1 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
k.RUnlock()
|
|
||||||
|
|
||||||
nctx := k.opts.Context
|
nctx := k.opts.Context
|
||||||
if ctx != nil {
|
if ctx != nil {
|
||||||
nctx = ctx
|
nctx = ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
kaddrs := k.opts.Addrs
|
c, _, err := k.connect(nctx, k.kopts...)
|
||||||
|
if err != nil {
|
||||||
// shuffle addrs
|
return err
|
||||||
var rng mrand.Rand
|
|
||||||
rng.Shuffle(len(kaddrs), func(i, j int) {
|
|
||||||
kaddrs[i], kaddrs[j] = kaddrs[j], kaddrs[i]
|
|
||||||
})
|
|
||||||
|
|
||||||
kopts := append(k.kopts, kgo.SeedBrokers(kaddrs...))
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-nctx.Done():
|
|
||||||
return nctx.Err()
|
|
||||||
default:
|
|
||||||
c, err := kgo.NewClient(kopts...)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Request versions in order to guess Kafka Cluster version
|
|
||||||
versionsReq := kmsg.NewApiVersionsRequest()
|
|
||||||
versionsRes, err := versionsReq.RequestWith(ctx, c)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to request api versions: %w", err)
|
|
||||||
}
|
|
||||||
err = kerr.ErrorForCode(versionsRes.ErrorCode)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to request api versions. Inner kafka error: %w", err)
|
|
||||||
}
|
|
||||||
versions := kversion.FromApiVersionsResponse(versionsRes)
|
|
||||||
|
|
||||||
if k.opts.Logger.V(logger.InfoLevel) {
|
|
||||||
logger.Infof(ctx, "[kgo] connected to to kafka cluster version %v", versions.VersionGuess())
|
|
||||||
}
|
|
||||||
|
|
||||||
k.Lock()
|
|
||||||
k.connected = true
|
|
||||||
k.writer = c
|
|
||||||
k.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
k.Lock()
|
||||||
|
k.c = c
|
||||||
|
k.connected.Store(1)
|
||||||
|
k.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) Disconnect(ctx context.Context) error {
|
func (k *Broker) Disconnect(ctx context.Context) error {
|
||||||
k.RLock()
|
if k.connected.Load() == 0 {
|
||||||
if !k.connected {
|
|
||||||
k.RUnlock()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
k.RUnlock()
|
|
||||||
|
|
||||||
k.Lock()
|
|
||||||
defer k.Unlock()
|
|
||||||
|
|
||||||
nctx := k.opts.Context
|
nctx := k.opts.Context
|
||||||
if ctx != nil {
|
if ctx != nil {
|
||||||
nctx = ctx
|
nctx = ctx
|
||||||
}
|
}
|
||||||
|
var span tracer.Span
|
||||||
|
ctx, span = k.opts.Tracer.Start(ctx, "Disconnect")
|
||||||
|
defer span.Finish()
|
||||||
|
|
||||||
|
k.Lock()
|
||||||
|
defer k.Unlock()
|
||||||
select {
|
select {
|
||||||
case <-nctx.Done():
|
case <-nctx.Done():
|
||||||
return nctx.Err()
|
return nctx.Err()
|
||||||
default:
|
default:
|
||||||
for _, sub := range k.subs {
|
for _, sub := range k.subs {
|
||||||
|
if sub.closed {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if err := sub.Unsubscribe(ctx); err != nil {
|
if err := sub.Unsubscribe(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
k.writer.Close()
|
if k.c != nil {
|
||||||
|
k.c.CloseAllowingRebalance()
|
||||||
|
// k.c.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
k.connected = false
|
k.connected.Store(0)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) Init(opts ...broker.Option) error {
|
func (k *Broker) Init(opts ...broker.Option) error {
|
||||||
k.Lock()
|
k.Lock()
|
||||||
defer k.Unlock()
|
defer k.Unlock()
|
||||||
|
|
||||||
if len(opts) == 0 && k.init {
|
if len(opts) == 0 && k.init {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&k.opts)
|
o(&k.opts)
|
||||||
}
|
}
|
||||||
@@ -222,68 +310,109 @@ func (k *kBroker) Init(opts ...broker.Option) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// kgo.RecordPartitioner(),
|
k.funcPublish = k.fnPublish
|
||||||
|
k.funcSubscribe = k.fnSubscribe
|
||||||
|
|
||||||
|
k.opts.Hooks.EachPrev(func(hook options.Hook) {
|
||||||
|
switch h := hook.(type) {
|
||||||
|
case broker.HookPublish:
|
||||||
|
k.funcPublish = h(k.funcPublish)
|
||||||
|
case broker.HookSubscribe:
|
||||||
|
k.funcSubscribe = h(k.funcSubscribe)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
k.init = true
|
k.init = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) Options() broker.Options {
|
func (k *Broker) Options() broker.Options {
|
||||||
return k.opts
|
return k.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) BatchPublish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
func (b *Broker) Publish(ctx context.Context, topic string, messages ...broker.Message) error {
|
||||||
return k.publish(ctx, msgs, opts...)
|
return b.funcPublish(ctx, topic, messages...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) Publish(ctx context.Context, topic string, msg *broker.Message, opts ...broker.PublishOption) error {
|
func (b *Broker) fnPublish(ctx context.Context, topic string, messages ...broker.Message) error {
|
||||||
msg.Header.Set(metadata.HeaderTopic, topic)
|
return b.publish(ctx, topic, messages...)
|
||||||
return k.publish(ctx, []*broker.Message{msg}, opts...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kBroker) publish(ctx context.Context, msgs []*broker.Message, opts ...broker.PublishOption) error {
|
func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error {
|
||||||
k.RLock()
|
if b.connected.Load() == 0 {
|
||||||
if !k.connected {
|
c, _, err := b.connect(ctx, b.kopts...)
|
||||||
k.RUnlock()
|
if err != nil {
|
||||||
return broker.ErrNotConnected
|
return err
|
||||||
|
}
|
||||||
|
b.Lock()
|
||||||
|
b.c = c
|
||||||
|
b.Unlock()
|
||||||
}
|
}
|
||||||
k.RUnlock()
|
|
||||||
options := broker.NewPublishOptions(opts...)
|
records := make([]*kgo.Record, 0, len(messages))
|
||||||
records := make([]*kgo.Record, 0, len(msgs))
|
|
||||||
var errs []string
|
var errs []string
|
||||||
var err error
|
|
||||||
var key []byte
|
var key []byte
|
||||||
|
var promise func(*kgo.Record, error)
|
||||||
|
|
||||||
if options.Context != nil {
|
for _, msg := range messages {
|
||||||
if k, ok := options.Context.Value(publishKey{}).([]byte); ok && k != nil {
|
|
||||||
key = k
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, msg := range msgs {
|
if mctx := msg.Context(); mctx != nil {
|
||||||
rec := &kgo.Record{Context: ctx, Key: key}
|
if k, ok := mctx.Value(publishKey{}).([]byte); ok && k != nil {
|
||||||
rec.Topic, _ = msg.Header.Get(metadata.HeaderTopic)
|
key = k
|
||||||
if k.opts.Codec.String() == "noop" {
|
|
||||||
rec.Value = msg.Body
|
|
||||||
for k, v := range msg.Header {
|
|
||||||
rec.Headers = append(rec.Headers, kgo.RecordHeader{Key: k, Value: []byte(v)})
|
|
||||||
}
|
}
|
||||||
} else if options.BodyOnly {
|
if p, ok := mctx.Value(publishPromiseKey{}).(func(*kgo.Record, error)); ok && p != nil {
|
||||||
rec.Value = msg.Body
|
promise = p
|
||||||
} else {
|
|
||||||
rec.Value, err = k.opts.Codec.Marshal(msg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rec := &kgo.Record{
|
||||||
|
Context: ctx,
|
||||||
|
Key: key,
|
||||||
|
Topic: topic,
|
||||||
|
Value: msg.Body(),
|
||||||
|
}
|
||||||
|
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc()
|
||||||
|
|
||||||
|
setHeaders(rec, msg.Header())
|
||||||
|
|
||||||
records = append(records, rec)
|
records = append(records, rec)
|
||||||
}
|
}
|
||||||
|
|
||||||
results := k.writer.ProduceSync(ctx, records...)
|
ts := time.Now()
|
||||||
|
|
||||||
|
if promise != nil {
|
||||||
|
|
||||||
|
for _, rec := range records {
|
||||||
|
b.c.Produce(ctx, rec, func(r *kgo.Record, err error) {
|
||||||
|
te := time.Since(ts)
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Dec()
|
||||||
|
b.opts.Meter.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", rec.Topic, "topic", rec.Topic).Update(te.Seconds())
|
||||||
|
b.opts.Meter.Histogram(semconv.PublishMessageDurationSeconds, "endpoint", rec.Topic, "topic", rec.Topic).Update(te.Seconds())
|
||||||
|
if err != nil {
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", rec.Topic, "topic", rec.Topic, "status", "failure").Inc()
|
||||||
|
} else {
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", rec.Topic, "topic", rec.Topic, "status", "success").Inc()
|
||||||
|
}
|
||||||
|
promise(r, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
results := b.c.ProduceSync(ctx, records...)
|
||||||
|
|
||||||
|
te := time.Since(ts)
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
|
b.opts.Meter.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
||||||
|
b.opts.Meter.Histogram(semconv.PublishMessageDurationSeconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Dec()
|
||||||
if result.Err != nil {
|
if result.Err != nil {
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "failure").Inc()
|
||||||
errs = append(errs, result.Err.Error())
|
errs = append(errs, result.Err.Error())
|
||||||
|
} else {
|
||||||
|
b.opts.Meter.Counter(semconv.PublishMessageTotal, "endpoint", result.Record.Topic, "topic", result.Record.Topic, "status", "success").Inc()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,55 +423,42 @@ func (k *kBroker) publish(ctx context.Context, msgs []*broker.Message, opts ...b
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type mlogger struct {
|
func (k *Broker) TopicExists(ctx context.Context, topic string) error {
|
||||||
l logger.Logger
|
mdreq := kmsg.NewMetadataRequest()
|
||||||
ctx context.Context
|
mdreq.Topics = []kmsg.MetadataRequestTopic{
|
||||||
}
|
{Topic: &topic},
|
||||||
|
|
||||||
func (l *mlogger) Log(lvl kgo.LogLevel, msg string, args ...interface{}) {
|
|
||||||
var mlvl logger.Level
|
|
||||||
switch lvl {
|
|
||||||
case kgo.LogLevelNone:
|
|
||||||
return
|
|
||||||
case kgo.LogLevelError:
|
|
||||||
mlvl = logger.ErrorLevel
|
|
||||||
case kgo.LogLevelWarn:
|
|
||||||
mlvl = logger.WarnLevel
|
|
||||||
case kgo.LogLevelInfo:
|
|
||||||
mlvl = logger.InfoLevel
|
|
||||||
case kgo.LogLevelDebug:
|
|
||||||
mlvl = logger.DebugLevel
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fields := make(map[string]interface{}, int(len(args)/2))
|
|
||||||
for i := 0; i < len(args)/2; i += 2 {
|
mdrsp, err := mdreq.RequestWith(ctx, k.c)
|
||||||
fields[fmt.Sprintf("%v", args[i])] = args[i+1]
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if mdrsp.Topics[0].ErrorCode != 0 {
|
||||||
|
return fmt.Errorf("topic %s not exists or permission error", topic)
|
||||||
}
|
}
|
||||||
l.l.Fields(fields).Log(l.ctx, mlvl, msg)
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *mlogger) Level() kgo.LogLevel {
|
func (b *Broker) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
||||||
switch l.l.Options().Level {
|
return b.funcSubscribe(ctx, topic, handler, opts...)
|
||||||
case logger.ErrorLevel:
|
}
|
||||||
return kgo.LogLevelError
|
|
||||||
case logger.WarnLevel:
|
func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interface{}, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
||||||
return kgo.LogLevelWarn
|
if err := broker.IsValidHandler(handler); err != nil {
|
||||||
case logger.InfoLevel:
|
return nil, err
|
||||||
return kgo.LogLevelInfo
|
|
||||||
case logger.DebugLevel, logger.TraceLevel:
|
|
||||||
return kgo.LogLevelDebug
|
|
||||||
}
|
}
|
||||||
return kgo.LogLevelNone
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *kBroker) BatchSubscribe(ctx context.Context, topic string, handler broker.BatchHandler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *kBroker) Subscribe(ctx context.Context, topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
|
||||||
options := broker.NewSubscribeOptions(opts...)
|
options := broker.NewSubscribeOptions(opts...)
|
||||||
|
|
||||||
|
switch handler.(type) {
|
||||||
|
default:
|
||||||
|
return nil, broker.ErrInvalidHandler
|
||||||
|
case func(broker.Message) error:
|
||||||
|
break
|
||||||
|
case func([]broker.Message) error:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
if options.Group == "" {
|
if options.Group == "" {
|
||||||
uid, err := id.New()
|
uid, err := id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -351,100 +467,110 @@ func (k *kBroker) Subscribe(ctx context.Context, topic string, handler broker.Ha
|
|||||||
options.Group = uid
|
options.Group = uid
|
||||||
}
|
}
|
||||||
|
|
||||||
kaddrs := k.opts.Addrs
|
commitInterval := DefaultCommitInterval
|
||||||
|
if b.opts.Context != nil {
|
||||||
|
if v, ok := b.opts.Context.Value(commitIntervalKey{}).(time.Duration); ok && v > 0 {
|
||||||
|
commitInterval = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var fatalOnError bool
|
||||||
|
if b.opts.Context != nil {
|
||||||
|
if v, ok := b.opts.Context.Value(fatalOnErrorKey{}).(bool); ok && v {
|
||||||
|
fatalOnError = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.Context != nil {
|
||||||
|
if v, ok := options.Context.Value(fatalOnErrorKey{}).(bool); ok && v {
|
||||||
|
fatalOnError = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub := &Subscriber{
|
||||||
|
topic: topic,
|
||||||
|
opts: options,
|
||||||
|
handler: handler,
|
||||||
|
kopts: b.opts,
|
||||||
|
consumers: make(map[tp]*consumer),
|
||||||
|
done: make(chan struct{}),
|
||||||
|
fatalOnError: fatalOnError,
|
||||||
|
connected: b.connected,
|
||||||
|
}
|
||||||
|
|
||||||
|
kopts := append(b.kopts,
|
||||||
|
kgo.ConsumerGroup(options.Group),
|
||||||
|
kgo.ConsumeTopics(topic),
|
||||||
|
kgo.ConsumeResetOffset(kgo.NewOffset().AtStart()),
|
||||||
|
kgo.FetchMaxWait(1*time.Second),
|
||||||
|
kgo.AutoCommitInterval(commitInterval),
|
||||||
|
kgo.OnPartitionsAssigned(sub.assigned),
|
||||||
|
kgo.OnPartitionsRevoked(sub.revoked),
|
||||||
|
kgo.StopProducerOnDataLossDetected(),
|
||||||
|
kgo.OnPartitionsLost(sub.lost),
|
||||||
|
kgo.AutoCommitCallback(sub.autocommit),
|
||||||
|
kgo.AutoCommitMarks(),
|
||||||
|
)
|
||||||
|
|
||||||
|
if options.Context != nil {
|
||||||
|
if v, ok := options.Context.Value(optionsKey{}).([]kgo.Opt); ok && len(v) > 0 {
|
||||||
|
kopts = append(kopts, v...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c, htracer, err := b.connect(ctx, kopts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mdreq := kmsg.NewMetadataRequest()
|
||||||
|
mdreq.Topics = []kmsg.MetadataRequestTopic{
|
||||||
|
{Topic: &topic},
|
||||||
|
}
|
||||||
|
|
||||||
|
mdrsp, err := mdreq.RequestWith(ctx, c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if mdrsp.Topics[0].ErrorCode != 0 {
|
||||||
|
return nil, fmt.Errorf("topic %s not exists or permission error", topic)
|
||||||
|
}
|
||||||
|
|
||||||
|
sub.c = c
|
||||||
|
sub.htracer = htracer
|
||||||
|
|
||||||
|
go sub.poll(ctx)
|
||||||
|
|
||||||
|
b.Lock()
|
||||||
|
b.subs = append(b.subs, sub)
|
||||||
|
b.Unlock()
|
||||||
|
|
||||||
|
return sub, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *Broker) String() string {
|
||||||
|
return "kgo"
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBroker(opts ...broker.Option) *Broker {
|
||||||
|
options := broker.NewOptions(opts...)
|
||||||
|
|
||||||
|
kaddrs := options.Addrs
|
||||||
// shuffle addrs
|
// shuffle addrs
|
||||||
var rng mrand.Rand
|
var rng mrand.Rand
|
||||||
rng.Shuffle(len(kaddrs), func(i, j int) {
|
rng.Shuffle(len(kaddrs), func(i, j int) {
|
||||||
kaddrs[i], kaddrs[j] = kaddrs[j], kaddrs[i]
|
kaddrs[i], kaddrs[j] = kaddrs[j], kaddrs[i]
|
||||||
})
|
})
|
||||||
|
|
||||||
td := DefaultCommitInterval
|
|
||||||
if k.opts.Context != nil {
|
|
||||||
if v, ok := k.opts.Context.Value(commitIntervalKey{}).(time.Duration); ok && v > 0 {
|
|
||||||
td = v
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sub := &subscriber{
|
|
||||||
topic: topic,
|
|
||||||
done: make(chan struct{}),
|
|
||||||
opts: options,
|
|
||||||
handler: handler,
|
|
||||||
kopts: k.opts,
|
|
||||||
consumers: make(map[string]map[int32]worker),
|
|
||||||
}
|
|
||||||
|
|
||||||
kopts := append(k.kopts,
|
|
||||||
kgo.SeedBrokers(kaddrs...),
|
|
||||||
kgo.ConsumerGroup(options.Group),
|
|
||||||
kgo.ConsumeTopics(topic),
|
|
||||||
kgo.ConsumeResetOffset(kgo.NewOffset().AtStart()),
|
|
||||||
kgo.FetchMaxWait(1*time.Second),
|
|
||||||
// kgo.KeepControlRecords(),
|
|
||||||
kgo.Balancers(kgo.CooperativeStickyBalancer(), kgo.StickyBalancer()),
|
|
||||||
kgo.FetchIsolationLevel(kgo.ReadUncommitted()),
|
|
||||||
kgo.WithHooks(&metrics{meter: k.opts.Meter}),
|
|
||||||
kgo.AutoCommitMarks(),
|
|
||||||
kgo.AutoCommitInterval(td),
|
|
||||||
kgo.OnPartitionsAssigned(sub.assigned),
|
|
||||||
kgo.OnPartitionsRevoked(sub.revoked),
|
|
||||||
kgo.OnPartitionsLost(sub.revoked),
|
|
||||||
)
|
|
||||||
|
|
||||||
reader, err := kgo.NewClient(kopts...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sub.reader = reader
|
|
||||||
go sub.run(ctx)
|
|
||||||
|
|
||||||
k.Lock()
|
|
||||||
k.subs = append(k.subs, sub)
|
|
||||||
k.Unlock()
|
|
||||||
return sub, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *kBroker) String() string {
|
|
||||||
return "kgo"
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBroker(opts ...broker.Option) *kBroker {
|
|
||||||
options := broker.NewOptions(opts...)
|
|
||||||
if options.Codec.String() != "noop" {
|
|
||||||
options.Logger.Infof(options.Context, "broker codec not noop, disable plain kafka headers usage")
|
|
||||||
}
|
|
||||||
kopts := []kgo.Opt{
|
kopts := []kgo.Opt{
|
||||||
|
kgo.DialTimeout(3 * time.Second),
|
||||||
kgo.DisableIdempotentWrite(),
|
kgo.DisableIdempotentWrite(),
|
||||||
kgo.ProducerBatchCompression(kgo.NoCompression()),
|
kgo.ProducerBatchCompression(kgo.NoCompression()),
|
||||||
kgo.WithLogger(&mlogger{l: options.Logger, ctx: options.Context}),
|
kgo.WithLogger(&mlogger{l: options.Logger.Clone(logger.WithAddCallerSkipCount(2)), ctx: options.Context}),
|
||||||
kgo.RetryBackoffFn(
|
kgo.SeedBrokers(kaddrs...),
|
||||||
func() func(int) time.Duration {
|
kgo.RetryBackoffFn(DefaultRetryBackoffFn),
|
||||||
var rng mrand.Rand
|
kgo.BlockRebalanceOnPoll(),
|
||||||
return func(fails int) time.Duration {
|
kgo.Balancers(kgo.CooperativeStickyBalancer()),
|
||||||
const (
|
kgo.FetchIsolationLevel(kgo.ReadUncommitted()),
|
||||||
min = 250 * time.Millisecond
|
kgo.UnknownTopicRetries(1),
|
||||||
max = 2 * time.Second
|
|
||||||
)
|
|
||||||
if fails <= 0 {
|
|
||||||
return min
|
|
||||||
}
|
|
||||||
if fails > 10 {
|
|
||||||
return max
|
|
||||||
}
|
|
||||||
|
|
||||||
backoff := min * time.Duration(1<<(fails-1))
|
|
||||||
jitter := 0.8 + 0.4*rng.Float64()
|
|
||||||
backoff = time.Duration(float64(backoff) * jitter)
|
|
||||||
|
|
||||||
if backoff > max {
|
|
||||||
return max
|
|
||||||
}
|
|
||||||
return backoff
|
|
||||||
}
|
|
||||||
}(),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.Context != nil {
|
if options.Context != nil {
|
||||||
@@ -453,8 +579,9 @@ func NewBroker(opts ...broker.Option) *kBroker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &kBroker{
|
return &Broker{
|
||||||
opts: options,
|
connected: &atomic.Uint32{},
|
||||||
kopts: kopts,
|
opts: options,
|
||||||
|
kopts: kopts,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
137
kgo_test.go
137
kgo_test.go
@@ -2,7 +2,6 @@ package kgo_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -10,22 +9,124 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
kg "github.com/twmb/franz-go/pkg/kgo"
|
kg "github.com/twmb/franz-go/pkg/kgo"
|
||||||
kgo "go.unistack.org/micro-broker-kgo/v3"
|
kgo "go.unistack.org/micro-broker-kgo/v4"
|
||||||
"go.unistack.org/micro/v3/broker"
|
"go.unistack.org/micro/v4/broker"
|
||||||
"go.unistack.org/micro/v3/logger"
|
"go.unistack.org/micro/v4/logger"
|
||||||
"go.unistack.org/micro/v3/metadata"
|
"go.unistack.org/micro/v4/logger/slog"
|
||||||
|
"go.unistack.org/micro/v4/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
msgcnt = int64(12000000)
|
msgcnt = int64(1200)
|
||||||
group = "38"
|
group = "38"
|
||||||
prefill = false
|
prefill = true
|
||||||
loglevel = logger.InfoLevel
|
loglevel = logger.DebugLevel
|
||||||
)
|
)
|
||||||
|
|
||||||
var bm = &broker.Message{
|
func TestFail(t *testing.T) {
|
||||||
Header: map[string]string{"hkey": "hval", metadata.HeaderTopic: "test"},
|
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
||||||
Body: []byte(`"body"`),
|
t.Skip()
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.DefaultLogger = slog.NewLogger()
|
||||||
|
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
var addrs []string
|
||||||
|
if addr := os.Getenv("BROKER_ADDRS"); len(addr) == 0 {
|
||||||
|
addrs = []string{"127.0.0.1:9092"}
|
||||||
|
} else {
|
||||||
|
addrs = strings.Split(addr, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
b := kgo.NewBroker(
|
||||||
|
broker.Addrs(addrs...),
|
||||||
|
kgo.CommitInterval(5*time.Second),
|
||||||
|
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024),
|
||||||
|
kg.AllowAutoTopicCreation(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
t.Logf("broker init")
|
||||||
|
if err := b.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("broker connect")
|
||||||
|
if err := b.Connect(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
t.Logf("broker disconnect")
|
||||||
|
if err := b.Disconnect(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
t.Logf("broker health %v", b.Health())
|
||||||
|
msgs := make([]broker.Message, 0, msgcnt)
|
||||||
|
for i := int64(0); i < msgcnt; i++ {
|
||||||
|
m, _ := b.NewMessage(ctx, metadata.Pairs("hkey", "hval"), []byte(`test`))
|
||||||
|
msgs = append(msgs, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, msg := range msgs {
|
||||||
|
t.Logf("broker publish")
|
||||||
|
if err := b.Publish(ctx, "test", msg); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// t.Skip()
|
||||||
|
|
||||||
|
idx := int64(0)
|
||||||
|
fn := func(msg broker.Message) error {
|
||||||
|
atomic.AddInt64(&idx, 1)
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
t.Logf("ack")
|
||||||
|
return msg.Ack()
|
||||||
|
}
|
||||||
|
|
||||||
|
sub, err := b.Subscribe(ctx, "test", fn,
|
||||||
|
broker.SubscribeAutoAck(true),
|
||||||
|
broker.SubscribeGroup(group),
|
||||||
|
broker.SubscribeBodyOnly(true))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if err := sub.Unsubscribe(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
t.Logf("health check")
|
||||||
|
if !b.Health() {
|
||||||
|
t.Logf("health works")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConnect(t *testing.T) {
|
||||||
|
var addrs []string
|
||||||
|
ctx := context.TODO()
|
||||||
|
b := kgo.NewBroker(
|
||||||
|
broker.Addrs(addrs...),
|
||||||
|
kgo.CommitInterval(5*time.Second),
|
||||||
|
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024)),
|
||||||
|
)
|
||||||
|
if err := b.Init(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := b.Connect(ctx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPubSub(t *testing.T) {
|
func TestPubSub(t *testing.T) {
|
||||||
@@ -33,7 +134,7 @@ func TestPubSub(t *testing.T) {
|
|||||||
t.Skip()
|
t.Skip()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel), logger.WithCallerSkipCount(3)); err != nil {
|
if err := logger.DefaultLogger.Init(logger.WithLevel(loglevel)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
@@ -50,6 +151,7 @@ func TestPubSub(t *testing.T) {
|
|||||||
kgo.CommitInterval(5*time.Second),
|
kgo.CommitInterval(5*time.Second),
|
||||||
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024)),
|
kgo.Options(kg.ClientID("test"), kg.FetchMaxBytes(10*1024*1024)),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
if err := b.Init(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -64,19 +166,20 @@ func TestPubSub(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if prefill {
|
if prefill {
|
||||||
msgs := make([]*broker.Message, 0, msgcnt)
|
msgs := make([]broker.Message, 0, msgcnt)
|
||||||
for i := int64(0); i < msgcnt; i++ {
|
for i := int64(0); i < msgcnt; i++ {
|
||||||
msgs = append(msgs, bm)
|
m, _ := b.NewMessage(ctx, metadata.Pairs("hkey", "hval"), []byte(`test`))
|
||||||
|
msgs = append(msgs, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := b.BatchPublish(ctx, msgs); err != nil {
|
if err := b.Publish(ctx, "test", msgs...); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// t.Skip()
|
// t.Skip()
|
||||||
}
|
}
|
||||||
done := make(chan bool, 1)
|
done := make(chan bool, 1)
|
||||||
idx := int64(0)
|
idx := int64(0)
|
||||||
fn := func(msg broker.Event) error {
|
fn := func(msg broker.Message) error {
|
||||||
atomic.AddInt64(&idx, 1)
|
atomic.AddInt64(&idx, 1)
|
||||||
// time.Sleep(200 * time.Millisecond)
|
// time.Sleep(200 * time.Millisecond)
|
||||||
return msg.Ack()
|
return msg.Ack()
|
||||||
@@ -107,7 +210,7 @@ func TestPubSub(t *testing.T) {
|
|||||||
if prc := atomic.LoadInt64(&idx); prc == msgcnt {
|
if prc := atomic.LoadInt64(&idx); prc == msgcnt {
|
||||||
close(done)
|
close(done)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("processed %v\n", prc)
|
t.Logf("processed %v\n", prc)
|
||||||
}
|
}
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
close(done)
|
close(done)
|
||||||
|
47
logger.go
Normal file
47
logger.go
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package kgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
|
"go.unistack.org/micro/v4/logger"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mlogger struct {
|
||||||
|
l logger.Logger
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *mlogger) Log(lvl kgo.LogLevel, msg string, args ...interface{}) {
|
||||||
|
var mlvl logger.Level
|
||||||
|
switch lvl {
|
||||||
|
case kgo.LogLevelNone:
|
||||||
|
return
|
||||||
|
case kgo.LogLevelError:
|
||||||
|
mlvl = logger.ErrorLevel
|
||||||
|
case kgo.LogLevelWarn:
|
||||||
|
mlvl = logger.WarnLevel
|
||||||
|
case kgo.LogLevelInfo:
|
||||||
|
mlvl = logger.InfoLevel
|
||||||
|
case kgo.LogLevelDebug:
|
||||||
|
mlvl = logger.DebugLevel
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
l.l.Log(l.ctx, mlvl, msg, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *mlogger) Level() kgo.LogLevel {
|
||||||
|
switch l.l.Options().Level {
|
||||||
|
case logger.ErrorLevel:
|
||||||
|
return kgo.LogLevelError
|
||||||
|
case logger.WarnLevel:
|
||||||
|
return kgo.LogLevelWarn
|
||||||
|
case logger.InfoLevel:
|
||||||
|
return kgo.LogLevelInfo
|
||||||
|
case logger.DebugLevel, logger.TraceLevel:
|
||||||
|
return kgo.LogLevelDebug
|
||||||
|
}
|
||||||
|
return kgo.LogLevelNone
|
||||||
|
}
|
@@ -6,22 +6,29 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/twmb/franz-go/pkg/kgo"
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
"go.unistack.org/micro/v3/meter"
|
"go.unistack.org/micro/v4/meter"
|
||||||
)
|
)
|
||||||
|
|
||||||
type metrics struct {
|
type hookMeter struct {
|
||||||
meter meter.Meter
|
meter meter.Meter
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ kgo.HookBrokerConnect = &metrics{}
|
_ kgo.HookBrokerConnect = &hookMeter{}
|
||||||
_ kgo.HookBrokerDisconnect = &metrics{}
|
_ kgo.HookBrokerDisconnect = &hookMeter{}
|
||||||
_ kgo.HookBrokerRead = &metrics{}
|
// HookBrokerE2E
|
||||||
_ kgo.HookBrokerThrottle = &metrics{}
|
_ kgo.HookBrokerRead = &hookMeter{}
|
||||||
_ kgo.HookBrokerWrite = &metrics{}
|
_ kgo.HookBrokerThrottle = &hookMeter{}
|
||||||
_ kgo.HookFetchBatchRead = &metrics{}
|
_ kgo.HookBrokerWrite = &hookMeter{}
|
||||||
_ kgo.HookProduceBatchWritten = &metrics{}
|
_ kgo.HookFetchBatchRead = &hookMeter{}
|
||||||
_ kgo.HookGroupManageError = &metrics{}
|
// HookFetchRecordBuffered
|
||||||
|
// HookFetchRecordUnbuffered
|
||||||
|
_ kgo.HookGroupManageError = &hookMeter{}
|
||||||
|
// HookNewClient
|
||||||
|
_ kgo.HookProduceBatchWritten = &hookMeter{}
|
||||||
|
// HookProduceRecordBuffered
|
||||||
|
// HookProduceRecordPartitioned
|
||||||
|
// HookProduceRecordUnbuffered
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -54,11 +61,11 @@ const (
|
|||||||
labelTopic = "topic"
|
labelTopic = "topic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *metrics) OnGroupManageError(err error) {
|
func (m *hookMeter) OnGroupManageError(_ error) {
|
||||||
m.meter.Counter(metricBrokerGroupErrors).Inc()
|
m.meter.Counter(metricBrokerGroupErrors).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) OnBrokerConnect(meta kgo.BrokerMetadata, _ time.Duration, _ net.Conn, err error) {
|
func (m *hookMeter) OnBrokerConnect(meta kgo.BrokerMetadata, _ time.Duration, _ net.Conn, err error) {
|
||||||
node := strconv.Itoa(int(meta.NodeID))
|
node := strconv.Itoa(int(meta.NodeID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.meter.Counter(metricBrokerConnects, labelNode, node, labelStatus, labelFaulure).Inc()
|
m.meter.Counter(metricBrokerConnects, labelNode, node, labelStatus, labelFaulure).Inc()
|
||||||
@@ -67,12 +74,12 @@ func (m *metrics) OnBrokerConnect(meta kgo.BrokerMetadata, _ time.Duration, _ ne
|
|||||||
m.meter.Counter(metricBrokerConnects, labelNode, node, labelStatus, labelSuccess).Inc()
|
m.meter.Counter(metricBrokerConnects, labelNode, node, labelStatus, labelSuccess).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) OnBrokerDisconnect(meta kgo.BrokerMetadata, _ net.Conn) {
|
func (m *hookMeter) OnBrokerDisconnect(meta kgo.BrokerMetadata, _ net.Conn) {
|
||||||
node := strconv.Itoa(int(meta.NodeID))
|
node := strconv.Itoa(int(meta.NodeID))
|
||||||
m.meter.Counter(metricBrokerDisconnects, labelNode, node).Inc()
|
m.meter.Counter(metricBrokerDisconnects, labelNode, node).Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) OnBrokerWrite(meta kgo.BrokerMetadata, _ int16, bytesWritten int, writeWait, timeToWrite time.Duration, err error) {
|
func (m *hookMeter) OnBrokerWrite(meta kgo.BrokerMetadata, _ int16, bytesWritten int, writeWait, timeToWrite time.Duration, err error) {
|
||||||
node := strconv.Itoa(int(meta.NodeID))
|
node := strconv.Itoa(int(meta.NodeID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.meter.Counter(metricBrokerWriteErrors, labelNode, node).Inc()
|
m.meter.Counter(metricBrokerWriteErrors, labelNode, node).Inc()
|
||||||
@@ -83,7 +90,7 @@ func (m *metrics) OnBrokerWrite(meta kgo.BrokerMetadata, _ int16, bytesWritten i
|
|||||||
m.meter.Histogram(metricBrokerWriteLatencies, labelNode, node).Update(timeToWrite.Seconds())
|
m.meter.Histogram(metricBrokerWriteLatencies, labelNode, node).Update(timeToWrite.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) OnBrokerRead(meta kgo.BrokerMetadata, _ int16, bytesRead int, readWait, timeToRead time.Duration, err error) {
|
func (m *hookMeter) OnBrokerRead(meta kgo.BrokerMetadata, _ int16, bytesRead int, readWait, timeToRead time.Duration, err error) {
|
||||||
node := strconv.Itoa(int(meta.NodeID))
|
node := strconv.Itoa(int(meta.NodeID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.meter.Counter(metricBrokerReadErrors, labelNode, node).Inc()
|
m.meter.Counter(metricBrokerReadErrors, labelNode, node).Inc()
|
||||||
@@ -95,18 +102,18 @@ func (m *metrics) OnBrokerRead(meta kgo.BrokerMetadata, _ int16, bytesRead int,
|
|||||||
m.meter.Histogram(metricBrokerReadLatencies, labelNode, node).Update(timeToRead.Seconds())
|
m.meter.Histogram(metricBrokerReadLatencies, labelNode, node).Update(timeToRead.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) OnBrokerThrottle(meta kgo.BrokerMetadata, throttleInterval time.Duration, _ bool) {
|
func (m *hookMeter) OnBrokerThrottle(meta kgo.BrokerMetadata, throttleInterval time.Duration, _ bool) {
|
||||||
node := strconv.Itoa(int(meta.NodeID))
|
node := strconv.Itoa(int(meta.NodeID))
|
||||||
m.meter.Histogram(metricBrokerThrottleLatencies, labelNode, node).Update(throttleInterval.Seconds())
|
m.meter.Histogram(metricBrokerThrottleLatencies, labelNode, node).Update(throttleInterval.Seconds())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) OnProduceBatchWritten(meta kgo.BrokerMetadata, topic string, _ int32, kmetrics kgo.ProduceBatchMetrics) {
|
func (m *hookMeter) OnProduceBatchWritten(meta kgo.BrokerMetadata, topic string, _ int32, kmetrics kgo.ProduceBatchMetrics) {
|
||||||
node := strconv.Itoa(int(meta.NodeID))
|
node := strconv.Itoa(int(meta.NodeID))
|
||||||
m.meter.Counter(metricBrokerProduceBytesUncompressed, labelNode, node, labelTopic, topic).Add(kmetrics.UncompressedBytes)
|
m.meter.Counter(metricBrokerProduceBytesUncompressed, labelNode, node, labelTopic, topic).Add(kmetrics.UncompressedBytes)
|
||||||
m.meter.Counter(metricBrokerProduceBytesCompressed, labelNode, node, labelTopic, topic).Add(kmetrics.CompressedBytes)
|
m.meter.Counter(metricBrokerProduceBytesCompressed, labelNode, node, labelTopic, topic).Add(kmetrics.CompressedBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metrics) OnFetchBatchRead(meta kgo.BrokerMetadata, topic string, _ int32, kmetrics kgo.FetchBatchMetrics) {
|
func (m *hookMeter) OnFetchBatchRead(meta kgo.BrokerMetadata, topic string, _ int32, kmetrics kgo.FetchBatchMetrics) {
|
||||||
node := strconv.Itoa(int(meta.NodeID))
|
node := strconv.Itoa(int(meta.NodeID))
|
||||||
m.meter.Counter(metricBrokerFetchBytesUncompressed, labelNode, node, labelTopic, topic).Add(kmetrics.UncompressedBytes)
|
m.meter.Counter(metricBrokerFetchBytesUncompressed, labelNode, node, labelTopic, topic).Add(kmetrics.UncompressedBytes)
|
||||||
m.meter.Counter(metricBrokerFetchBytesCompressed, labelNode, node, labelTopic, topic).Add(kmetrics.CompressedBytes)
|
m.meter.Counter(metricBrokerFetchBytesCompressed, labelNode, node, labelTopic, topic).Add(kmetrics.CompressedBytes)
|
69
options.go
69
options.go
@@ -4,14 +4,21 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
kgo "github.com/twmb/franz-go/pkg/kgo"
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
"go.unistack.org/micro/v3/broker"
|
"go.unistack.org/micro/v4/broker"
|
||||||
"go.unistack.org/micro/v3/client"
|
|
||||||
"go.unistack.org/micro/v3/server"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultCommitInterval specifies how fast send commit offsets to kafka
|
var (
|
||||||
var DefaultCommitInterval = 5 * time.Second
|
|
||||||
|
// DefaultCommitInterval specifies how fast send commit offsets to kafka
|
||||||
|
DefaultCommitInterval = 5 * time.Second
|
||||||
|
|
||||||
|
// DefaultStatsInterval specifies how fast check consumer lag
|
||||||
|
DefaultStatsInterval = 5 * time.Second
|
||||||
|
|
||||||
|
// DefaultSubscribeMaxInflight specifies how much messages keep inflight
|
||||||
|
DefaultSubscribeMaxInflight = 100
|
||||||
|
)
|
||||||
|
|
||||||
type subscribeContextKey struct{}
|
type subscribeContextKey struct{}
|
||||||
|
|
||||||
@@ -27,11 +34,6 @@ func PublishKey(key []byte) broker.PublishOption {
|
|||||||
return broker.SetPublishOption(publishKey{}, key)
|
return broker.SetPublishOption(publishKey{}, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClientPublishKey set the kafka message key (client option)
|
|
||||||
func ClientPublishKey(key []byte) client.PublishOption {
|
|
||||||
return client.SetPublishOption(publishKey{}, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
type optionsKey struct{}
|
type optionsKey struct{}
|
||||||
|
|
||||||
// Options pass additional options to broker
|
// Options pass additional options to broker
|
||||||
@@ -49,7 +51,7 @@ func Options(opts ...kgo.Opt) broker.Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeOptions pass additional options to broker
|
// SubscribeOptions pass additional options to broker in Subscribe
|
||||||
func SubscribeOptions(opts ...kgo.Opt) broker.SubscribeOption {
|
func SubscribeOptions(opts ...kgo.Opt) broker.SubscribeOption {
|
||||||
return func(o *broker.SubscribeOptions) {
|
return func(o *broker.SubscribeOptions) {
|
||||||
if o.Context == nil {
|
if o.Context == nil {
|
||||||
@@ -64,19 +66,22 @@ func SubscribeOptions(opts ...kgo.Opt) broker.SubscribeOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscriberOptions pass additional options to broker
|
type fatalOnErrorKey struct{}
|
||||||
func SubscriberOptions(opts ...kgo.Opt) server.SubscriberOption {
|
|
||||||
return func(o *server.SubscriberOptions) {
|
func FatalOnError(b bool) broker.Option {
|
||||||
if o.Context == nil {
|
return broker.SetOption(fatalOnErrorKey{}, b)
|
||||||
o.Context = context.Background()
|
}
|
||||||
}
|
|
||||||
options, ok := o.Context.Value(optionsKey{}).([]kgo.Opt)
|
type clientIDKey struct{}
|
||||||
if !ok {
|
|
||||||
options = make([]kgo.Opt, 0, len(opts))
|
func ClientID(id string) broker.Option {
|
||||||
}
|
return broker.SetOption(clientIDKey{}, id)
|
||||||
options = append(options, opts...)
|
}
|
||||||
o.Context = context.WithValue(o.Context, optionsKey{}, options)
|
|
||||||
}
|
type groupKey struct{}
|
||||||
|
|
||||||
|
func Group(id string) broker.Option {
|
||||||
|
return broker.SetOption(groupKey{}, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
type commitIntervalKey struct{}
|
type commitIntervalKey struct{}
|
||||||
@@ -86,11 +91,21 @@ func CommitInterval(td time.Duration) broker.Option {
|
|||||||
return broker.SetOption(commitIntervalKey{}, td)
|
return broker.SetOption(commitIntervalKey{}, td)
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultSubscribeMaxInflight = 1000
|
|
||||||
|
|
||||||
type subscribeMaxInflightKey struct{}
|
type subscribeMaxInflightKey struct{}
|
||||||
|
|
||||||
// SubscribeMaxInFlight max queued messages
|
// SubscribeMaxInFlight max queued messages
|
||||||
func SubscribeMaxInFlight(n int) broker.SubscribeOption {
|
func SubscribeMaxInFlight(n int) broker.SubscribeOption {
|
||||||
return broker.SetSubscribeOption(subscribeMaxInflightKey{}, n)
|
return broker.SetSubscribeOption(subscribeMaxInflightKey{}, n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SubscribeMaxInFlight max queued messages
|
||||||
|
func SubscribeFatalOnError(b bool) broker.SubscribeOption {
|
||||||
|
return broker.SetSubscribeOption(fatalOnErrorKey{}, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
type publishPromiseKey struct{}
|
||||||
|
|
||||||
|
// PublishPromise set the kafka promise func for Produce
|
||||||
|
func PublishPromise(fn func(*kgo.Record, error)) broker.PublishOption {
|
||||||
|
return broker.SetPublishOption(publishPromiseKey{}, fn)
|
||||||
|
}
|
||||||
|
291
subscriber.go
Normal file
291
subscriber.go
Normal file
@@ -0,0 +1,291 @@
|
|||||||
|
package kgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/twmb/franz-go/pkg/kadm"
|
||||||
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
|
"github.com/twmb/franz-go/pkg/kmsg"
|
||||||
|
"go.unistack.org/micro/v4/broker"
|
||||||
|
"go.unistack.org/micro/v4/logger"
|
||||||
|
"go.unistack.org/micro/v4/metadata"
|
||||||
|
"go.unistack.org/micro/v4/semconv"
|
||||||
|
"go.unistack.org/micro/v4/tracer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type tp struct {
|
||||||
|
t string
|
||||||
|
p int32
|
||||||
|
}
|
||||||
|
|
||||||
|
type consumer struct {
|
||||||
|
topic string
|
||||||
|
c *kgo.Client
|
||||||
|
htracer *hookTracer
|
||||||
|
quit chan struct{}
|
||||||
|
done chan struct{}
|
||||||
|
recs chan kgo.FetchTopicPartition
|
||||||
|
kopts broker.Options
|
||||||
|
partition int32
|
||||||
|
opts broker.SubscribeOptions
|
||||||
|
handler interface{}
|
||||||
|
connected *atomic.Uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type Subscriber struct {
|
||||||
|
consumers map[tp]*consumer
|
||||||
|
c *kgo.Client
|
||||||
|
htracer *hookTracer
|
||||||
|
topic string
|
||||||
|
|
||||||
|
handler interface{}
|
||||||
|
done chan struct{}
|
||||||
|
kopts broker.Options
|
||||||
|
opts broker.SubscribeOptions
|
||||||
|
|
||||||
|
connected *atomic.Uint32
|
||||||
|
sync.RWMutex
|
||||||
|
closed bool
|
||||||
|
fatalOnError bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) Client() *kgo.Client {
|
||||||
|
return s.c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) Options() broker.SubscribeOptions {
|
||||||
|
return s.opts
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) Topic() string {
|
||||||
|
return s.topic
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) Unsubscribe(ctx context.Context) error {
|
||||||
|
if s.closed {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
s.c.PauseFetchTopics(s.topic)
|
||||||
|
s.c.CloseAllowingRebalance()
|
||||||
|
kc := make(map[string][]int32)
|
||||||
|
for ctp := range s.consumers {
|
||||||
|
kc[ctp.t] = append(kc[ctp.t], ctp.p)
|
||||||
|
}
|
||||||
|
s.killConsumers(ctx, kc)
|
||||||
|
close(s.done)
|
||||||
|
s.closed = true
|
||||||
|
s.c.ResumeFetchTopics(s.topic)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) poll(ctx context.Context) {
|
||||||
|
maxInflight := DefaultSubscribeMaxInflight
|
||||||
|
if s.opts.Context != nil {
|
||||||
|
if n, ok := s.opts.Context.Value(subscribeMaxInflightKey{}).(int); n > 0 && ok {
|
||||||
|
maxInflight = n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
ac := kadm.NewClient(s.c)
|
||||||
|
ticker := time.NewTicker(DefaultStatsInterval)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
ticker.Stop()
|
||||||
|
return
|
||||||
|
case <-ticker.C:
|
||||||
|
dgls, err := ac.Lag(ctx, s.opts.Group)
|
||||||
|
if err != nil || !dgls.Ok() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
dgl, ok := dgls[s.opts.Group]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lmap, ok := dgl.Lag[s.topic]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Lock()
|
||||||
|
for p, l := range lmap {
|
||||||
|
s.kopts.Meter.Counter(semconv.BrokerGroupLag, "topic", s.topic, "group", s.opts.Group, "partition", strconv.Itoa(int(p)), "lag", strconv.Itoa(int(l.Lag)))
|
||||||
|
}
|
||||||
|
s.Unlock()
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
s.c.CloseAllowingRebalance()
|
||||||
|
return
|
||||||
|
case <-s.done:
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
fetches := s.c.PollRecords(ctx, maxInflight)
|
||||||
|
if !s.closed && fetches.IsClientClosed() {
|
||||||
|
s.closed = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fetches.EachError(func(t string, p int32, err error) {
|
||||||
|
s.kopts.Logger.Fatal(ctx, fmt.Sprintf("[kgo] fetch topic %s partition %d error", t, p), err)
|
||||||
|
})
|
||||||
|
|
||||||
|
fetches.EachPartition(func(p kgo.FetchTopicPartition) {
|
||||||
|
tps := tp{p.Topic, p.Partition}
|
||||||
|
s.consumers[tps].recs <- p
|
||||||
|
})
|
||||||
|
s.c.AllowRebalance()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) killConsumers(ctx context.Context, lost map[string][]int32) {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
defer wg.Wait()
|
||||||
|
|
||||||
|
for topic, partitions := range lost {
|
||||||
|
for _, partition := range partitions {
|
||||||
|
tps := tp{topic, partition}
|
||||||
|
pc := s.consumers[tps]
|
||||||
|
delete(s.consumers, tps)
|
||||||
|
close(pc.quit)
|
||||||
|
if s.kopts.Logger.V(logger.DebugLevel) {
|
||||||
|
s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] waiting for work to finish topic %s partition %d", topic, partition))
|
||||||
|
}
|
||||||
|
wg.Add(1)
|
||||||
|
go func() { <-pc.done; wg.Done() }()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) autocommit(_ *kgo.Client, _ *kmsg.OffsetCommitRequest, _ *kmsg.OffsetCommitResponse, err error) {
|
||||||
|
if err != nil {
|
||||||
|
s.connected.Store(0)
|
||||||
|
if s.fatalOnError {
|
||||||
|
s.kopts.Logger.Fatal(context.TODO(), "kgo.AutoCommitCallback error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) lost(ctx context.Context, _ *kgo.Client, lost map[string][]int32) {
|
||||||
|
if s.kopts.Logger.V(logger.ErrorLevel) {
|
||||||
|
s.kopts.Logger.Error(ctx, fmt.Sprintf("[kgo] lost %#+v", lost))
|
||||||
|
}
|
||||||
|
s.killConsumers(ctx, lost)
|
||||||
|
// s.connected.Store(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) revoked(ctx context.Context, c *kgo.Client, revoked map[string][]int32) {
|
||||||
|
if s.kopts.Logger.V(logger.DebugLevel) {
|
||||||
|
s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] revoked %#+v", revoked))
|
||||||
|
}
|
||||||
|
s.killConsumers(ctx, revoked)
|
||||||
|
if err := c.CommitMarkedOffsets(ctx); err != nil {
|
||||||
|
s.kopts.Logger.Error(ctx, "[kgo] revoked CommitMarkedOffsets error", err)
|
||||||
|
// s.connected.Store(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Subscriber) assigned(_ context.Context, c *kgo.Client, assigned map[string][]int32) {
|
||||||
|
for topic, partitions := range assigned {
|
||||||
|
for _, partition := range partitions {
|
||||||
|
pc := &consumer{
|
||||||
|
c: c,
|
||||||
|
topic: topic,
|
||||||
|
partition: partition,
|
||||||
|
htracer: s.htracer,
|
||||||
|
quit: make(chan struct{}),
|
||||||
|
done: make(chan struct{}),
|
||||||
|
recs: make(chan kgo.FetchTopicPartition, 100),
|
||||||
|
handler: s.handler,
|
||||||
|
kopts: s.kopts,
|
||||||
|
opts: s.opts,
|
||||||
|
connected: s.connected,
|
||||||
|
}
|
||||||
|
s.Lock()
|
||||||
|
s.consumers[tp{topic, partition}] = pc
|
||||||
|
s.Unlock()
|
||||||
|
go pc.consume()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *consumer) consume() {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
defer close(pc.done)
|
||||||
|
if pc.kopts.Logger.V(logger.DebugLevel) {
|
||||||
|
pc.kopts.Logger.Debug(pc.kopts.Context, fmt.Sprintf("starting, topic %s partition %d", pc.topic, pc.partition))
|
||||||
|
defer pc.kopts.Logger.Debug(pc.kopts.Context, fmt.Sprintf("killing, topic %s partition %d", pc.topic, pc.partition))
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-pc.quit:
|
||||||
|
return
|
||||||
|
case p := <-pc.recs:
|
||||||
|
for _, record := range p.Records {
|
||||||
|
ctx, sp := pc.htracer.WithProcessSpan(record)
|
||||||
|
ts := time.Now()
|
||||||
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Inc()
|
||||||
|
p := messagePool.Get().(*kgoMessage)
|
||||||
|
|
||||||
|
p.body = record.Value
|
||||||
|
p.topic = record.Topic
|
||||||
|
p.ack = false
|
||||||
|
p.hdr = metadata.New(len(record.Headers))
|
||||||
|
p.ctx = ctx
|
||||||
|
for _, hdr := range record.Headers {
|
||||||
|
p.hdr.Set(hdr.Key, string(hdr.Value))
|
||||||
|
}
|
||||||
|
|
||||||
|
switch h := pc.handler.(type) {
|
||||||
|
case func(broker.Message) error:
|
||||||
|
err = h(p)
|
||||||
|
case func([]broker.Message) error:
|
||||||
|
err = h([]broker.Message{p})
|
||||||
|
}
|
||||||
|
|
||||||
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageInflight, "endpoint", record.Topic, "topic", record.Topic).Dec()
|
||||||
|
if err != nil {
|
||||||
|
sp.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
pc.kopts.Meter.Counter(semconv.SubscribeMessageTotal, "endpoint", record.Topic, "topic", record.Topic, "status", "failure").Inc()
|
||||||
|
} else if pc.opts.AutoAck {
|
||||||
|
p.ack = true
|
||||||
|
}
|
||||||
|
|
||||||
|
te := time.Since(ts)
|
||||||
|
pc.kopts.Meter.Summary(semconv.SubscribeMessageLatencyMicroseconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
||||||
|
pc.kopts.Meter.Histogram(semconv.SubscribeMessageDurationSeconds, "endpoint", record.Topic, "topic", record.Topic).Update(te.Seconds())
|
||||||
|
|
||||||
|
ack := p.ack
|
||||||
|
messagePool.Put(p)
|
||||||
|
|
||||||
|
if ack {
|
||||||
|
pc.c.MarkCommitRecords(record)
|
||||||
|
} else {
|
||||||
|
sp.Finish()
|
||||||
|
pc.connected.Store(0)
|
||||||
|
pc.kopts.Logger.Fatal(pc.kopts.Context, "[kgo] message not commited")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sp.Finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
204
tracer.go
Normal file
204
tracer.go
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
package kgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/twmb/franz-go/pkg/kgo"
|
||||||
|
semconv "go.opentelemetry.io/otel/semconv/v1.18.0"
|
||||||
|
"go.unistack.org/micro/v4/metadata"
|
||||||
|
"go.unistack.org/micro/v4/tracer"
|
||||||
|
)
|
||||||
|
|
||||||
|
type hookTracer struct {
|
||||||
|
tracer tracer.Tracer
|
||||||
|
clientID string
|
||||||
|
group string
|
||||||
|
}
|
||||||
|
|
||||||
|
var messagingSystem = semconv.MessagingSystemKey.String("kafka")
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ kgo.HookProduceRecordBuffered = (*hookTracer)(nil)
|
||||||
|
_ kgo.HookProduceRecordUnbuffered = (*hookTracer)(nil)
|
||||||
|
_ kgo.HookFetchRecordBuffered = (*hookTracer)(nil)
|
||||||
|
_ kgo.HookFetchRecordUnbuffered = (*hookTracer)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
|
// OnProduceRecordBuffered starts a new span for the "publish" operation on a
|
||||||
|
// buffered record.
|
||||||
|
//
|
||||||
|
// It sets span options and injects the span context into record and updates
|
||||||
|
// the record's context, so it can be ended in the OnProduceRecordUnbuffered
|
||||||
|
// hook.
|
||||||
|
func (m *hookTracer) OnProduceRecordBuffered(r *kgo.Record) {
|
||||||
|
// Set up span options.
|
||||||
|
attrs := []interface{}{
|
||||||
|
messagingSystem,
|
||||||
|
semconv.MessagingDestinationKindTopic,
|
||||||
|
semconv.MessagingDestinationName(r.Topic),
|
||||||
|
semconv.MessagingOperationPublish,
|
||||||
|
}
|
||||||
|
attrs = maybeKeyAttr(attrs, r)
|
||||||
|
if m.clientID != "" {
|
||||||
|
attrs = append(attrs, semconv.MessagingKafkaClientIDKey.String(m.clientID))
|
||||||
|
}
|
||||||
|
opts := []tracer.SpanOption{
|
||||||
|
tracer.WithSpanLabels(attrs...),
|
||||||
|
tracer.WithSpanKind(tracer.SpanKindProducer),
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Context == nil {
|
||||||
|
r.Context = context.Background()
|
||||||
|
}
|
||||||
|
|
||||||
|
omd, ok := metadata.FromOutgoingContext(r.Context)
|
||||||
|
if !ok {
|
||||||
|
omd = metadata.New(len(r.Headers))
|
||||||
|
}
|
||||||
|
|
||||||
|
md := metadata.Copy(omd)
|
||||||
|
for _, h := range r.Headers {
|
||||||
|
md.Set(h.Key, string(h.Value))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
r.Context, _ = m.tracer.Start(metadata.NewOutgoingContext(r.Context, md), "sdk.broker", opts...)
|
||||||
|
} else {
|
||||||
|
r.Context, _ = m.tracer.Start(r.Context, "sdk.broker", opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
setHeaders(r, omd, metadata.HeaderContentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnProduceRecordUnbuffered continues and ends the "publish" span for an
|
||||||
|
// unbuffered record.
|
||||||
|
//
|
||||||
|
// It sets attributes with values unset when producing and records any error
|
||||||
|
// that occurred during the publish operation.
|
||||||
|
func (m *hookTracer) OnProduceRecordUnbuffered(r *kgo.Record, err error) {
|
||||||
|
if span, ok := tracer.SpanFromContext(r.Context); ok {
|
||||||
|
span.AddLabels(
|
||||||
|
semconv.MessagingKafkaDestinationPartition(int(r.Partition)),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
span.SetStatus(tracer.SpanStatusError, err.Error())
|
||||||
|
}
|
||||||
|
span.Finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnFetchRecordBuffered starts a new span for the "receive" operation on a
|
||||||
|
// buffered record.
|
||||||
|
//
|
||||||
|
// It sets the span options and extracts the span context from the record,
|
||||||
|
// updates the record's context to ensure it can be ended in the
|
||||||
|
// OnFetchRecordUnbuffered hook and can be used in downstream consumer
|
||||||
|
// processing.
|
||||||
|
func (m *hookTracer) OnFetchRecordBuffered(r *kgo.Record) {
|
||||||
|
// Set up the span options.
|
||||||
|
attrs := []interface{}{
|
||||||
|
messagingSystem,
|
||||||
|
semconv.MessagingSourceKindTopic,
|
||||||
|
semconv.MessagingSourceName(r.Topic),
|
||||||
|
semconv.MessagingOperationReceive,
|
||||||
|
semconv.MessagingKafkaSourcePartition(int(r.Partition)),
|
||||||
|
}
|
||||||
|
attrs = maybeKeyAttr(attrs, r)
|
||||||
|
if m.clientID != "" {
|
||||||
|
attrs = append(attrs, semconv.MessagingKafkaClientIDKey.String(m.clientID))
|
||||||
|
}
|
||||||
|
if m.group != "" {
|
||||||
|
attrs = append(attrs, semconv.MessagingKafkaConsumerGroupKey.String(m.group))
|
||||||
|
}
|
||||||
|
opts := []tracer.SpanOption{
|
||||||
|
tracer.WithSpanLabels(attrs...),
|
||||||
|
tracer.WithSpanKind(tracer.SpanKindConsumer),
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Context == nil {
|
||||||
|
r.Context = context.Background()
|
||||||
|
}
|
||||||
|
omd, ok := metadata.FromIncomingContext(r.Context)
|
||||||
|
if !ok {
|
||||||
|
omd = metadata.New(len(r.Headers))
|
||||||
|
}
|
||||||
|
|
||||||
|
md := metadata.Copy(omd)
|
||||||
|
for _, h := range r.Headers {
|
||||||
|
md.Set(h.Key, string(h.Value))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
r.Context, _ = m.tracer.Start(metadata.NewIncomingContext(r.Context, md), "sdk.broker", opts...)
|
||||||
|
} else {
|
||||||
|
r.Context, _ = m.tracer.Start(r.Context, "sdk.broker", opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
setHeaders(r, omd, metadata.HeaderContentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnFetchRecordUnbuffered continues and ends the "receive" span for an
|
||||||
|
// unbuffered record.
|
||||||
|
func (m *hookTracer) OnFetchRecordUnbuffered(r *kgo.Record, _ bool) {
|
||||||
|
span, _ := tracer.SpanFromContext(r.Context)
|
||||||
|
span.Finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithProcessSpan starts a new span for the "process" operation on a consumer
|
||||||
|
// record.
|
||||||
|
//
|
||||||
|
// It sets up the span options. The user's application code is responsible for
|
||||||
|
// ending the span.
|
||||||
|
//
|
||||||
|
// This should only ever be called within a polling loop of a consumed record and
|
||||||
|
// not a record which has been created for producing, so call this at the start of each
|
||||||
|
// iteration of your processing for the record.
|
||||||
|
func (m *hookTracer) WithProcessSpan(r *kgo.Record) (context.Context, tracer.Span) {
|
||||||
|
// Set up the span options.
|
||||||
|
attrs := []interface{}{
|
||||||
|
messagingSystem,
|
||||||
|
semconv.MessagingSourceKindTopic,
|
||||||
|
semconv.MessagingSourceName(r.Topic),
|
||||||
|
semconv.MessagingOperationProcess,
|
||||||
|
semconv.MessagingKafkaSourcePartition(int(r.Partition)),
|
||||||
|
semconv.MessagingKafkaMessageOffset(int(r.Offset)),
|
||||||
|
}
|
||||||
|
attrs = maybeKeyAttr(attrs, r)
|
||||||
|
if m.clientID != "" {
|
||||||
|
attrs = append(attrs, semconv.MessagingKafkaClientIDKey.String(m.clientID))
|
||||||
|
}
|
||||||
|
if m.group != "" {
|
||||||
|
attrs = append(attrs, semconv.MessagingKafkaConsumerGroupKey.String(m.group))
|
||||||
|
}
|
||||||
|
opts := []tracer.SpanOption{
|
||||||
|
tracer.WithSpanLabels(attrs...),
|
||||||
|
tracer.WithSpanKind(tracer.SpanKindConsumer),
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Context == nil {
|
||||||
|
r.Context = context.Background()
|
||||||
|
}
|
||||||
|
md, ok := metadata.FromIncomingContext(r.Context)
|
||||||
|
if !ok {
|
||||||
|
md = metadata.New(len(r.Headers))
|
||||||
|
}
|
||||||
|
for _, h := range r.Headers {
|
||||||
|
md.Set(h.Key, string(h.Value))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start a new span using the provided context and options.
|
||||||
|
return m.tracer.Start(r.Context, "sdk.broker", opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func maybeKeyAttr(attrs []interface{}, r *kgo.Record) []interface{} {
|
||||||
|
if r.Key == nil {
|
||||||
|
return attrs
|
||||||
|
}
|
||||||
|
var keykey string
|
||||||
|
if !utf8.Valid(r.Key) {
|
||||||
|
return attrs
|
||||||
|
}
|
||||||
|
keykey = string(r.Key)
|
||||||
|
return append(attrs, semconv.MessagingKafkaMessageKeyKey.String(keykey))
|
||||||
|
}
|
244
util.go
244
util.go
@@ -1,244 +0,0 @@
|
|||||||
package kgo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
kgo "github.com/twmb/franz-go/pkg/kgo"
|
|
||||||
"go.unistack.org/micro/v3/broker"
|
|
||||||
"go.unistack.org/micro/v3/logger"
|
|
||||||
"go.unistack.org/micro/v3/metadata"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ErrLostMessage = errors.New("message not marked for offsets commit and will be lost in next iteration")
|
|
||||||
|
|
||||||
var pPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
|
||||||
return &publication{msg: broker.NewMessage("")}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
type worker struct {
|
|
||||||
done chan struct{}
|
|
||||||
recs chan []*kgo.Record
|
|
||||||
cherr chan error
|
|
||||||
handler broker.Handler
|
|
||||||
batchHandler broker.BatchHandler
|
|
||||||
opts broker.SubscribeOptions
|
|
||||||
kopts broker.Options
|
|
||||||
tpmap map[string][]int32
|
|
||||||
maxInflight int
|
|
||||||
reader *kgo.Client
|
|
||||||
ctx context.Context
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) run(ctx context.Context) {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return
|
|
||||||
case <-s.kopts.Context.Done():
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
fetches := s.reader.PollFetches(ctx)
|
|
||||||
if fetches.IsClientClosed() {
|
|
||||||
// TODO: fatal ?
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(fetches.Errors()) > 0 {
|
|
||||||
for _, err := range fetches.Errors() {
|
|
||||||
s.kopts.Logger.Fatalf(ctx, "fetch err topic %s partition %d: %v", err.Topic, err.Partition, err.Err)
|
|
||||||
}
|
|
||||||
// TODO: fatal ?
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
fetches.EachPartition(func(p kgo.FetchTopicPartition) {
|
|
||||||
s.Lock()
|
|
||||||
consumers := s.consumers[p.Topic]
|
|
||||||
s.Unlock()
|
|
||||||
if consumers == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w, ok := consumers[p.Partition]
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case err := <-w.cherr:
|
|
||||||
s.kopts.Logger.Fatalf(ctx, "handle err: %v", err)
|
|
||||||
return
|
|
||||||
case w.recs <- p.Records:
|
|
||||||
case <-w.done:
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) assigned(ctx context.Context, _ *kgo.Client, assigned map[string][]int32) {
|
|
||||||
maxInflight := DefaultSubscribeMaxInflight
|
|
||||||
|
|
||||||
if s.opts.Context != nil {
|
|
||||||
if n, ok := s.opts.Context.Value(subscribeMaxInflightKey{}).(int); n > 0 && ok {
|
|
||||||
maxInflight = n
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s.Lock()
|
|
||||||
for topic, partitions := range assigned {
|
|
||||||
if s.consumers[topic] == nil {
|
|
||||||
s.consumers[topic] = make(map[int32]worker)
|
|
||||||
}
|
|
||||||
for _, partition := range partitions {
|
|
||||||
w := worker{
|
|
||||||
done: make(chan struct{}),
|
|
||||||
recs: make(chan []*kgo.Record),
|
|
||||||
cherr: make(chan error),
|
|
||||||
kopts: s.kopts,
|
|
||||||
opts: s.opts,
|
|
||||||
ctx: ctx,
|
|
||||||
tpmap: map[string][]int32{topic: []int32{partition}},
|
|
||||||
reader: s.reader,
|
|
||||||
handler: s.handler,
|
|
||||||
batchHandler: s.batchhandler,
|
|
||||||
maxInflight: maxInflight,
|
|
||||||
}
|
|
||||||
s.consumers[topic][partition] = w
|
|
||||||
go w.handle()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *subscriber) revoked(_ context.Context, _ *kgo.Client, revoked map[string][]int32) {
|
|
||||||
s.Lock()
|
|
||||||
for topic, partitions := range revoked {
|
|
||||||
ptopics := s.consumers[topic]
|
|
||||||
for _, partition := range partitions {
|
|
||||||
w := ptopics[partition]
|
|
||||||
delete(ptopics, partition)
|
|
||||||
if len(ptopics) == 0 {
|
|
||||||
delete(s.consumers, topic)
|
|
||||||
}
|
|
||||||
close(w.done)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
s.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *worker) handle() {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
eh := w.kopts.ErrorHandler
|
|
||||||
if w.opts.ErrorHandler != nil {
|
|
||||||
eh = w.opts.ErrorHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
paused := false
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-w.ctx.Done():
|
|
||||||
w.cherr <- w.ctx.Err()
|
|
||||||
return
|
|
||||||
case <-w.done:
|
|
||||||
return
|
|
||||||
case recs := <-w.recs:
|
|
||||||
if len(recs) >= w.maxInflight {
|
|
||||||
paused = true
|
|
||||||
w.reader.PauseFetchPartitions(w.tpmap)
|
|
||||||
}
|
|
||||||
for _, record := range recs {
|
|
||||||
p := pPool.Get().(*publication)
|
|
||||||
p.msg.Header = nil
|
|
||||||
p.msg.Body = nil
|
|
||||||
p.topic = record.Topic
|
|
||||||
p.err = nil
|
|
||||||
p.ack = false
|
|
||||||
if w.opts.BodyOnly {
|
|
||||||
p.msg.Body = record.Value
|
|
||||||
if l := len(record.Headers); l > 0 {
|
|
||||||
if p.msg.Header == nil {
|
|
||||||
p.msg.Header = metadata.New(l)
|
|
||||||
}
|
|
||||||
for _, h := range record.Headers {
|
|
||||||
p.msg.Header.Set(h.Key, string(h.Value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if w.kopts.Codec.String() == "noop" {
|
|
||||||
p.msg.Body = record.Value
|
|
||||||
p.msg.Header = metadata.New(len(record.Headers))
|
|
||||||
for _, h := range record.Headers {
|
|
||||||
p.msg.Header.Set(h.Key, string(h.Value))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err := w.kopts.Codec.Unmarshal(record.Value, p.msg); err != nil {
|
|
||||||
p.err = err
|
|
||||||
p.msg.Body = record.Value
|
|
||||||
if l := len(record.Headers); l > 0 {
|
|
||||||
if p.msg.Header == nil {
|
|
||||||
p.msg.Header = metadata.New(l)
|
|
||||||
}
|
|
||||||
for _, h := range record.Headers {
|
|
||||||
p.msg.Header.Set(h.Key, string(h.Value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if eh != nil {
|
|
||||||
_ = eh(p)
|
|
||||||
if p.ack {
|
|
||||||
w.reader.MarkCommitRecords(record)
|
|
||||||
} else {
|
|
||||||
w.cherr <- ErrLostMessage
|
|
||||||
pPool.Put(p)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
pPool.Put(p)
|
|
||||||
continue
|
|
||||||
} else {
|
|
||||||
if w.kopts.Logger.V(logger.ErrorLevel) {
|
|
||||||
w.kopts.Logger.Errorf(w.kopts.Context, "[kgo]: failed to unmarshal: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pPool.Put(p)
|
|
||||||
w.cherr <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if l := len(record.Headers); l > 0 {
|
|
||||||
if p.msg.Header == nil {
|
|
||||||
p.msg.Header = metadata.New(l)
|
|
||||||
}
|
|
||||||
for _, h := range record.Headers {
|
|
||||||
p.msg.Header.Set(h.Key, string(h.Value))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = w.handler(p)
|
|
||||||
if err == nil && w.opts.AutoAck {
|
|
||||||
p.ack = true
|
|
||||||
} else if err != nil {
|
|
||||||
p.err = err
|
|
||||||
if eh != nil {
|
|
||||||
_ = eh(p)
|
|
||||||
} else {
|
|
||||||
if w.kopts.Logger.V(logger.ErrorLevel) {
|
|
||||||
w.kopts.Logger.Errorf(w.kopts.Context, "[kgo]: subscriber error: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if p.ack {
|
|
||||||
pPool.Put(p)
|
|
||||||
w.reader.MarkCommitRecords(record)
|
|
||||||
} else {
|
|
||||||
pPool.Put(p)
|
|
||||||
w.cherr <- ErrLostMessage
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if paused {
|
|
||||||
paused = false
|
|
||||||
w.reader.ResumeFetchPartitions(w.tpmap)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user