Compare commits
	
		
			21 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| ffd10d7ea9 | |||
| 
						 | 
					c4f8de4aca | ||
| 63d82be92a | |||
| cced3b84c1 | |||
| 
						 | 
					82c1bda50f | ||
| 4b0b70e7d0 | |||
| 
						 | 
					da88718e03 | ||
| aed304b7e3 | |||
| b796c43257 | |||
| b0691edf82 | |||
| d43b59f2f3 | |||
| f976516d6b | |||
| 8f5c03ad60 | |||
| 
						 | 
					9e9c1434ff | ||
| fe81a14413 | |||
| eb56a48dda | |||
| 71e8a13c70 | |||
| b538ef82b5 | |||
| 44e2d5f9a4 | |||
| 
						 | 
					9426208e1c | ||
| d6e73a3419 | 
							
								
								
									
										7
									
								
								.github/workflows/job_coverage.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								.github/workflows/job_coverage.yml
									
									
									
									
										vendored
									
									
								
							@@ -8,12 +8,11 @@ on:
 | 
				
			|||||||
      - '.gitea/**'
 | 
					      - '.gitea/**'
 | 
				
			||||||
  pull_request:
 | 
					  pull_request:
 | 
				
			||||||
    branches: [ main, v3, v4 ]
 | 
					    branches: [ main, v3, v4 ]
 | 
				
			||||||
  # Allows you to run this workflow manually from the Actions tab
 | 
					 | 
				
			||||||
  workflow_dispatch:
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
jobs:
 | 
					jobs:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  build:
 | 
					  build:
 | 
				
			||||||
 | 
					    if: github.server_url != 'https://github.com'
 | 
				
			||||||
    runs-on: ubuntu-latest
 | 
					    runs-on: ubuntu-latest
 | 
				
			||||||
    steps:
 | 
					    steps:
 | 
				
			||||||
    - name: checkout code
 | 
					    - name: checkout code
 | 
				
			||||||
@@ -42,8 +41,8 @@ jobs:
 | 
				
			|||||||
      name: autocommit
 | 
					      name: autocommit
 | 
				
			||||||
      with:
 | 
					      with:
 | 
				
			||||||
        commit_message: Apply Code Coverage Badge
 | 
					        commit_message: Apply Code Coverage Badge
 | 
				
			||||||
        skip_fetch: true
 | 
					        skip_fetch: false
 | 
				
			||||||
        skip_checkout: true
 | 
					        skip_checkout: false
 | 
				
			||||||
        file_pattern: ./README.md
 | 
					        file_pattern: ./README.md
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    - name: push
 | 
					    - name: push
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								.github/workflows/job_lint.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/job_lint.yml
									
									
									
									
										vendored
									
									
								
							@@ -24,6 +24,6 @@ jobs:
 | 
				
			|||||||
    - name: setup deps
 | 
					    - name: setup deps
 | 
				
			||||||
      run: go get -v ./...
 | 
					      run: go get -v ./...
 | 
				
			||||||
    - name: run lint
 | 
					    - name: run lint
 | 
				
			||||||
      uses: https://github.com/golangci/golangci-lint-action@v6
 | 
					      uses: golangci/golangci-lint-action@v6
 | 
				
			||||||
      with:
 | 
					      with:
 | 
				
			||||||
        version: 'latest'
 | 
					        version: 'latest'
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										50
									
								
								.github/workflows/job_sync.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										50
									
								
								.github/workflows/job_sync.yml
									
									
									
									
										vendored
									
									
								
							@@ -8,7 +8,7 @@ on:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
jobs:
 | 
					jobs:
 | 
				
			||||||
  sync:
 | 
					  sync:
 | 
				
			||||||
    if: github.server_url == 'zhttps://github.com'
 | 
					    if: github.server_url != 'https://github.com'
 | 
				
			||||||
    runs-on: ubuntu-latest
 | 
					    runs-on: ubuntu-latest
 | 
				
			||||||
    steps:
 | 
					    steps:
 | 
				
			||||||
    - name: init
 | 
					    - name: init
 | 
				
			||||||
@@ -18,34 +18,76 @@ jobs:
 | 
				
			|||||||
        echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
 | 
					        echo "machine git.unistack.org login vtolstov password ${{ secrets.TOKEN_GITEA }}" >> /root/.netrc
 | 
				
			||||||
        echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
 | 
					        echo "machine github.com login vtolstov password ${{ secrets.TOKEN_GITHUB }}" >> /root/.netrc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    - name: check master
 | 
				
			||||||
 | 
					      id: check_master
 | 
				
			||||||
 | 
					      run: |
 | 
				
			||||||
 | 
					        src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
 | 
				
			||||||
 | 
					        dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/master | cut -f1)
 | 
				
			||||||
 | 
					        echo "src_hash=$src_hash"
 | 
				
			||||||
 | 
					        echo "dst_hash=$dst_hash"
 | 
				
			||||||
 | 
					        if [ "$src_hash" != "$dst_hash" ]; then
 | 
				
			||||||
 | 
					          echo "sync_needed=true" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					          echo "sync_needed=false" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    - name: sync master
 | 
					    - name: sync master
 | 
				
			||||||
 | 
					      if: steps.check_master.outputs.sync_needed == 'true'
 | 
				
			||||||
      run: |
 | 
					      run: |
 | 
				
			||||||
        git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
 | 
					        git clone --filter=blob:none --filter=tree:0 --branch master --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
 | 
				
			||||||
        cd repo
 | 
					        cd repo
 | 
				
			||||||
        git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
 | 
					        git remote add --no-tags --fetch --track master upstream https://github.com/${GITHUB_REPOSITORY}
 | 
				
			||||||
        git merge upstream/master
 | 
					        git pull --rebase upstream master
 | 
				
			||||||
        git push upstream master --progress
 | 
					        git push upstream master --progress
 | 
				
			||||||
        git push origin master --progress
 | 
					        git push origin master --progress
 | 
				
			||||||
        cd ../
 | 
					        cd ../
 | 
				
			||||||
        rm -rf repo
 | 
					        rm -rf repo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    - name: check v3
 | 
				
			||||||
 | 
					      id: check_v3
 | 
				
			||||||
 | 
					      run: |
 | 
				
			||||||
 | 
					        src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
 | 
				
			||||||
 | 
					        dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v3 | cut -f1)
 | 
				
			||||||
 | 
					        echo "src_hash=$src_hash"
 | 
				
			||||||
 | 
					        echo "dst_hash=$dst_hash"
 | 
				
			||||||
 | 
					        if [ "$src_hash" != "$dst_hash" ]; then
 | 
				
			||||||
 | 
					          echo "sync_needed=true" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					          echo "sync_needed=false" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    - name: sync v3
 | 
					    - name: sync v3
 | 
				
			||||||
 | 
					      if: steps.check_v3.outputs.sync_needed == 'true'
 | 
				
			||||||
      run: |
 | 
					      run: |
 | 
				
			||||||
        git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
 | 
					        git clone --filter=blob:none --filter=tree:0 --branch v3 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
 | 
				
			||||||
        cd repo
 | 
					        cd repo
 | 
				
			||||||
        git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
 | 
					        git remote add --no-tags --fetch --track v3 upstream https://github.com/${GITHUB_REPOSITORY}
 | 
				
			||||||
        git merge upstream/v3
 | 
					        git pull --rebase upstream v3
 | 
				
			||||||
        git push upstream v3 --progress
 | 
					        git push upstream v3 --progress
 | 
				
			||||||
        git push origin v3 --progress
 | 
					        git push origin v3 --progress
 | 
				
			||||||
        cd ../
 | 
					        cd ../
 | 
				
			||||||
        rm -rf repo
 | 
					        rm -rf repo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    - name: check v4
 | 
				
			||||||
 | 
					      id: check_v4
 | 
				
			||||||
 | 
					      run: |
 | 
				
			||||||
 | 
					        src_hash=$(git ls-remote https://github.com/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
 | 
				
			||||||
 | 
					        dst_hash=$(git ls-remote ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} refs/heads/v4 | cut -f1)
 | 
				
			||||||
 | 
					        echo "src_hash=$src_hash"
 | 
				
			||||||
 | 
					        echo "dst_hash=$dst_hash"
 | 
				
			||||||
 | 
					        if [ "$src_hash" != "$dst_hash" ]; then
 | 
				
			||||||
 | 
					          echo "sync_needed=true" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					          echo "sync_needed=false" >> $GITHUB_OUTPUT
 | 
				
			||||||
 | 
					        fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    - name: sync v4
 | 
					    - name: sync v4
 | 
				
			||||||
 | 
					      if: steps.check_v4.outputs.sync_needed == 'true'
 | 
				
			||||||
      run: |
 | 
					      run: |
 | 
				
			||||||
        git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
 | 
					        git clone --filter=blob:none --filter=tree:0 --branch v4 --single-branch ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY} repo
 | 
				
			||||||
        cd repo
 | 
					        cd repo
 | 
				
			||||||
        git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
 | 
					        git remote add --no-tags --fetch --track v4 upstream https://github.com/${GITHUB_REPOSITORY}
 | 
				
			||||||
        git merge upstream/v4
 | 
					        git pull --rebase upstream v4
 | 
				
			||||||
        git push upstream v4 --progress
 | 
					        git push upstream v4 --progress
 | 
				
			||||||
        git push origin v4 --progress
 | 
					        git push origin v4 --progress
 | 
				
			||||||
        cd ../
 | 
					        cd ../
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										6
									
								
								.github/workflows/job_tests.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								.github/workflows/job_tests.yml
									
									
									
									
										vendored
									
									
								
							@@ -32,19 +32,19 @@ jobs:
 | 
				
			|||||||
        go-version: 'stable'
 | 
					        go-version: 'stable'
 | 
				
			||||||
    - name: setup go work
 | 
					    - name: setup go work
 | 
				
			||||||
      env:
 | 
					      env:
 | 
				
			||||||
        GOWORK: /workspace/${{ github.repository_owner }}/go.work
 | 
					        GOWORK: ${{ github.workspace }}/go.work
 | 
				
			||||||
      run: |
 | 
					      run: |
 | 
				
			||||||
        go work init
 | 
					        go work init
 | 
				
			||||||
        go work use .
 | 
					        go work use .
 | 
				
			||||||
        go work use micro-tests
 | 
					        go work use micro-tests
 | 
				
			||||||
    - name: setup deps
 | 
					    - name: setup deps
 | 
				
			||||||
      env:
 | 
					      env:
 | 
				
			||||||
        GOWORK: /workspace/${{ github.repository_owner }}/go.work
 | 
					        GOWORK: ${{ github.workspace }}/go.work
 | 
				
			||||||
      run: go get -v ./...
 | 
					      run: go get -v ./...
 | 
				
			||||||
    - name: run tests
 | 
					    - name: run tests
 | 
				
			||||||
      env:
 | 
					      env:
 | 
				
			||||||
        INTEGRATION_TESTS: yes
 | 
					        INTEGRATION_TESTS: yes
 | 
				
			||||||
        GOWORK: /workspace/${{ github.repository_owner }}/go.work
 | 
					        GOWORK: ${{ github.workspace }}/go.work
 | 
				
			||||||
      run: |
 | 
					      run: |
 | 
				
			||||||
        cd micro-tests
 | 
					        cd micro-tests
 | 
				
			||||||
        go test -mod readonly -v ./... || true
 | 
					        go test -mod readonly -v ./... || true
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,2 +1,2 @@
 | 
				
			|||||||
