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
  2. import (
  3. "context"
  4. "os"
  5. "github.com/ydb-platform/ydb-go-sdk/v3"
  6. "github.com/ydb-platform/ydb-go-sdk/v3/balancers"
  7. )
  8. func main() {
  9. ctx, cancel := context.WithCancel(context.Background())
  10. defer cancel()
  11. db, err := ydb.Open(
  12. ctx,
  13. os.Getenv("YDB_CONNECTION_STRING"),
  14. ydb.WithBalancer(
  15. balancers.PreferLocalDC(
  16. balancers.RandomChoice(),
  17. ),
  18. ),
  19. )
  20. if err != nil {
  21. panic(err)
  22. }
  23. defer func() {
  24. _ = db.Close(ctx)
  25. }()
  26. }

Prefer the nearest data - 图1