Skip to content

Commit cd7c3b2

Browse files
committed
added balancers.NoDiscovery() balancer + changed balancers.SingleConn() as alias to balancers.NoDiscovery()
1 parent 04bbfbd commit cd7c3b2

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

balancers/balancers.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ func RandomChoice() *balancerConfig.Config {
2121
return &balancerConfig.Config{}
2222
}
2323

24-
func SingleConn() *balancerConfig.Config {
24+
func NoDiscovery() *balancerConfig.Config {
2525
return &balancerConfig.Config{
26-
SingleConn: true,
26+
NoDiscovery: true,
2727
}
2828
}
2929

30+
func SingleConn() *balancerConfig.Config {
31+
return NoDiscovery()
32+
}
33+
3034
type filterLocalDC struct{}
3135

3236
func (filterLocalDC) Allow(info balancerConfig.Info, e endpoint.Info) bool {

balancers/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ func TestFromConfig(t *testing.T) {
2525
{
2626
name: "disable",
2727
config: `disable`,
28-
res: balancerConfig.Config{SingleConn: true},
28+
res: balancerConfig.Config{NoDiscovery: true},
2929
},
3030
{
3131
name: "single",
3232
config: `single`,
33-
res: balancerConfig.Config{SingleConn: true},
33+
res: balancerConfig.Config{NoDiscovery: true},
3434
},
3535
{
3636
name: "single/JSON",
3737
config: `{
3838
"type": "single"
3939
}`,
40-
res: balancerConfig.Config{SingleConn: true},
40+
res: balancerConfig.Config{NoDiscovery: true},
4141
},
4242
{
4343
name: "round_robin",

config/defaults_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package config
1+
package config_test
22

33
import (
44
"context"
55
"fmt"
6+
"github.com/ydb-platform/ydb-go-sdk/v3"
7+
"github.com/ydb-platform/ydb-go-sdk/v3/config"
68
"net"
79
"sync"
810
"testing"
@@ -145,11 +147,11 @@ func (d *CustomDialer) DialContext(ctx context.Context, addr string) (net.Conn,
145147
d.mu.Unlock()
146148

147149
// Log the dial attempt
148-
fmt.Printf("Attempting to dial %s (attempt #%d)\n", addr, attemptCount)
150+
fmt.Printf("Attempting to dial '%s' (attempt #%d)\n", addr, attemptCount)
149151

150152
if exists && delay > 0 {
151153
// Simulating connection delay or timeout
152-
fmt.Printf("Simulating delay of %v for %s\n", delay, addr)
154+
fmt.Printf("Simulating delay of %v for '%s'\n", delay, addr)
153155

154156
select {
155157
case <-time.After(delay):
@@ -286,21 +288,23 @@ func TestGRPCLoadBalancingPolicies(t *testing.T) {
286288
t.Logf("Attempting to connect with %s balancing policy", tc.balancingPolicy)
287289

288290
// Establish connection with our balancing policy
289-
conn, err := grpc.DialContext(
290-
ctx,
291-
"test:///unused",
292-
grpc.WithContextDialer(dialer.DialContext),
293-
grpc.WithTransportCredentials(insecure.NewCredentials()),
294-
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingPolicy": "%s"}`, tc.balancingPolicy)),
295-
grpc.WithBlock(),
291+
db, err := ydb.Open(ctx, "test://localhost:12345/local", // Используем схему test: которую мы зарегистрировали для manual resolver
292+
//ydb.WithBalancer(balancers.NoDiscovery()),
293+
ydb.With(config.WithGrpcOptions(
294+
grpc.WithResolvers(r),
295+
grpc.WithContextDialer(dialer.DialContext),
296+
grpc.WithTransportCredentials(insecure.NewCredentials()),
297+
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingPolicy": "%s"}`, tc.balancingPolicy)),
298+
//grpc.WithBlock(),
299+
)),
296300
)
297301

298302
dialDuration := time.Since(dialStart)
299303

300304
if err != nil {
301305
t.Fatalf("Failed to dial: %v", err)
302306
}
303-
defer conn.Close()
307+
defer db.Close(ctx)
304308

305309
// Log all dial attempts
306310
t.Logf("Connection established in %v", dialDuration)
@@ -310,7 +314,7 @@ func TestGRPCLoadBalancingPolicies(t *testing.T) {
310314
}
311315

312316
// Create client and make a request
313-
client := NewSimpleServiceClient(conn)
317+
client := NewSimpleServiceClient(ydb.GRPCConn(db))
314318
_, err = client.Ping(context.Background(), &emptypb.Empty{})
315319
if err != nil {
316320
t.Fatalf("Ping failed: %v", err)

internal/balancer/balancer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func New(ctx context.Context, driverConfig *config.Config, pool *conn.Pool, opts
257257
b.balancerConfig = *config
258258
}
259259

260-
if b.balancerConfig.SingleConn {
260+
if b.balancerConfig.NoDiscovery {
261261
b.applyDiscoveredEndpoints(ctx, []endpoint.Endpoint{
262262
endpoint.New(driverConfig.Endpoint()),
263263
}, "")

internal/balancer/config/routerconfig.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
type Config struct {
1313
Filter Filter
1414
AllowFallback bool
15-
SingleConn bool
15+
NoDiscovery bool
1616
DetectNearestDC bool
1717
}
1818

1919
func (c Config) String() string {
20-
if c.SingleConn {
21-
return "SingleConn"
20+
if c.NoDiscovery {
21+
return "NoDiscovery"
2222
}
2323

2424
buffer := xstring.Buffer()

0 commit comments

Comments
 (0)