Skip to content

Commit 86aeef7

Browse files
authored
Merge pull request #324 from ydb-platform/close-session
add create session goroutine to table client spawned goroutines wait …
2 parents 23a39ac + 2f21bc3 commit 86aeef7

File tree

7 files changed

+186
-328
lines changed

7 files changed

+186
-328
lines changed

.github/workflows/tests.yml

Lines changed: 21 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ jobs:
2424
uses: actions/checkout@v2
2525
- name: Test
2626
run: go test -race -tags fast -coverprofile unit.txt -covermode atomic ./...
27-
- name: Upload coverage to Codecov
27+
- name: Upload coverage report to Codecov
2828
uses: codecov/codecov-action@v2
2929
with:
3030
file: ./unit.txt
3131
flags: unit,${{ matrix.os }},${{ matrix.go-version }}
3232
name: unit
33-
table:
33+
e2e:
3434
strategy:
3535
matrix:
36-
go-version: [1.18.x, 1.19.x]
36+
go-version: [1.14.x, 1.18.x, 1.19.x]
3737
os: [ubuntu-latest]
3838
services:
3939
ydb:
@@ -65,217 +65,52 @@ jobs:
6565
uses: actions/checkout@v2
6666
- name: Wait database available
6767
run: bash ./.github/scripts/wait-ydb-container.sh
68-
- name: Test
68+
- name: Test Table
6969
run: go test -race -coverpkg=./... -coverprofile table.txt -covermode atomic ./table/table_e2e_test.go
70-
- name: Upload coverage to Codecov
70+
- name: Upload Table coverage report to Codecov
7171
uses: codecov/codecov-action@v2
7272
with:
7373
file: ./table.txt
74-
flags: table,e2e,integration,${{ matrix.os }},${{ matrix.go-version }}
74+
flags: table,e2e,${{ matrix.os }},${{ matrix.go-version }}
7575
name: table
76-
ratelimiter:
77-
strategy:
78-
matrix:
79-
go-version: [1.18.x, 1.19.x]
80-
os: [ubuntu-latest]
81-
services:
82-
ydb:
83-
image: cr.yandex/yc/yandex-docker-local-ydb:latest
84-
ports:
85-
- 2135:2135
86-
- 2136:2136
87-
- 8765:8765
88-
volumes:
89-
- /tmp/ydb_certs:/ydb_certs
90-
env:
91-
YDB_LOCAL_SURVIVE_RESTART: true
92-
YDB_USE_IN_MEMORY_PDISKS: true
93-
options: '-h localhost'
94-
env:
95-
OS: ${{ matrix.os }}
96-
GO: ${{ matrix.go-version }}
97-
YDB_CONNECTION_STRING: grpcs://localhost:2135/local
98-
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
99-
runs-on: ${{ matrix.os }}
100-
steps:
101-
- name: Install Go
102-
uses: actions/setup-go@v3
103-
with:
104-
go-version: ${{ matrix.go-version }}
105-
- name: Checkout code
106-
uses: actions/checkout@v2
107-
- name: Wait database available
108-
run: bash ./.github/scripts/wait-ydb-container.sh
109-
- name: Test
76+
- name: Test Ratelimiter
11077
run: go test -race -coverpkg=./... -coverprofile ratelimiter.txt -covermode atomic ./ratelimiter/ratelimiter_e2e_test.go
11178
shell: bash
112-
- name: Upload coverage to Codecov
79+
- name: Upload Ratelimiter coverage report to Codecov
11380
uses: codecov/codecov-action@v2
11481
with:
11582
file: ./ratelimiter.txt
116-
flags: ratelimiter,e2e,integration,${{ matrix.os }},${{ matrix.go-version }}
83+
flags: ratelimiter,e2e,${{ matrix.os }},${{ matrix.go-version }}
11784
name: ratelimiter
118-
scripting:
119-
strategy:
120-
matrix:
121-
go-version: [1.18.x, 1.19.x]
122-
os: [ubuntu-latest]
123-
services:
124-
ydb:
125-
image: cr.yandex/yc/yandex-docker-local-ydb:latest
126-
ports:
127-
- 2135:2135
128-
- 2136:2136
129-
- 8765:8765
130-
volumes:
131-
- /tmp/ydb_certs:/ydb_certs
132-
env:
133-
YDB_LOCAL_SURVIVE_RESTART: true
134-
YDB_USE_IN_MEMORY_PDISKS: true
135-
options: '-h localhost'
136-
env:
137-
OS: ${{ matrix.os }}
138-
GO: ${{ matrix.go-version }}
139-
YDB_CONNECTION_STRING: grpcs://localhost:2135/local
140-
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
141-
runs-on: ${{ matrix.os }}
142-
steps:
143-
- name: Install Go
144-
uses: actions/setup-go@v3
145-
with:
146-
go-version: ${{ matrix.go-version }}
147-
- name: Checkout code
148-
uses: actions/checkout@v2
149-
- name: Wait database available
150-
run: bash ./.github/scripts/wait-ydb-container.sh
151-
- name: Test
85+
- name: Test Scripting
15286
run: go test -race -coverpkg=./... -coverprofile scripting.txt -covermode atomic ./scripting/scripting_e2e_test.go
153-
- name: Upload coverage to Codecov
87+
- name: Upload Scripting coverage report to Codecov
15488
uses: codecov/codecov-action@v2
15589
with:
15690
file: ./scripting.txt
157-
flags: scripting,e2e,integration,${{ matrix.os }},${{ matrix.go-version }}
91+
flags: scripting,e2e,${{ matrix.os }},${{ matrix.go-version }}
15892
name: scripting
159-
topic:
160-
strategy:
161-
matrix:
162-
go-version: [1.18.x, 1.19.x]
163-
os: [ubuntu-latest]
164-
services:
165-
ydb:
166-
image: cr.yandex/yc/yandex-docker-local-ydb:latest
167-
ports:
168-
- 2135:2135
169-
- 2136:2136
170-
- 8765:8765
171-
volumes:
172-
- /tmp/ydb_certs:/ydb_certs
173-
env:
174-
YDB_LOCAL_SURVIVE_RESTART: true
175-
YDB_USE_IN_MEMORY_PDISKS: true
176-
options: '-h localhost'
177-
env:
178-
OS: ${{ matrix.os }}
179-
GO: ${{ matrix.go-version }}
180-
YDB_CONNECTION_STRING: grpcs://localhost:2135/local
181-
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
182-
runs-on: ${{ matrix.os }}
183-
steps:
184-
- name: Install Go
185-
uses: actions/setup-go@v3
186-
with:
187-
go-version: ${{ matrix.go-version }}
188-
- name: Checkout code
189-
uses: actions/checkout@v2
190-
- name: Wait database available
191-
run: bash ./.github/scripts/wait-ydb-container.sh
192-
- name: Test topic client
93+
- name: Test Topic client
19394
run: go test -race -coverpkg=./... -coverprofile topic.txt -covermode atomic ./topic/client_e2e_test.go ./topic/reader_e2e_test.go
194-
- name: Upload coverage to Codecov
95+
- name: Upload Topic coverage report to Codecov
19596
uses: codecov/codecov-action@v2
19697
with:
19798
file: ./topic.txt
198-
flags: topic,e2e,integration,${{ matrix.os }},${{ matrix.go-version }}
99+
flags: topic,e2e,${{ matrix.os }},${{ matrix.go-version }}
199100
name: topic
200-
discovery:
201-
strategy:
202-
matrix:
203-
go-version: [1.18.x, 1.19.x]
204-
os: [ubuntu-latest]
205-
services:
206-
ydb:
207-
image: cr.yandex/yc/yandex-docker-local-ydb:latest
208-
ports:
209-
- 2135:2135
210-
- 2136:2136
211-
- 8765:8765
212-
volumes:
213-
- /tmp/ydb_certs:/ydb_certs
214-
env:
215-
YDB_LOCAL_SURVIVE_RESTART: true
216-
YDB_USE_IN_MEMORY_PDISKS: true
217-
options: '-h localhost'
218-
env:
219-
OS: ${{ matrix.os }}
220-
GO: ${{ matrix.go-version }}
221-
YDB_CONNECTION_STRING: grpcs://localhost:2135/local
222-
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
223-
runs-on: ${{ matrix.os }}
224-
steps:
225-
- name: Install Go
226-
uses: actions/setup-go@v3
227-
with:
228-
go-version: ${{ matrix.go-version }}
229-
- name: Checkout code
230-
uses: actions/checkout@v2
231-
- name: Wait database available
232-
run: bash ./.github/scripts/wait-ydb-container.sh
233-
- name: Test
101+
- name: Test Discovery
234102
run: go test -race -coverpkg=./... -coverprofile discovery.txt -covermode atomic ./discovery/discovery_e2e_test.go
235-
- name: Upload coverage to Codecov
103+
- name: Upload Discovery coverage report to Codecov
236104
uses: codecov/codecov-action@v2
237105
with:
238106
file: ./discovery.txt
239-
flags: discovery,e2e,integration,${{ matrix.os }},${{ matrix.go-version }}
107+
flags: discovery,e2e,${{ matrix.os }},${{ matrix.go-version }}
240108
name: discovery
241-
connection:
242-
strategy:
243-
matrix:
244-
go-version: [1.18.x, 1.19.x]
245-
os: [ubuntu-latest]
246-
services:
247-
ydb:
248-
image: cr.yandex/yc/yandex-docker-local-ydb:latest
249-
ports:
250-
- 2135:2135
251-
- 2136:2136
252-
- 8765:8765
253-
volumes:
254-
- /tmp/ydb_certs:/ydb_certs
255-
env:
256-
YDB_LOCAL_SURVIVE_RESTART: true
257-
YDB_USE_IN_MEMORY_PDISKS: true
258-
options: '-h localhost'
259-
env:
260-
OS: ${{ matrix.os }}
261-
GO: ${{ matrix.go-version }}
262-
YDB_CONNECTION_STRING: grpcs://localhost:2135/local
263-
YDB_SSL_ROOT_CERTIFICATES_FILE: /tmp/ydb_certs/ca.pem
264-
runs-on: ${{ matrix.os }}
265-
steps:
266-
- name: Install Go
267-
uses: actions/setup-go@v3
268-
with:
269-
go-version: ${{ matrix.go-version }}
270-
- name: Checkout code
271-
uses: actions/checkout@v2
272-
- name: Wait database available
273-
run: bash ./.github/scripts/wait-ydb-container.sh
274-
- name: Test
109+
- name: Test Connection
275110
run: go test -race -coverpkg=./... -coverprofile connection.txt -covermode atomic ./connection_e2e_test.go
276-
- name: Upload coverage to Codecov
111+
- name: Upload Connection coverage report to Codecov
277112
uses: codecov/codecov-action@v2
278113
with:
279114
file: ./connection.txt
280-
flags: connection,e2e,integration,${{ matrix.os }},${{ matrix.go-version }}
115+
flags: connection,e2e,${{ matrix.os }},${{ matrix.go-version }}
281116
name: connection