# micro-broker-kgo
 | 
					# micro-broker-kgo
 | 
				
			||||||

 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								errors.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								errors.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					package kgo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func isContextError(err error) bool {
 | 
				
			||||||
 | 
						return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										20
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,23 +1,27 @@
 | 
				
			|||||||
module go.unistack.org/micro-broker-kgo/v4
 | 
					module go.unistack.org/micro-broker-kgo/v4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.23.8
 | 
					go 1.24.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/twmb/franz-go v1.19.1
 | 
						github.com/stretchr/testify v1.11.1
 | 
				
			||||||
	github.com/twmb/franz-go/pkg/kadm v1.16.0
 | 
						github.com/twmb/franz-go v1.19.5
 | 
				
			||||||
 | 
						github.com/twmb/franz-go/pkg/kadm v1.16.1
 | 
				
			||||||
	github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3
 | 
						github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3
 | 
				
			||||||
	github.com/twmb/franz-go/pkg/kmsg v1.11.2
 | 
						github.com/twmb/franz-go/pkg/kmsg v1.11.2
 | 
				
			||||||
	go.opentelemetry.io/otel v1.35.0
 | 
						go.opentelemetry.io/otel v1.38.0
 | 
				
			||||||
	go.unistack.org/micro/v4 v4.1.14
 | 
						go.unistack.org/micro/v4 v4.1.21
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
 | 
						github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
 | 
				
			||||||
	github.com/google/uuid v1.6.0 // indirect
 | 
						github.com/google/uuid v1.6.0 // indirect
 | 
				
			||||||
	github.com/klauspost/compress v1.18.0 // indirect
 | 
						github.com/klauspost/compress v1.18.0 // indirect
 | 
				
			||||||
	github.com/matoous/go-nanoid v1.5.1 // indirect
 | 
						github.com/matoous/go-nanoid v1.5.1 // indirect
 | 
				
			||||||
	github.com/pierrec/lz4/v4 v4.1.22 // indirect
 | 
						github.com/pierrec/lz4/v4 v4.1.22 // indirect
 | 
				
			||||||
	github.com/spf13/cast v1.8.0 // indirect
 | 
						github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
 | 
				
			||||||
 | 
						github.com/spf13/cast v1.10.0 // indirect
 | 
				
			||||||
	go.unistack.org/micro-proto/v4 v4.1.0 // indirect
 | 
						go.unistack.org/micro-proto/v4 v4.1.0 // indirect
 | 
				
			||||||
	golang.org/x/crypto v0.38.0 // indirect
 | 
						golang.org/x/crypto v0.43.0 // indirect
 | 
				
			||||||
	google.golang.org/protobuf v1.36.6 // indirect
 | 
						google.golang.org/protobuf v1.36.10 // indirect
 | 
				
			||||||
 | 
						gopkg.in/yaml.v3 v3.0.1 // indirect
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										35
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								go.sum
									
									
									
									
									
								
							@@ -20,27 +20,30 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
 | 
				
			|||||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
					github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
				
			||||||
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
 | 
					github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
 | 
				
			||||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
 | 
					github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
 | 
				
			||||||
github.com/spf13/cast v1.8.0 h1:gEN9K4b8Xws4EX0+a0reLmhq8moKn7ntRlQYgjPeCDk=
 | 
					github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
 | 
				
			||||||
github.com/spf13/cast v1.8.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
 | 
					github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
 | 
				
			||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
 | 
					github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
 | 
				
			||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 | 
					github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
 | 
				
			||||||
github.com/twmb/franz-go v1.19.1 h1:cOhDFUkGvUFHSQ7UYW6bO77BJa2fYEk5mA2AX+1NIdE=
 | 
					github.com/twmb/franz-go v1.19.5 h1:W7+o8D0RsQsedqib71OVlLeZ0zI6CbFra7yTYhZTs5Y=
 | 
				
			||||||
github.com/twmb/franz-go v1.19.1/go.mod h1:4kFJ5tmbbl7asgwAGVuyG1ZMx0NNpYk7EqflvWfPCpM=
 | 
					github.com/twmb/franz-go v1.19.5/go.mod h1:4kFJ5tmbbl7asgwAGVuyG1ZMx0NNpYk7EqflvWfPCpM=
 | 
				
			||||||
github.com/twmb/franz-go/pkg/kadm v1.16.0 h1:STMs1t5lYR5mR974PSiwNzE5TvsosByTp+rKXLOhAjE=
 | 
					github.com/twmb/franz-go/pkg/kadm v1.16.1 h1:IEkrhTljgLHJ0/hT/InhXGjPdmWfFvxp7o/MR7vJ8cw=
 | 
				
			||||||
github.com/twmb/franz-go/pkg/kadm v1.16.0/go.mod h1:MUdcUtnf9ph4SFBLLA/XxE29rvLhWYLM9Ygb8dfSCvw=
 | 
					github.com/twmb/franz-go/pkg/kadm v1.16.1/go.mod h1:Ue/ye1cc9ipsQFg7udFbbGiFNzQMqiH73fGC2y0rwyc=
 | 
				
			||||||
github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3 h1:p24opKWPySAy8xSl8NqRgOv7Q+bX7kdrQirBVRJzQfo=
 | 
					github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3 h1:p24opKWPySAy8xSl8NqRgOv7Q+bX7kdrQirBVRJzQfo=
 | 
				
			||||||
github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3/go.mod h1:7uQs3Ae6HkWT1Y9elMbqtAcNFCI0y6+iS+Phw49L49U=
 | 
					github.com/twmb/franz-go/pkg/kfake v0.0.0-20250508175730-72e1646135e3/go.mod h1:7uQs3Ae6HkWT1Y9elMbqtAcNFCI0y6+iS+Phw49L49U=
 | 
				
			||||||
github.com/twmb/franz-go/pkg/kmsg v1.11.2 h1:hIw75FpwcAjgeyfIGFqivAvwC5uNIOWRGvQgZhH4mhg=
 | 
					github.com/twmb/franz-go/pkg/kmsg v1.11.2 h1:hIw75FpwcAjgeyfIGFqivAvwC5uNIOWRGvQgZhH4mhg=
 | 
				
			||||||
github.com/twmb/franz-go/pkg/kmsg v1.11.2/go.mod h1:CFfkkLysDNmukPYhGzuUcDtf46gQSqCZHMW1T4Z+wDE=
 | 
					github.com/twmb/franz-go/pkg/kmsg v1.11.2/go.mod h1:CFfkkLysDNmukPYhGzuUcDtf46gQSqCZHMW1T4Z+wDE=
 | 
				
			||||||
go.opentelemetry.io/otel v1.35.0 h1:xKWKPxrxB6OtMCbmMY021CqC45J+3Onta9MqjhnusiQ=
 | 
					go.opentelemetry.io/otel v1.38.0 h1:RkfdswUDRimDg0m2Az18RKOsnI8UDzppJAtj01/Ymk8=
 | 
				
			||||||
go.opentelemetry.io/otel v1.35.0/go.mod h1:UEqy8Zp11hpkUrL73gSlELM0DupHoiq72dR+Zqel/+Y=
 | 
					go.opentelemetry.io/otel v1.38.0/go.mod h1:zcmtmQ1+YmQM9wrNsTGV/q/uyusom3P8RxwExxkZhjM=
 | 
				
			||||||
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
 | 
					go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
 | 
				
			||||||
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
 | 
					go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
 | 
				
			||||||
go.unistack.org/micro/v4 v4.1.14 h1:6EotPq9kz/gaFb5YulHdKuuUwmj/7Hk44DpOlzh/A6k=
 | 
					go.unistack.org/micro/v4 v4.1.21 h1:F9PrbI1BhXSDS0FopwcO5wWrT2Xh38w9VhVZZBHjfg8=
 | 
				
			||||||
go.unistack.org/micro/v4 v4.1.14/go.mod h1:xleO2M5Yxh4s6I+RUcLrEpUjobefh+71ctrdIfn7TUs=
 | 
					go.unistack.org/micro/v4 v4.1.21/go.mod h1:nlBXTbx0rQrSZX4HPp2m57PHmpuGPWUd0O+jpUIiPto=
 | 
				
			||||||
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
 | 
					golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
 | 
				
			||||||
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
 | 
					golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
 | 
				
			||||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
 | 
					google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
 | 
				
			||||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
 | 
					google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
 | 
				
			||||||
 | 
					gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
				
			||||||
 | 
					gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
 | 
				
			||||||
 | 
					gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
					gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
				
			||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
					gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,50 +26,83 @@ var (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *hookEvent) OnGroupManageError(err error) {
 | 
					func (m *hookEvent) OnGroupManageError(err error) {
 | 
				
			||||||
	if err != nil && !kgo.IsRetryableBrokerErr(err) {
 | 
						switch {
 | 
				
			||||||
		m.connected.Store(0)
 | 
						case err == nil || isContextError(err) || kgo.IsRetryableBrokerErr(err):
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							ctx := context.TODO()
 | 
				
			||||||
 | 
							logMsg := "kgo.OnGroupManageError"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if m.fatalOnError {
 | 
							if m.fatalOnError {
 | 
				
			||||||
			m.log.Fatal(context.TODO(), "kgo.OnGroupManageError", err)
 | 
								m.log.Fatal(ctx, logMsg, err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								m.log.Error(ctx, logMsg, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *hookEvent) OnBrokerConnect(_ kgo.BrokerMetadata, _ time.Duration, _ net.Conn, err error) {
 | 
					func (m *hookEvent) OnBrokerConnect(_ kgo.BrokerMetadata, _ time.Duration, _ net.Conn, err error) {
 | 
				
			||||||
	if err != nil && !kgo.IsRetryableBrokerErr(err) {
 | 
						switch {
 | 
				
			||||||
		m.connected.Store(0)
 | 
						case err == nil || isContextError(err) || kgo.IsRetryableBrokerErr(err):
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							ctx := context.TODO()
 | 
				
			||||||
 | 
							logMsg := "kgo.OnBrokerConnect"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if m.fatalOnError {
 | 
							if m.fatalOnError {
 | 
				
			||||||
			m.log.Fatal(context.TODO(), "kgo.OnBrokerConnect", err)
 | 
								m.log.Fatal(ctx, logMsg, err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								m.log.Error(ctx, logMsg, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *hookEvent) OnBrokerDisconnect(_ kgo.BrokerMetadata, _ net.Conn) {
 | 
					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) {
 | 
					func (m *hookEvent) OnBrokerWrite(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
 | 
				
			||||||
	if err != nil && !kgo.IsRetryableBrokerErr(err) {
 | 
						switch {
 | 
				
			||||||
		m.connected.Store(0)
 | 
						case err == nil || isContextError(err) || kgo.IsRetryableBrokerErr(err):
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							ctx := context.TODO()
 | 
				
			||||||
 | 
							logMsg := "kgo.OnBrokerWrite"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if m.fatalOnError {
 | 
							if m.fatalOnError {
 | 
				
			||||||
			m.log.Fatal(context.TODO(), "kgo.OnBrokerWrite", err)
 | 
								m.log.Fatal(ctx, logMsg, err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								m.log.Error(ctx, logMsg, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *hookEvent) OnBrokerRead(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
 | 
					func (m *hookEvent) OnBrokerRead(_ kgo.BrokerMetadata, _ int16, _ int, _ time.Duration, _ time.Duration, err error) {
 | 
				
			||||||
	if err != nil && !kgo.IsRetryableBrokerErr(err) {
 | 
						switch {
 | 
				
			||||||
		m.connected.Store(0)
 | 
						case err == nil || isContextError(err) || kgo.IsRetryableBrokerErr(err):
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							ctx := context.TODO()
 | 
				
			||||||
 | 
							logMsg := "kgo.OnBrokerRead"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if m.fatalOnError {
 | 
							if m.fatalOnError {
 | 
				
			||||||
			m.log.Fatal(context.TODO(), "kgo.OnBrokerRead", err)
 | 
								m.log.Fatal(ctx, logMsg, err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								m.log.Error(ctx, logMsg, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (m *hookEvent) OnProduceRecordUnbuffered(_ *kgo.Record, err error) {
 | 
					func (m *hookEvent) OnProduceRecordUnbuffered(_ *kgo.Record, err error) {
 | 
				
			||||||
	if err != nil && !kgo.IsRetryableBrokerErr(err) {
 | 
						switch {
 | 
				
			||||||
		m.connected.Store(0)
 | 
						case err == nil || isContextError(err) || kgo.IsRetryableBrokerErr(err):
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							ctx := context.TODO()
 | 
				
			||||||
 | 
							logMsg := "kgo.OnProduceRecordUnbuffered"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if m.fatalOnError {
 | 
							if m.fatalOnError {
 | 
				
			||||||
			m.log.Fatal(context.TODO(), "kgo.OnProduceRecordUnbuffered", err)
 | 
								m.log.Fatal(ctx, logMsg, err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								m.log.Error(ctx, logMsg, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										471
									
								
								hook_event_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										471
									
								
								hook_event_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,471 @@
 | 
				
			|||||||
 | 
					package kgo
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"net"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/stretchr/testify/require"
 | 
				
			||||||
 | 
						"github.com/twmb/franz-go/pkg/kgo"
 | 
				
			||||||
 | 
						"go.unistack.org/micro/v4/logger"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHookEvent_OnGroupManageError(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name                  string
 | 
				
			||||||
 | 
							inputErr              error
 | 
				
			||||||
 | 
							fatalOnError          bool
 | 
				
			||||||
 | 
							expectedErrorIsCalled bool
 | 
				
			||||||
 | 
							expectedErrorMsg      string
 | 
				
			||||||
 | 
							expectedFatalIsCalled bool
 | 
				
			||||||
 | 
							expectedFatalMsg      string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "error is nil",
 | 
				
			||||||
 | 
								inputErr:              nil,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context canceled",
 | 
				
			||||||
 | 
								inputErr:              context.Canceled,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context deadline exceeded",
 | 
				
			||||||
 | 
								inputErr:              context.DeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: deadline exceeded (os package)",
 | 
				
			||||||
 | 
								inputErr:              os.ErrDeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: EOF (io package)",
 | 
				
			||||||
 | 
								inputErr:              io.EOF,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: closed network connection (net package)",
 | 
				
			||||||
 | 
								inputErr:              net.ErrClosed,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (non-fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          false,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: true,
 | 
				
			||||||
 | 
								expectedErrorMsg:      "kgo.OnGroupManageError",
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          true,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: true,
 | 
				
			||||||
 | 
								expectedFatalMsg:      "kgo.OnGroupManageError",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								log := &mockLogger{}
 | 
				
			||||||
 | 
								he := &hookEvent{log: log, fatalOnError: tt.fatalOnError}
 | 
				
			||||||
 | 
								he.OnGroupManageError(tt.inputErr)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorIsCalled, log.errorIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorMsg, log.errorMsg)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalIsCalled, log.fatalIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalMsg, log.fatalMsg)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHookEvent_OnBrokerConnect(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name                  string
 | 
				
			||||||
 | 
							inputErr              error
 | 
				
			||||||
 | 
							fatalOnError          bool
 | 
				
			||||||
 | 
							expectedErrorIsCalled bool
 | 
				
			||||||
 | 
							expectedErrorMsg      string
 | 
				
			||||||
 | 
							expectedFatalIsCalled bool
 | 
				
			||||||
 | 
							expectedFatalMsg      string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "error is nil",
 | 
				
			||||||
 | 
								inputErr:              nil,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context canceled",
 | 
				
			||||||
 | 
								inputErr:              context.Canceled,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context deadline exceeded",
 | 
				
			||||||
 | 
								inputErr:              context.DeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: deadline exceeded (os package)",
 | 
				
			||||||
 | 
								inputErr:              os.ErrDeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: EOF (io package)",
 | 
				
			||||||
 | 
								inputErr:              io.EOF,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: closed network connection (net package)",
 | 
				
			||||||
 | 
								inputErr:              net.ErrClosed,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (non-fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          false,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: true,
 | 
				
			||||||
 | 
								expectedErrorMsg:      "kgo.OnBrokerConnect",
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          true,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: true,
 | 
				
			||||||
 | 
								expectedFatalMsg:      "kgo.OnBrokerConnect",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								log := &mockLogger{}
 | 
				
			||||||
 | 
								he := &hookEvent{log: log, fatalOnError: tt.fatalOnError}
 | 
				
			||||||
 | 
								he.OnBrokerConnect(kgo.BrokerMetadata{}, 0, nil, tt.inputErr)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorIsCalled, log.errorIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorMsg, log.errorMsg)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalIsCalled, log.fatalIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalMsg, log.fatalMsg)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHookEvent_OnBrokerWrite(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name                  string
 | 
				
			||||||
 | 
							inputErr              error
 | 
				
			||||||
 | 
							fatalOnError          bool
 | 
				
			||||||
 | 
							expectedErrorIsCalled bool
 | 
				
			||||||
 | 
							expectedErrorMsg      string
 | 
				
			||||||
 | 
							expectedFatalIsCalled bool
 | 
				
			||||||
 | 
							expectedFatalMsg      string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "error is nil",
 | 
				
			||||||
 | 
								inputErr:              nil,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context canceled",
 | 
				
			||||||
 | 
								inputErr:              context.Canceled,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context deadline exceeded",
 | 
				
			||||||
 | 
								inputErr:              context.DeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: deadline exceeded (os package)",
 | 
				
			||||||
 | 
								inputErr:              os.ErrDeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: EOF (io package)",
 | 
				
			||||||
 | 
								inputErr:              io.EOF,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: closed network connection (net package)",
 | 
				
			||||||
 | 
								inputErr:              net.ErrClosed,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (non-fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          false,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: true,
 | 
				
			||||||
 | 
								expectedErrorMsg:      "kgo.OnBrokerWrite",
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          true,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: true,
 | 
				
			||||||
 | 
								expectedFatalMsg:      "kgo.OnBrokerWrite",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								log := &mockLogger{}
 | 
				
			||||||
 | 
								he := &hookEvent{log: log, fatalOnError: tt.fatalOnError}
 | 
				
			||||||
 | 
								he.OnBrokerWrite(kgo.BrokerMetadata{}, 0, 0, 0, 0, tt.inputErr)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorIsCalled, log.errorIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorMsg, log.errorMsg)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalIsCalled, log.fatalIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalMsg, log.fatalMsg)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHookEvent_OnBrokerRead(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name                  string
 | 
				
			||||||
 | 
							inputErr              error
 | 
				
			||||||
 | 
							fatalOnError          bool
 | 
				
			||||||
 | 
							expectedErrorIsCalled bool
 | 
				
			||||||
 | 
							expectedErrorMsg      string
 | 
				
			||||||
 | 
							expectedFatalIsCalled bool
 | 
				
			||||||
 | 
							expectedFatalMsg      string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "error is nil",
 | 
				
			||||||
 | 
								inputErr:              nil,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context canceled",
 | 
				
			||||||
 | 
								inputErr:              context.Canceled,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context deadline exceeded",
 | 
				
			||||||
 | 
								inputErr:              context.DeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: deadline exceeded (os package)",
 | 
				
			||||||
 | 
								inputErr:              os.ErrDeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: EOF (io package)",
 | 
				
			||||||
 | 
								inputErr:              io.EOF,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: closed network connection (net package)",
 | 
				
			||||||
 | 
								inputErr:              net.ErrClosed,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (non-fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          false,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: true,
 | 
				
			||||||
 | 
								expectedErrorMsg:      "kgo.OnBrokerRead",
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          true,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: true,
 | 
				
			||||||
 | 
								expectedFatalMsg:      "kgo.OnBrokerRead",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								log := &mockLogger{}
 | 
				
			||||||
 | 
								he := &hookEvent{log: log, fatalOnError: tt.fatalOnError}
 | 
				
			||||||
 | 
								he.OnBrokerRead(kgo.BrokerMetadata{}, 0, 0, 0, 0, tt.inputErr)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorIsCalled, log.errorIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorMsg, log.errorMsg)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalIsCalled, log.fatalIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalMsg, log.fatalMsg)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestHookEvent_OnProduceRecordUnbuffered(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							name                  string
 | 
				
			||||||
 | 
							inputErr              error
 | 
				
			||||||
 | 
							fatalOnError          bool
 | 
				
			||||||
 | 
							expectedErrorIsCalled bool
 | 
				
			||||||
 | 
							expectedErrorMsg      string
 | 
				
			||||||
 | 
							expectedFatalIsCalled bool
 | 
				
			||||||
 | 
							expectedFatalMsg      string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "error is nil",
 | 
				
			||||||
 | 
								inputErr:              nil,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context canceled",
 | 
				
			||||||
 | 
								inputErr:              context.Canceled,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "context deadline exceeded",
 | 
				
			||||||
 | 
								inputErr:              context.DeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: deadline exceeded (os package)",
 | 
				
			||||||
 | 
								inputErr:              os.ErrDeadlineExceeded,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: EOF (io package)",
 | 
				
			||||||
 | 
								inputErr:              io.EOF,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "retryable error: closed network connection (net package)",
 | 
				
			||||||
 | 
								inputErr:              net.ErrClosed,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (non-fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          false,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: true,
 | 
				
			||||||
 | 
								expectedErrorMsg:      "kgo.OnProduceRecordUnbuffered",
 | 
				
			||||||
 | 
								expectedFatalIsCalled: false,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name:                  "some error (fatal)",
 | 
				
			||||||
 | 
								inputErr:              errors.New("some error"),
 | 
				
			||||||
 | 
								fatalOnError:          true,
 | 
				
			||||||
 | 
								expectedErrorIsCalled: false,
 | 
				
			||||||
 | 
								expectedFatalIsCalled: true,
 | 
				
			||||||
 | 
								expectedFatalMsg:      "kgo.OnProduceRecordUnbuffered",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tt := range tests {
 | 
				
			||||||
 | 
							t.Run(tt.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								log := &mockLogger{}
 | 
				
			||||||
 | 
								he := &hookEvent{log: log, fatalOnError: tt.fatalOnError}
 | 
				
			||||||
 | 
								he.OnProduceRecordUnbuffered(&kgo.Record{}, tt.inputErr)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorIsCalled, log.errorIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedErrorMsg, log.errorMsg)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalIsCalled, log.fatalIsCalled)
 | 
				
			||||||
 | 
								require.Equal(t, tt.expectedFatalMsg, log.fatalMsg)
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Mocks
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type mockLogger struct {
 | 
				
			||||||
 | 
						errorIsCalled bool
 | 
				
			||||||
 | 
						errorMsg      string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						fatalIsCalled bool
 | 
				
			||||||
 | 
						fatalMsg      string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Init(...logger.Option) error {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Clone(...logger.Option) logger.Logger {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) V(logger.Level) bool {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Level(logger.Level) {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Options() logger.Options {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Fields(...interface{}) logger.Logger {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Info(context.Context, string, ...interface{}) {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Trace(context.Context, string, ...interface{}) {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Debug(context.Context, string, ...interface{}) {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Warn(context.Context, string, ...interface{}) {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Error(ctx context.Context, msg string, args ...interface{}) {
 | 
				
			||||||
 | 
						m.errorIsCalled = true
 | 
				
			||||||
 | 
						m.errorMsg = msg
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Fatal(ctx context.Context, msg string, args ...interface{}) {
 | 
				
			||||||
 | 
						m.fatalIsCalled = true
 | 
				
			||||||
 | 
						m.fatalMsg = msg
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Log(context.Context, logger.Level, string, ...interface{}) {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) Name() string {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (m *mockLogger) String() string {
 | 
				
			||||||
 | 
						panic("implement me")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										165
									
								
								kgo.go
									
									
									
									
									
								
							
							
						
						
									
										165
									
								
								kgo.go
									
									
									
									
									
								
							@@ -6,11 +6,13 @@ import (
 | 
				
			|||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"math/rand/v2"
 | 
						"math/rand/v2"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"sync"
 | 
						"sync"
 | 
				
			||||||
	"sync/atomic"
 | 
						"sync/atomic"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/twmb/franz-go/pkg/kadm"
 | 
				
			||||||
	"github.com/twmb/franz-go/pkg/kgo"
 | 
						"github.com/twmb/franz-go/pkg/kgo"
 | 
				
			||||||
	"github.com/twmb/franz-go/pkg/kmsg"
 | 
						"github.com/twmb/franz-go/pkg/kmsg"
 | 
				
			||||||
	"go.unistack.org/micro/v4/broker"
 | 
						"go.unistack.org/micro/v4/broker"
 | 
				
			||||||
@@ -74,7 +76,7 @@ type Broker struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	opts broker.Options
 | 
						opts broker.Options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sync.RWMutex
 | 
						mu   sync.RWMutex
 | 
				
			||||||
	init bool
 | 
						init bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -108,7 +110,7 @@ type kgoMessage struct {
 | 
				
			|||||||
	ctx   context.Context
 | 
						ctx   context.Context
 | 
				
			||||||
	body  []byte
 | 
						body  []byte
 | 
				
			||||||
	hdr   metadata.Metadata
 | 
						hdr   metadata.Metadata
 | 
				
			||||||
	opts  broker.PublishOptions
 | 
						opts  broker.MessageOptions
 | 
				
			||||||
	ack   bool
 | 
						ack   bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -141,22 +143,22 @@ func (b *Broker) newCodec(ct string) (codec.Codec, error) {
 | 
				
			|||||||
	if idx := strings.IndexRune(ct, ';'); idx >= 0 {
 | 
						if idx := strings.IndexRune(ct, ';'); idx >= 0 {
 | 
				
			||||||
		ct = ct[:idx]
 | 
							ct = ct[:idx]
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	b.RLock()
 | 
						b.mu.RLock()
 | 
				
			||||||
	c, ok := b.opts.Codecs[ct]
 | 
						c, ok := b.opts.Codecs[ct]
 | 
				
			||||||
	b.RUnlock()
 | 
						b.mu.RUnlock()
 | 
				
			||||||
	if ok {
 | 
						if ok {
 | 
				
			||||||
		return c, nil
 | 
							return c, nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil, codec.ErrUnknownContentType
 | 
						return nil, codec.ErrUnknownContentType
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (b *Broker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...broker.PublishOption) (broker.Message, error) {
 | 
					func (b *Broker) NewMessage(ctx context.Context, hdr metadata.Metadata, body interface{}, opts ...broker.MessageOption) (broker.Message, error) {
 | 
				
			||||||
	options := broker.NewPublishOptions(opts...)
 | 
						options := broker.NewMessageOptions(opts...)
 | 
				
			||||||
	if options.ContentType == "" {
 | 
						if options.ContentType == "" {
 | 
				
			||||||
		options.ContentType = b.opts.ContentType
 | 
							options.ContentType = b.opts.ContentType
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	m := &kgoMessage{ctx: ctx, hdr: hdr, opts: options}
 | 
						m := &kgoMessage{ctx: ctx, hdr: hdr.Copy(), opts: options}
 | 
				
			||||||
	c, err := b.newCodec(m.opts.ContentType)
 | 
						c, err := b.newCodec(m.opts.ContentType)
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
		m.body, err = c.Marshal(body)
 | 
							m.body, err = c.Marshal(body)
 | 
				
			||||||
@@ -165,6 +167,8 @@ func (b *Broker) NewMessage(ctx context.Context, hdr metadata.Metadata, body int
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						m.hdr.Set(metadata.HeaderContentType, m.opts.ContentType)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return m, nil
 | 
						return m, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -238,10 +242,72 @@ func (k *Broker) Connect(ctx context.Context) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	k.Lock()
 | 
						k.mu.Lock()
 | 
				
			||||||
	k.c = c
 | 
						k.c = c
 | 
				
			||||||
	k.connected.Store(1)
 | 
						k.connected.Store(1)
 | 
				
			||||||
	k.Unlock()
 | 
						k.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						exposeLag := false
 | 
				
			||||||
 | 
						if k.opts.Context != nil {
 | 
				
			||||||
 | 
							if v, ok := k.opts.Context.Value(exposeLagKey{}).(bool); ok && v {
 | 
				
			||||||
 | 
								exposeLag = v
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if exposeLag {
 | 
				
			||||||
 | 
							var mu sync.Mutex
 | 
				
			||||||
 | 
							var lastUpdate time.Time
 | 
				
			||||||
 | 
							type pl struct {
 | 
				
			||||||
 | 
								p string
 | 
				
			||||||
 | 
								l float64
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							lag := make(map[string]map[string]pl) // topic => group => partition => lag
 | 
				
			||||||
 | 
							ac := kadm.NewClient(k.c)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							updateStats := func() {
 | 
				
			||||||
 | 
								mu.Lock()
 | 
				
			||||||
 | 
								if time.Since(lastUpdate) < DefaultStatsInterval {
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								k.mu.Lock()
 | 
				
			||||||
 | 
								groups := make([]string, 0, len(k.subs))
 | 
				
			||||||
 | 
								for _, g := range k.subs {
 | 
				
			||||||
 | 
									groups = append(groups, g.opts.Group)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								k.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								dgls, err := ac.Lag(ctx, groups...)
 | 
				
			||||||
 | 
								if err != nil || !dgls.Ok() {
 | 
				
			||||||
 | 
									k.opts.Logger.Error(k.opts.Context, "kgo describe group lag error", err)
 | 
				
			||||||
 | 
									return
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								for gn, dgl := range dgls {
 | 
				
			||||||
 | 
									for tn, lmap := range dgl.Lag {
 | 
				
			||||||
 | 
										if _, ok := lag[tn]; !ok {
 | 
				
			||||||
 | 
											lag[tn] = make(map[string]pl)
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										for p, l := range lmap {
 | 
				
			||||||
 | 
											lag[tn][gn] = pl{p: strconv.Itoa(int(p)), l: float64(l.Lag)}
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for tn, dg := range lag {
 | 
				
			||||||
 | 
								for gn, gl := range dg {
 | 
				
			||||||
 | 
									k.opts.Meter.Gauge(semconv.BrokerGroupLag,
 | 
				
			||||||
 | 
										func() float64 { updateStats(); return gl.l },
 | 
				
			||||||
 | 
										"topic", tn,
 | 
				
			||||||
 | 
										"group", gn,
 | 
				
			||||||
 | 
										"partition", gl.p)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -259,8 +325,8 @@ func (k *Broker) Disconnect(ctx context.Context) error {
 | 
				
			|||||||
	ctx, span = k.opts.Tracer.Start(ctx, "Disconnect")
 | 
						ctx, span = k.opts.Tracer.Start(ctx, "Disconnect")
 | 
				
			||||||
	defer span.Finish()
 | 
						defer span.Finish()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	k.Lock()
 | 
						k.mu.Lock()
 | 
				
			||||||
	defer k.Unlock()
 | 
						defer k.mu.Unlock()
 | 
				
			||||||
	select {
 | 
						select {
 | 
				
			||||||
	case <-nctx.Done():
 | 
						case <-nctx.Done():
 | 
				
			||||||
		return nctx.Err()
 | 
							return nctx.Err()
 | 
				
			||||||
@@ -284,8 +350,8 @@ func (k *Broker) Disconnect(ctx context.Context) error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (k *Broker) Init(opts ...broker.Option) error {
 | 
					func (k *Broker) Init(opts ...broker.Option) error {
 | 
				
			||||||
	k.Lock()
 | 
						k.mu.Lock()
 | 
				
			||||||
	defer k.Unlock()
 | 
						defer k.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(opts) == 0 && k.init {
 | 
						if len(opts) == 0 && k.init {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
@@ -344,51 +410,44 @@ func (b *Broker) fnPublish(ctx context.Context, topic string, messages ...broker
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error {
 | 
					func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error {
 | 
				
			||||||
	if b.connected.Load() == 0 {
 | 
						var records []*kgo.Record
 | 
				
			||||||
		c, _, err := b.connect(ctx, b.kopts...)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		b.Lock()
 | 
					 | 
				
			||||||
		b.c = c
 | 
					 | 
				
			||||||
		b.Unlock()
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	records := make([]*kgo.Record, 0, len(messages))
 | 
					 | 
				
			||||||
	var errs []string
 | 
					 | 
				
			||||||
	var key []byte
 | 
					 | 
				
			||||||
	var promise func(*kgo.Record, error)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, msg := range messages {
 | 
						for _, msg := range messages {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if mctx := msg.Context(); mctx != nil {
 | 
					 | 
				
			||||||
			if k, ok := mctx.Value(publishKey{}).([]byte); ok && k != nil {
 | 
					 | 
				
			||||||
				key = k
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			if p, ok := mctx.Value(publishPromiseKey{}).(func(*kgo.Record, error)); ok && p != nil {
 | 
					 | 
				
			||||||
				promise = p
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		rec := &kgo.Record{
 | 
							rec := &kgo.Record{
 | 
				
			||||||
			Context: ctx,
 | 
								Context: msg.Context(),
 | 
				
			||||||
			Key:     key,
 | 
					 | 
				
			||||||
			Topic:   topic,
 | 
								Topic:   topic,
 | 
				
			||||||
			Value:   msg.Body(),
 | 
								Value:   msg.Body(),
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc()
 | 
							var promise func(*kgo.Record, error)
 | 
				
			||||||
 | 
							if rec.Context != nil {
 | 
				
			||||||
 | 
								if k, ok := rec.Context.Value(messageKey{}).([]byte); ok && k != nil {
 | 
				
			||||||
 | 
									rec.Key = k
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if p, ok := rec.Context.Value(messagePromiseKey{}).(func(*kgo.Record, error)); ok && p != nil {
 | 
				
			||||||
 | 
									promise = p
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							kmsg, ok := msg.(*kgoMessage)
 | 
				
			||||||
 | 
							if !ok {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if kmsg.opts.Context != nil {
 | 
				
			||||||
 | 
								if k, ok := kmsg.opts.Context.Value(messageKey{}).([]byte); ok && k != nil {
 | 
				
			||||||
 | 
									rec.Key = k
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if p, ok := kmsg.opts.Context.Value(messagePromiseKey{}).(func(*kgo.Record, error)); ok && p != nil {
 | 
				
			||||||
 | 
									promise = p
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		setHeaders(rec, msg.Header())
 | 
							setHeaders(rec, msg.Header())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		records = append(records, rec)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	ts := time.Now()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if promise != nil {
 | 
							if promise != nil {
 | 
				
			||||||
 | 
								ts := time.Now()
 | 
				
			||||||
		for _, rec := range records {
 | 
								b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Inc()
 | 
				
			||||||
			b.c.Produce(ctx, rec, func(r *kgo.Record, err error) {
 | 
								b.c.Produce(ctx, rec, func(r *kgo.Record, err error) {
 | 
				
			||||||
				te := time.Since(ts)
 | 
									te := time.Since(ts)
 | 
				
			||||||
				b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Dec()
 | 
									b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", rec.Topic, "topic", rec.Topic).Dec()
 | 
				
			||||||
@@ -401,12 +460,17 @@ func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.M
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
				promise(r, err)
 | 
									promise(r, err)
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								records = append(records, rec)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return nil
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if len(records) > 0 {
 | 
				
			||||||
 | 
							var errs []string
 | 
				
			||||||
 | 
							ts := time.Now()
 | 
				
			||||||
 | 
							b.opts.Meter.Counter(semconv.PublishMessageInflight, "endpoint", topic, "topic", topic).Set(uint64(len(records)))
 | 
				
			||||||
		results := b.c.ProduceSync(ctx, records...)
 | 
							results := b.c.ProduceSync(ctx, records...)
 | 
				
			||||||
 | 
					 | 
				
			||||||
		te := time.Since(ts)
 | 
							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.Summary(semconv.PublishMessageLatencyMicroseconds, "endpoint", result.Record.Topic, "topic", result.Record.Topic).Update(te.Seconds())
 | 
				
			||||||
@@ -423,6 +487,7 @@ func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.M
 | 
				
			|||||||
		if len(errs) > 0 {
 | 
							if len(errs) > 0 {
 | 
				
			||||||
			return fmt.Errorf("publish error: %s", strings.Join(errs, "\n"))
 | 
								return fmt.Errorf("publish error: %s", strings.Join(errs, "\n"))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -549,9 +614,9 @@ func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interfac
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	go sub.poll(ctx)
 | 
						go sub.poll(ctx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	b.Lock()
 | 
						b.mu.Lock()
 | 
				
			||||||
	b.subs = append(b.subs, sub)
 | 
						b.subs = append(b.subs, sub)
 | 
				
			||||||
	b.Unlock()
 | 
						b.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return sub, nil
 | 
						return sub, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								kgo_test.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								kgo_test.go
									
									
									
									
									
								
							@@ -81,7 +81,7 @@ func TestFail(t *testing.T) {
 | 
				
			|||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		for _, msg := range msgs {
 | 
							for _, msg := range msgs {
 | 
				
			||||||
			//		t.Logf("broker publish")
 | 
								//		t.Logf("broker publish")
 | 
				
			||||||
			if err := b.Publish(ctx, "test", msg); err != nil {
 | 
								if err := b.Publish(ctx, "test.fail", msg); err != nil {
 | 
				
			||||||
				t.Fatal(err)
 | 
									t.Fatal(err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -96,7 +96,7 @@ func TestFail(t *testing.T) {
 | 
				
			|||||||
		return msg.Ack()
 | 
							return msg.Ack()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sub, err := b.Subscribe(ctx, "test", fn,
 | 
						sub, err := b.Subscribe(ctx, "test.fail", fn,
 | 
				
			||||||
		broker.SubscribeAutoAck(true),
 | 
							broker.SubscribeAutoAck(true),
 | 
				
			||||||
		broker.SubscribeGroup(group),
 | 
							broker.SubscribeGroup(group),
 | 
				
			||||||
		broker.SubscribeBodyOnly(true))
 | 
							broker.SubscribeBodyOnly(true))
 | 
				
			||||||
@@ -184,7 +184,7 @@ func TestPubSub(t *testing.T) {
 | 
				
			|||||||
			msgs = append(msgs, m)
 | 
								msgs = append(msgs, m)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := b.Publish(ctx, "test", msgs...); err != nil {
 | 
							if err := b.Publish(ctx, "test.pubsub", msgs...); err != nil {
 | 
				
			||||||
			t.Fatal(err)
 | 
								t.Fatal(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		//	t.Skip()
 | 
							//	t.Skip()
 | 
				
			||||||
@@ -197,7 +197,7 @@ func TestPubSub(t *testing.T) {
 | 
				
			|||||||
		return msg.Ack()
 | 
							return msg.Ack()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	sub, err := b.Subscribe(ctx, "test", fn,
 | 
						sub, err := b.Subscribe(ctx, "test.pubsub", fn,
 | 
				
			||||||
		broker.SubscribeAutoAck(true),
 | 
							broker.SubscribeAutoAck(true),
 | 
				
			||||||
		broker.SubscribeGroup(group),
 | 
							broker.SubscribeGroup(group),
 | 
				
			||||||
		broker.SubscribeBodyOnly(true))
 | 
							broker.SubscribeBodyOnly(true))
 | 
				
			||||||
@@ -222,7 +222,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 {
 | 
				
			||||||
					t.Logf("processed %v\n", prc)
 | 
										t.Logf("processed %v of %v\n", prc, msgcnt)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			case <-ticker.C:
 | 
								case <-ticker.C:
 | 
				
			||||||
				close(done)
 | 
									close(done)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										25
									
								
								options.go
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								options.go
									
									
									
									
									
								
							@@ -14,7 +14,7 @@ var (
 | 
				
			|||||||
	DefaultCommitInterval = 5 * time.Second
 | 
						DefaultCommitInterval = 5 * time.Second
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// DefaultStatsInterval specifies how fast check consumer lag
 | 
						// DefaultStatsInterval specifies how fast check consumer lag
 | 
				
			||||||
	DefaultStatsInterval = 5 * time.Second
 | 
						DefaultStatsInterval = 30 * time.Second
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// DefaultSubscribeMaxInflight specifies how much messages keep inflight
 | 
						// DefaultSubscribeMaxInflight specifies how much messages keep inflight
 | 
				
			||||||
	DefaultSubscribeMaxInflight = 100
 | 
						DefaultSubscribeMaxInflight = 100
 | 
				
			||||||
@@ -27,11 +27,11 @@ func SubscribeContext(ctx context.Context) broker.SubscribeOption {
 | 
				
			|||||||
	return broker.SetSubscribeOption(subscribeContextKey{}, ctx)
 | 
						return broker.SetSubscribeOption(subscribeContextKey{}, ctx)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type publishKey struct{}
 | 
					type messageKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// PublishKey set the kafka message key (broker option)
 | 
					// MessageKey set the kafka message key (broker option)
 | 
				
			||||||
func PublishKey(key []byte) broker.PublishOption {
 | 
					func MessageKey(key []byte) broker.MessageOption {
 | 
				
			||||||
	return broker.SetPublishOption(publishKey{}, key)
 | 
						return broker.SetMessageOption(messageKey{}, key)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type optionsKey struct{}
 | 
					type optionsKey struct{}
 | 
				
			||||||
@@ -103,11 +103,11 @@ func SubscribeFatalOnError(b bool) broker.SubscribeOption {
 | 
				
			|||||||
	return broker.SetSubscribeOption(fatalOnErrorKey{}, b)
 | 
						return broker.SetSubscribeOption(fatalOnErrorKey{}, b)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type publishPromiseKey struct{}
 | 
					type messagePromiseKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// PublishPromise set the kafka promise func for Produce
 | 
					// MessagePromise set the kafka promise func for Produce
 | 
				
			||||||
func PublishPromise(fn func(*kgo.Record, error)) broker.PublishOption {
 | 
					func MessagePromise(fn func(*kgo.Record, error)) broker.MessageOption {
 | 
				
			||||||
	return broker.SetPublishOption(publishPromiseKey{}, fn)
 | 
						return broker.SetMessageOption(messagePromiseKey{}, fn)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type subscribeMessagePoolKey struct{}
 | 
					type subscribeMessagePoolKey struct{}
 | 
				
			||||||
@@ -116,3 +116,10 @@ type subscribeMessagePoolKey struct{}
 | 
				
			|||||||
func SubscribeMessagePool(b bool) broker.SubscribeOption {
 | 
					func SubscribeMessagePool(b bool) broker.SubscribeOption {
 | 
				
			||||||
	return broker.SetSubscribeOption(subscribeMessagePoolKey{}, b)
 | 
						return broker.SetSubscribeOption(subscribeMessagePoolKey{}, b)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type exposeLagKey struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ExposeLag enabled subscriber lag via [meter.Meter]
 | 
				
			||||||
 | 
					func ExposeLag(b bool) broker.Option {
 | 
				
			||||||
 | 
						return broker.SetOption(exposeLagKey{}, b)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,7 +8,6 @@ import (
 | 
				
			|||||||
	"sync/atomic"
 | 
						"sync/atomic"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/twmb/franz-go/pkg/kadm"
 | 
					 | 
				
			||||||
	"github.com/twmb/franz-go/pkg/kgo"
 | 
						"github.com/twmb/franz-go/pkg/kgo"
 | 
				
			||||||
	"github.com/twmb/franz-go/pkg/kmsg"
 | 
						"github.com/twmb/franz-go/pkg/kmsg"
 | 
				
			||||||
	"go.unistack.org/micro/v4/broker"
 | 
						"go.unistack.org/micro/v4/broker"
 | 
				
			||||||
@@ -48,9 +47,8 @@ type Subscriber struct {
 | 
				
			|||||||
	done         chan struct{}
 | 
						done         chan struct{}
 | 
				
			||||||
	kopts        broker.Options
 | 
						kopts        broker.Options
 | 
				
			||||||
	opts         broker.SubscribeOptions
 | 
						opts         broker.SubscribeOptions
 | 
				
			||||||
 | 
					 | 
				
			||||||
	connected    *atomic.Uint32
 | 
						connected    *atomic.Uint32
 | 
				
			||||||
	sync.RWMutex
 | 
						mu           sync.RWMutex
 | 
				
			||||||
	closed       bool
 | 
						closed       bool
 | 
				
			||||||
	fatalOnError bool
 | 
						fatalOnError bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -88,46 +86,13 @@ func (s *Subscriber) Unsubscribe(ctx context.Context) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func (s *Subscriber) poll(ctx context.Context) {
 | 
					func (s *Subscriber) poll(ctx context.Context) {
 | 
				
			||||||
	maxInflight := DefaultSubscribeMaxInflight
 | 
						maxInflight := DefaultSubscribeMaxInflight
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if s.opts.Context != nil {
 | 
						if s.opts.Context != nil {
 | 
				
			||||||
		if n, ok := s.opts.Context.Value(subscribeMaxInflightKey{}).(int); n > 0 && ok {
 | 
							if n, ok := s.opts.Context.Value(subscribeMaxInflightKey{}).(int); n > 0 && ok {
 | 
				
			||||||
			maxInflight = n
 | 
								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))).Set(uint64(l.Lag))
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				s.Unlock()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for {
 | 
						for {
 | 
				
			||||||
		select {
 | 
							select {
 | 
				
			||||||
		case <-ctx.Done():
 | 
							case <-ctx.Done():
 | 
				
			||||||
@@ -142,14 +107,18 @@ func (s *Subscriber) poll(ctx context.Context) {
 | 
				
			|||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			fetches.EachError(func(t string, p int32, err error) {
 | 
								fetches.EachError(func(t string, p int32, err error) {
 | 
				
			||||||
 | 
									if kgo.IsRetryableBrokerErr(err) {
 | 
				
			||||||
 | 
										s.kopts.Logger.Error(ctx, fmt.Sprintf("[kgo] fetch topic %s partition %d error", t, p), err)
 | 
				
			||||||
 | 
									} else {
 | 
				
			||||||
					s.kopts.Logger.Fatal(ctx, fmt.Sprintf("[kgo] fetch topic %s partition %d error", t, p), err)
 | 
										s.kopts.Logger.Fatal(ctx, fmt.Sprintf("[kgo] fetch topic %s partition %d error", t, p), err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			fetches.EachPartition(func(p kgo.FetchTopicPartition) {
 | 
								fetches.EachPartition(func(p kgo.FetchTopicPartition) {
 | 
				
			||||||
				tps := tp{p.Topic, p.Partition}
 | 
									tps := tp{p.Topic, p.Partition}
 | 
				
			||||||
				s.Lock()
 | 
									s.mu.Lock()
 | 
				
			||||||
				c := s.consumers[tps]
 | 
									c := s.consumers[tps]
 | 
				
			||||||
				s.Unlock()
 | 
									s.mu.Unlock()
 | 
				
			||||||
				if c != nil {
 | 
									if c != nil {
 | 
				
			||||||
					c.recs <- p
 | 
										c.recs <- p
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
@@ -166,15 +135,15 @@ func (s *Subscriber) killConsumers(ctx context.Context, lost map[string][]int32)
 | 
				
			|||||||
	for topic, partitions := range lost {
 | 
						for topic, partitions := range lost {
 | 
				
			||||||
		for _, partition := range partitions {
 | 
							for _, partition := range partitions {
 | 
				
			||||||
			tps := tp{topic, partition}
 | 
								tps := tp{topic, partition}
 | 
				
			||||||
			s.Lock()
 | 
								s.mu.Lock()
 | 
				
			||||||
			pc, ok := s.consumers[tps]
 | 
								pc, ok := s.consumers[tps]
 | 
				
			||||||
			s.Unlock()
 | 
								s.mu.Unlock()
 | 
				
			||||||
			if !ok {
 | 
								if !ok {
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			s.Lock()
 | 
								s.mu.Lock()
 | 
				
			||||||
			delete(s.consumers, tps)
 | 
								delete(s.consumers, tps)
 | 
				
			||||||
			s.Unlock()
 | 
								s.mu.Unlock()
 | 
				
			||||||
			close(pc.quit)
 | 
								close(pc.quit)
 | 
				
			||||||
			if s.kopts.Logger.V(logger.DebugLevel) {
 | 
								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))
 | 
									s.kopts.Logger.Debug(ctx, fmt.Sprintf("[kgo] waiting for work to finish topic %s partition %d", topic, partition))
 | 
				
			||||||
@@ -230,9 +199,9 @@ func (s *Subscriber) assigned(_ context.Context, c *kgo.Client, assigned map[str
 | 
				
			|||||||
				opts:        s.opts,
 | 
									opts:        s.opts,
 | 
				
			||||||
				connected:   s.connected,
 | 
									connected:   s.connected,
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			s.Lock()
 | 
								s.mu.Lock()
 | 
				
			||||||
			s.consumers[tp{topic, partition}] = pc
 | 
								s.consumers[tp{topic, partition}] = pc
 | 
				
			||||||
			s.Unlock()
 | 
								s.mu.Unlock()
 | 
				
			||||||
			go pc.consume()
 | 
								go pc.consume()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -272,7 +241,11 @@ func (pc *consumer) consume() {
 | 
				
			|||||||
				for _, hdr := range record.Headers {
 | 
									for _, hdr := range record.Headers {
 | 
				
			||||||
					pm.hdr.Set(hdr.Key, string(hdr.Value))
 | 
										pm.hdr.Set(hdr.Key, string(hdr.Value))
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
									pm.hdr.Set("Micro-Offset", strconv.FormatInt(record.Offset, 10))
 | 
				
			||||||
 | 
									pm.hdr.Set("Micro-Partition", strconv.FormatInt(int64(record.Partition), 10))
 | 
				
			||||||
 | 
									pm.hdr.Set("Micro-Topic", record.Topic)
 | 
				
			||||||
 | 
									pm.hdr.Set("Micro-Key", string(record.Key))
 | 
				
			||||||
 | 
									pm.hdr.Set("Micro-Timestamp", strconv.FormatInt(record.Timestamp.Unix(), 10))
 | 
				
			||||||
				switch h := pc.handler.(type) {
 | 
									switch h := pc.handler.(type) {
 | 
				
			||||||
				case func(broker.Message) error:
 | 
									case func(broker.Message) error:
 | 
				
			||||||
					err = h(pm)
 | 
										err = h(pm)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user