Skip to content

Commit 2b5425b

Browse files
committed
Using one balancer for all clients
1 parent 84b7145 commit 2b5425b

11 files changed

Lines changed: 137 additions & 23 deletions

File tree

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package rpcplatform
1818

1919
import (
20+
"github.com/nexcode/rpcplatform/internal/config"
2021
"google.golang.org/grpc"
2122
"google.golang.org/grpc/resolver/manual"
2223
)
@@ -26,4 +27,5 @@ type Client struct {
2627
target string
2728
client *grpc.ClientConn
2829
resolver *manual.Resolver
30+
config *config.Client
2931
}

client_updatestate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
package rpcplatform
1818

1919
import (
20-
"google.golang.org/grpc/attributes"
20+
"github.com/nexcode/rpcplatform/internal/grpcattrs"
2121
"google.golang.org/grpc/resolver"
2222
)
2323

2424
func (c *Client) updateState(init bool, serverInfoTree map[string]*ServerInfo) {
2525
state := resolver.State{
2626
Addresses: make([]resolver.Address, 0, len(serverInfoTree)),
27+
// Attributes: grpcattrs.SetClientConfig(nil, c.config), // https://github.com/grpc/grpc-go/pull/8696
2728
}
2829

2930
for _, value := range serverInfoTree {
3031
state.Addresses = append(state.Addresses, resolver.Address{
3132
Addr: value.Address,
32-
Attributes: attributes.New(struct{}{}, value.Attributes),
33+
Attributes: grpcattrs.SetClientConfig(grpcattrs.SetAttributes(nil, value.Attributes), c.config),
3334
})
3435
}
3536

init.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2025 RPCPlatform Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package rpcplatform
18+
19+
import (
20+
"github.com/nexcode/rpcplatform/internal/balancer"
21+
)
22+
23+
func init() {
24+
balancer.Register()
25+
}

internal/balancer/pickerbuilder.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@
1616

1717
package balancer
1818

19-
type pickerBuilder struct {
20-
maxActiveServers int
21-
}
19+
type pickerBuilder struct{}

internal/balancer/pickerbuilder_makeconninfo.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import (
2121
"math"
2222
"slices"
2323

24-
"github.com/nexcode/rpcplatform/internal/attributes"
24+
"github.com/nexcode/rpcplatform/internal/config"
25+
"github.com/nexcode/rpcplatform/internal/grpcattrs"
2526
"google.golang.org/grpc/balancer"
2627
"google.golang.org/grpc/balancer/base"
2728
)
@@ -34,12 +35,18 @@ type connInfo struct {
3435
count int
3536
}
3637

37-
func (pb *pickerBuilder) makeConnInfo(pickerInfo base.PickerBuildInfo) ([]*connInfo, int) {
38+
func (*pickerBuilder) makeConnInfo(pickerInfo base.PickerBuildInfo) ([]*connInfo, int) {
3839
connInfoArr := make([]*connInfo, 0, len(pickerInfo.ReadySCs))
40+
3941
var totalWeight int
42+
var config *config.Client
4043

4144
for subConn, subConnInfo := range pickerInfo.ReadySCs {
42-
attributes := subConnInfo.Address.Attributes.Value(struct{}{}).(*attributes.Attributes)
45+
if config == nil {
46+
config = grpcattrs.GetClientConfig(subConnInfo.Address.Attributes)
47+
}
48+
49+
attributes := grpcattrs.GetAttributes(subConnInfo.Address.Attributes)
4350

4451
if attributes.BalancerWeight <= 0 {
4552
continue
@@ -56,8 +63,8 @@ func (pb *pickerBuilder) makeConnInfo(pickerInfo base.PickerBuildInfo) ([]*connI
5663
return cmp.Compare(b.priority, a.priority)
5764
})
5865

59-
if pb.maxActiveServers > 0 && pb.maxActiveServers < len(connInfoArr) {
60-
connInfoArr = connInfoArr[:pb.maxActiveServers]
66+
if config.MaxActiveServers > 0 && config.MaxActiveServers < len(connInfoArr) {
67+
connInfoArr = connInfoArr[:config.MaxActiveServers]
6168
}
6269

6370
for _, connInfo := range connInfoArr {

internal/balancer/register.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import (
2121
"google.golang.org/grpc/balancer/base"
2222
)
2323

24-
func Register(name string, maxActiveServers int) {
25-
balancer.Register(base.NewBalancerBuilder(name, &pickerBuilder{
26-
maxActiveServers: maxActiveServers,
27-
}, base.Config{
24+
func Register() {
25+
balancer.Register(base.NewBalancerBuilder("rpcplatform", &pickerBuilder{}, base.Config{
2826
HealthCheck: true,
2927
}))
3028
}

internal/grpcattrs/attributes.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2025 RPCPlatform Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package grpcattrs
18+
19+
import (
20+
"github.com/nexcode/rpcplatform/internal/attributes"
21+
grpcattrs "google.golang.org/grpc/attributes"
22+
)
23+
24+
func GetAttributes(attrs *grpcattrs.Attributes) *attributes.Attributes {
25+
value, _ := attrs.Value(keyAttributes).(*attributes.Attributes)
26+
return value
27+
}
28+
29+
func SetAttributes(attrs *grpcattrs.Attributes, value *attributes.Attributes) *grpcattrs.Attributes {
30+
return attrs.WithValue(keyAttributes, value)
31+
}

internal/grpcattrs/attrkey.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2025 RPCPlatform Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package grpcattrs
18+
19+
type attrKey uint8
20+
21+
const (
22+
keyAttributes attrKey = iota
23+
keyClientConfig
24+
)

internal/grpcattrs/clientconfig.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2025 RPCPlatform Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package grpcattrs
18+
19+
import (
20+
"github.com/nexcode/rpcplatform/internal/config"
21+
grpcattrs "google.golang.org/grpc/attributes"
22+
)
23+
24+
func GetClientConfig(attrs *grpcattrs.Attributes) *config.Client {
25+
value, _ := attrs.Value(keyClientConfig).(*config.Client)
26+
return value
27+
}
28+
29+
func SetClientConfig(attrs *grpcattrs.Attributes, value *config.Client) *grpcattrs.Attributes {
30+
return attrs.WithValue(keyClientConfig, value)
31+
}

internal/resolver/resolver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ import (
2020
"google.golang.org/grpc/resolver/manual"
2121
)
2222

23-
func NewResolver() *manual.Resolver {
24-
return manual.NewBuilderWithScheme("dns")
23+
func New() *manual.Resolver {
24+
return manual.NewBuilderWithScheme("rpcplatform")
2525
}

0 commit comments

Comments
 (0)