Skip to content

Commit 54199fe

Browse files
committed
Breaking API changes and lookup method for observing running servers
1 parent a42d33d commit 54199fe

36 files changed

Lines changed: 518 additions & 396 deletions

attributes/attributes.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 attributes
18+
19+
// New returns attributes with default values.
20+
func New() *Attributes {
21+
return &Attributes{
22+
BalancerWeight: 1,
23+
}
24+
}
25+
26+
// Attributes provides server attribute values.
27+
type Attributes struct {
28+
BalancerPriority int
29+
BalancerWeight int
30+
}
Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 RPCPlatform Authors
2+
* Copyright 2025 RPCPlatform Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,22 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
package options
17+
package attributes
1818

19-
import (
20-
"github.com/nexcode/rpcplatform/internal/config"
21-
etcd "go.etcd.io/etcd/client/v3"
22-
)
19+
import "strconv"
2320

24-
func Make(etcdClient *etcd.Client, etcdPrefix string, options []Option) *config.Config {
25-
config := &config.Config{
26-
EtcdClient: etcdClient,
27-
EtcdPrefix: etcdPrefix,
21+
// Load used internally to load keys and values from strings.
22+
func (sa *Attributes) Load(key, value string) {
23+
switch key {
24+
case balancerPriority:
25+
if v, err := strconv.Atoi(value); err == nil {
26+
sa.BalancerPriority = v
27+
}
28+
case balancerWeight:
29+
if v, err := strconv.Atoi(value); err == nil {
30+
sa.BalancerWeight = v
31+
}
2832
}
29-
30-
for _, option := range options {
31-
option.apply(config)
32-
}
33-
34-
return config
3533
}

attributes/attributes_values.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 attributes
18+
19+
import "strconv"
20+
21+
// Values used internally to create an string array of keys and values.
22+
func (sa *Attributes) Values() []string {
23+
return []string{
24+
balancerPriority, strconv.Itoa(sa.BalancerPriority),
25+
balancerWeight, strconv.Itoa(sa.BalancerWeight),
26+
}
27+
}

errors/errors.go renamed to attributes/const.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 RPCPlatform Authors
2+
* Copyright 2025 RPCPlatform Authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,10 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package errors
17+
package attributes
1818

19-
import "errors"
20-
21-
var (
22-
ErrGRPCOptionsExpected = errors.New("*[]grpc.DialOption or *[]grpc.ServerOption expected")
19+
const (
20+
balancerPriority = "balancerPriority"
21+
balancerWeight = "balancerWeight"
2322
)

attributes_server.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

client.go

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

1919
import (
20-
etcd "go.etcd.io/etcd/client/v3"
2120
"google.golang.org/grpc"
2221
"google.golang.org/grpc/resolver/manual"
2322
)
2423

2524
type Client struct {
2625
target string
27-
etcd *etcd.Client
2826
client *grpc.ClientConn
2927
resolver *manual.Resolver
3028
}

client_stateinit.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

client_statewatcher.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

client_updatestate.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,21 @@
1717
package rpcplatform
1818

1919
import (
20+
"github.com/nexcode/rpcplatform/internal/serverinfo"
21+
"google.golang.org/grpc/attributes"
2022
"google.golang.org/grpc/resolver"
2123
)
2224

23-
func (c *Client) updateState(init bool, serverInfo map[string]string) {
24-
tree := c.makeServerInfo(serverInfo)
25-
25+
func (c *Client) updateState(init bool, serverInfoTree map[string]*serverinfo.ServerInfo) {
2626
state := resolver.State{
27-
Addresses: make([]resolver.Address, 0, len(tree)),
27+
Addresses: make([]resolver.Address, 0, len(serverInfoTree)),
2828
}
2929

30-
for _, value := range tree {
31-
address := resolver.Address{
32-
Addr: value.address,
33-
}
34-
35-
for key, value := range value.attributes {
36-
address.Attributes = address.Attributes.WithValue(key, value)
37-
}
38-
39-
state.Addresses = append(state.Addresses, address)
30+
for _, value := range serverInfoTree {
31+
state.Addresses = append(state.Addresses, resolver.Address{
32+
Addr: value.Address,
33+
Attributes: attributes.New(struct{}{}, value.Attributes),
34+
})
4035
}
4136

4237
if init {

examples/attributes/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
## Adding attributes
22

3-
This example is very similar to [QuickStart](../quickstart), but now we will use additional attributes for client and server.
3+
This example is very similar to [QuickStart](../quickstart), but now we will use additional attributes for server.
44

55
For the client, we will set the maximum number of active servers (all other servers above this value will be backup servers):
66

77
```go
8-
attributes := rpcplatform.Attributes().Client()
9-
attributes.SetMaxActiveServers(2)
10-
11-
client, err := rpcp.NewClient("server", attributes)
8+
client, err := rpcp.NewClient("server", options.Client.MaxActiveServers(2))
129
if err != nil {
13-
panic(err)
10+
panic(err)
1411
}
1512
```
1613

17-
And for the server we will set the balancing weight:
14+
And for the server we will set the balancing weight attribute:
1815

1916
```go
20-
attributes := rpcplatform.Attributes().Server()
21-
attributes.SetBalancerWeight(4)
17+
attributes := attributes.New()
18+
attributes.BalancerWeight = 4
2219

23-
server, err := rpcp.NewServer("server", "localhost:", attributes)
20+
server, err := rpcp.NewServer("server", "localhost:", options.Server.Attributes(attributes))
2421
if err != nil {
25-
panic(err)
22+
panic(err)
2623
}
2724
```
2825

0 commit comments

Comments
 (0)