|
| 1 | +package balancer |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/ibalancer" |
| 7 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/multi" |
| 8 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/rr" |
| 9 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/balancer/single" |
| 10 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/conn" |
| 11 | +) |
| 12 | + |
| 13 | +func RoundRobin() ibalancer.Balancer { |
| 14 | + return rr.RoundRobin() |
| 15 | +} |
| 16 | + |
| 17 | +func RandomChoice() ibalancer.Balancer { |
| 18 | + return rr.RandomChoice() |
| 19 | +} |
| 20 | + |
| 21 | +func SingleConn() ibalancer.Balancer { |
| 22 | + return single.Balancer() |
| 23 | +} |
| 24 | + |
| 25 | +// PreferLocalDC creates balancer which use endpoints only in location such as initial endpoint location |
| 26 | +// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location |
| 27 | +func PreferLocalDC(balancer ibalancer.Balancer) ibalancer.Balancer { |
| 28 | + return multi.Balancer( |
| 29 | + multi.WithBalancer( |
| 30 | + balancer, |
| 31 | + func(cc conn.Conn) bool { |
| 32 | + return cc.Endpoint().LocalDC() |
| 33 | + }, |
| 34 | + ), |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +// PreferLocalDCWithFallBack creates balancer which use endpoints only in location such as initial endpoint location |
| 39 | +// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location |
| 40 | +// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead |
| 41 | +func PreferLocalDCWithFallBack(balancer ibalancer.Balancer) ibalancer.Balancer { |
| 42 | + return multi.Balancer( |
| 43 | + multi.WithBalancer( |
| 44 | + balancer, |
| 45 | + func(cc conn.Conn) bool { |
| 46 | + return cc.Endpoint().LocalDC() |
| 47 | + }, |
| 48 | + ), |
| 49 | + multi.WithBalancer( |
| 50 | + balancer.(ibalancer.Creator).Create(), |
| 51 | + func(cc conn.Conn) bool { |
| 52 | + return !cc.Endpoint().LocalDC() |
| 53 | + }, |
| 54 | + ), |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +// PreferLocations creates balancer which use endpoints only in selected locations (such as "MAN", "VLA", etc.) |
| 59 | +// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location |
| 60 | +func PreferLocations(balancer ibalancer.Balancer, locations ...string) ibalancer.Balancer { |
| 61 | + if len(locations) == 0 { |
| 62 | + panic("empty list of locations") |
| 63 | + } |
| 64 | + for i := range locations { |
| 65 | + locations[i] = strings.ToUpper(locations[i]) |
| 66 | + } |
| 67 | + return multi.Balancer( |
| 68 | + multi.WithBalancer(balancer, func(cc conn.Conn) bool { |
| 69 | + location := strings.ToUpper(cc.Endpoint().Location()) |
| 70 | + for _, l := range locations { |
| 71 | + if location == l { |
| 72 | + return true |
| 73 | + } |
| 74 | + } |
| 75 | + return false |
| 76 | + }), |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +// PreferLocationsWithFallback creates balancer which use endpoints only in selected locations |
| 81 | +// Balancer "balancer" defines balancing algorithm between endpoints selected with filter by location |
| 82 | +// If filter returned zero endpoints from all discovery endpoints list - used all endpoint instead |
| 83 | +func PreferLocationsWithFallback(balancer ibalancer.Balancer, locations ...string) ibalancer.Balancer { |
| 84 | + if len(locations) == 0 { |
| 85 | + panic("empty list of locations") |
| 86 | + } |
| 87 | + for i := range locations { |
| 88 | + locations[i] = strings.ToUpper(locations[i]) |
| 89 | + } |
| 90 | + return multi.Balancer( |
| 91 | + multi.WithBalancer(balancer, func(cc conn.Conn) bool { |
| 92 | + location := strings.ToUpper(cc.Endpoint().Location()) |
| 93 | + for _, l := range locations { |
| 94 | + if location == l { |
| 95 | + return true |
| 96 | + } |
| 97 | + } |
| 98 | + return false |
| 99 | + }), |
| 100 | + multi.WithBalancer(balancer.(ibalancer.Creator).Create(), func(cc conn.Conn) bool { |
| 101 | + location := strings.ToUpper(cc.Endpoint().Location()) |
| 102 | + for _, l := range locations { |
| 103 | + if location == l { |
| 104 | + return false |
| 105 | + } |
| 106 | + } |
| 107 | + return true |
| 108 | + }), |
| 109 | + ) |
| 110 | +} |
| 111 | + |
| 112 | +func Default() ibalancer.Balancer { |
| 113 | + return PreferLocalDCWithFallBack(RandomChoice()) |
| 114 | +} |
0 commit comments