Prefer the nearest data center
Note
The article is being updated.
Below are examples of the code for setting the “prefer the nearest data center” balancing algorithm option in different YDB SDKs
Go
1. package main
3. import (
4. "context"
5. "os"
7. "github.com/ydb-platform/ydb-go-sdk/v3"
8. "github.com/ydb-platform/ydb-go-sdk/v3/balancers"
9. )
11. func main() {
12. ctx, cancel := context.WithCancel(context.Background())
13. defer cancel()
14. db, err := ydb.Open(
15. ctx,
16. os.Getenv("YDB_CONNECTION_STRING"),
17. ydb.WithBalancer(
18. balancers.PreferLocalDC(
19. balancers.RandomChoice(),
20. ),
21. ),
22. )
23. if err != nil {
24. panic(err)
25. }
26. defer func() {
27. _ = db.Close(ctx)
28. }()
29. }

