Skip to content

Commit 49ee9b5

Browse files
committed
scripting service client
1 parent e28dbd2 commit 49ee9b5

File tree

22 files changed

+605
-218
lines changed

22 files changed

+605
-218
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## 3.7.0
2-
* Replaced `Option` to `CustomOption` on `Connection` interface methods
2+
* Replaced `Option` to `CustomOption` on `Connection` interface methods
33
* Implements `WithCustom[Token,Database]` options for redefine database and token
44
* Removed experimental `balancer.PreferEndpoints[WithFallback][RegEx]` balancers
5-
* Implements `balancer.PreferLocations[WithFallback]` balancers
65
* Supported connections `TTL` with `Option` `WithConnectionTTL`
7-
* Remove unnecessary `WithFastDial` option (lazy connections always fast inserts into cluster)
6+
* Remove unnecessary `WithFastDial` option (lazy connections are always fast inserts into cluster)
7+
* Added `ScriptingYQL` service client
88

99
## 3.6.2
1010
* Refactored table retry helpers

connection.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,46 @@ import (
2020
"github.com/ydb-platform/ydb-go-sdk/v3/log"
2121
"github.com/ydb-platform/ydb-go-sdk/v3/ratelimiter"
2222
"github.com/ydb-platform/ydb-go-sdk/v3/scheme"
23+
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
2324
"github.com/ydb-platform/ydb-go-sdk/v3/table"
2425
tableConfig "github.com/ydb-platform/ydb-go-sdk/v3/table/config"
2526
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
2627
)
2728

29+
// Connection interface provide access to YDB service clients
30+
// Interface and list of clients may be changed in the future
2831
type Connection interface {
2932
db.Connection
3033

3134
// Table returns table client with options from Connection instance.
3235
// Options provide options replacement for requested table client
33-
// such as endpoint, database, secure connection flag and credentials
34-
// Options replacement feature not implements now
36+
// such as database and access token
3537
Table(opts ...CustomOption) table.Client
3638

3739
// Scheme returns scheme client with options from Connection instance.
3840
// Options provide options replacement for requested scheme client
39-
// such as endpoint, database, secure connection flag and credentials
40-
// Options replacement feature not implements now
41+
// such as database and access token
4142
Scheme(opts ...CustomOption) scheme.Client
4243

4344
// Coordination returns coordination client with options from Connection instance.
4445
// Options provide options replacement for requested coordination client
45-
// such as endpoint, database, secure connection flag and credentials
46-
// Options replacement feature not implements now
46+
// such as database and access token
4747
Coordination(opts ...CustomOption) coordination.Client
4848

4949
// Ratelimiter returns rate limiter client with options from Connection instance.
5050
// Options provide options replacement for requested rate limiter client
51-
// such as endpoint, database, secure connection flag and credentials
52-
// Options replacement feature not implements now
51+
// such as database and access token
5352
Ratelimiter(opts ...CustomOption) ratelimiter.Client
5453

5554
// Discovery returns discovery client with options from Connection instance.
5655
// Options provide options replacement for requested discovery client
57-
// such as endpoint, database, secure connection flag and credentials
58-
// Options replacement feature not implements now
56+
// such as database and access token
5957
Discovery(opts ...CustomOption) discovery.Client
6058

61-
// Close clears resources and close all connections to YDB
62-
Close(ctx context.Context) error
59+
// Scripting returns scripting client with options from Connection instance.
60+
// Options provide options replacement for requested discovery client
61+
// such as database and access token
62+
Scripting(opts ...CustomOption) scripting.Client
6363
}
6464