internal/background/worker.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@ package background
22

33
import (
44
"context"
5+
"errors"
56
"runtime/pprof"
67
"sync"
8+
"sync/atomic"
79

810
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
11+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
912
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsync"
1013
)
1114

15+
var ErrAlreadyClosed = errors.New("background worker already closed")
16+
1217
// A Worker must not be copied after first use
1318
type Worker struct {
14-
ctx context.Context
15-
workers sync.WaitGroup
16-
19+
ctx context.Context
20+
workers sync.WaitGroup
1721
onceInit sync.Once
1822

19-
m xsync.Mutex
20-
stop xcontext.CancelErrFunc
23+
m xsync.Mutex
24+
25+
closed uint32
26+
stop xcontext.CancelErrFunc
2127
}
2228

2329
func NewWorker(parent context.Context) *Worker {
@@ -34,6 +40,10 @@ func (b *Worker) Context() context.Context {
3440
}
3541

3642
func (b *Worker) Start(name string, f func(ctx context.Context)) {
43+
if atomic.LoadUint32(&b.closed) != 0 {
44+
return
45+
}
46+
3747
b.init()
3848

3949
b.m.Lock()
@@ -61,13 +71,20 @@ func (b *Worker) Done() <-chan struct{} {
6171
}
6272

6373
func (b *Worker) Close(ctx context.Context, err error) error {
74+
if !atomic.CompareAndSwapUint32(&b.closed, 0, 1) {
75+
return xerrors.WithStackTrace(ErrAlreadyClosed)
76+
}
77+
6478
b.init()
6579

6680
b.stop(err)
6781

6882
waitCtx, waitCancel := context.WithCancel(ctx)
6983

7084
go func() {
85+
b.m.Lock()
86+
defer b.m.Unlock()
87+
7188
b.workers.Wait()
7289
waitCancel()
7390
}()

0 commit comments

Comments
 (0)