6565
type connection struct {
@@ -71,6 +71,7 @@ type connection struct {
7171
db db.Connection
7272
table table.Client
7373
scheme scheme.Client
74+
scripting scripting.Client
7475
discovery discovery.Client
7576
coordination coordination.Client
7677
rateLimiter ratelimiter.Client
@@ -95,9 +96,15 @@ func (c *connection) Close(ctx context.Context) error {
9596
if err := c.table.Close(ctx); err != nil {
9697
issues = append(issues, err)
9798
}
99+
if err := c.scripting.Close(ctx); err != nil {
100+
issues = append(issues, err)
101+
}
98102
if err := c.db.Close(ctx); err != nil {
99103
issues = append(issues, err)
100104
}
105+
if err := c.conns.Close(ctx); err != nil {
106+
issues = append(issues, err)
107+
}
101108
if len(issues) > 0 {
102109
return errors.NewWithIssues("close failed", issues...)
103110
}
@@ -170,6 +177,13 @@ func (c *connection) Discovery(opts ...CustomOption) discovery.Client {
170177
return proxy.Discovery(c.discovery, c.meta(opts...))
171178
}
172179

180+
func (c *connection) Scripting(opts ...CustomOption) scripting.Client {
181+
if len(opts) == 0 {
182+
return c.scripting
183+
}
184+
return proxy.Scripting(c.scripting, c.meta(opts...))
185+
}
186+
173187
func (c *connection) meta(opts ...CustomOption) meta.Meta {
174188
if len(opts) == 0 {
175189
return c.config.Meta()
@@ -225,6 +239,7 @@ func New(ctx context.Context, opts ...Option) (_ Connection, err error) {
225239
}
226240
c.table = lazy.Table(c.db, c.tableOptions)
227241
c.scheme = lazy.Scheme(c.db)
242+
c.scripting = lazy.Scripting(c.db)
228243
c.discovery = lazy.Discovery(c.db, c.config.Trace())
229244
c.coordination = lazy.Coordination(c.db)
230245
c.rateLimiter = lazy.Ratelimiter(c.db)
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package icluster
22

33
import (
4-
"context"
5-
64
"google.golang.org/grpc"
5+
6+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
77
)
88

99
type Cluster interface {
@@ -12,7 +12,5 @@ type Cluster interface {
1212
// Lazy getting grpc-connection must use for embedded client-side balancing
1313
// DB may be put into code-generated client constructor as is.
1414
grpc.ClientConnInterface
15-
16-
// Close clears resources and close all connections to YDB
17-
Close(ctx context.Context) error
15+
closer.Closer
1816
}

internal/conn/conn.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type conn struct {
4646
}
4747

4848
func (c *conn) Park(ctx context.Context) error {
49+
c.Lock()
50+
defer c.Unlock()
4951
return c.close(ctx)
5052
}
5153

@@ -155,6 +157,7 @@ func isBroken(raw *grpc.ClientConn) bool {
155157
return s == connectivity.Shutdown || s == connectivity.TransientFailure
156158
}
157159

160+
// conn must be locked
158161
func (c *conn) close(ctx context.Context) (err error) {
159162
if c.cc == nil {
160163
return nil

internal/conn/pool.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88

99
"google.golang.org/grpc"
1010

11+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
1112
"github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/errors"
1214
)
1315

1416
type Pool interface {
17+
closer.Closer
18+
1519
Get(endpoint endpoint.Endpoint) Conn
1620
Pessimize(ctx context.Context, e endpoint.Endpoint) error
17-
Close()
1821
}
1922

2023
type PoolConfig interface {
@@ -51,19 +54,24 @@ func (p *pool) Get(endpoint endpoint.Endpoint) Conn {
5154
return cc
5255
}
5356

54-
func (p *pool) Close() {
57+
func (p *pool) Close(ctx context.Context) error {
5558
close(p.done)
59+
var issues []error
60+
p.mtx.Lock()
61+
defer p.mtx.Unlock()
62+
for a, c := range p.conns {
63+
if err := c.Close(ctx); err != nil {
64+
issues = append(issues, err)
65+
}
66+
delete(p.conns, a)
67+
}
68+
if len(issues) == 0 {
69+
return nil
70+
}
71+
return errors.NewWithIssues("connection pool close failed", issues...)
5672
}
5773

5874
func (p *pool) connCloser(ctx context.Context, interval time.Duration) {
59-
defer func() {
60-
p.mtx.RLock()
61-
for a, c := range p.conns {
62-
c.Close(ctx)
63-
delete(p.conns, a)
64-
}
65-
p.mtx.RUnlock()
66-
}()
6775
for {
6876
select {
6977
case <-p.done:

internal/errors/database.go

Lines changed: 0 additions & 152 deletions
This file was deleted.

internal/errors/retryable.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package errors
2+
3+
type RetryableError struct {
4+
Msg string
5+
BackoffType BackoffType
6+
MustDeleteSession bool
7+
}
8+
9+
func (e *RetryableError) Error() string {
10+
return e.Msg
11+
}

internal/lazy/coordiantion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type lazyCoordination struct {
1616
m sync.Mutex
1717
}
1818

19-
func Coordination(db db.Connection) *lazyCoordination {
19+
func Coordination(db db.Connection) coordination.Client {
2020
return &lazyCoordination{
2121
db: db,
2222
}

internal/lazy/discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type lazyDiscovery struct {
1717
m sync.Mutex
1818
}
1919

20-
func Discovery(db db.Connection, trace trace.Driver) *lazyDiscovery {
20+
func Discovery(db db.Connection, trace trace.Driver) discovery.Client {
2121
return &lazyDiscovery{
2222
db: db,
2323
trace: trace,

0 commit comments

Comments
 (0)