-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
code optimization
- Loading branch information
Showing
10 changed files
with
136 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,64 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/werbenhu/srouter" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type Client struct { | ||
Addr string | ||
conn *grpc.ClientConn | ||
rpc srouter.RouterClient | ||
} | ||
|
||
func (c *Client) Match() { | ||
func New(router string) (*Client, error) { | ||
client := &Client{Addr: router} | ||
conn, err := grpc.Dial(client.Addr, grpc.WithInsecure()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client.conn = conn | ||
client.rpc = srouter.NewRouterClient(conn) | ||
return client, nil | ||
} | ||
|
||
func (c *Client) Members() { | ||
func (c *Client) Close() { | ||
c.conn.Close() | ||
} | ||
|
||
func (c *Client) Match(group string, key string) (*srouter.Service, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
service, err := c.rpc.Match(ctx, &srouter.MatchRequest{ | ||
Group: group, | ||
Key: key, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return srouter.NewService(service.Id, service.Group, service.Addr), nil | ||
} | ||
|
||
func (c *Client) Members(group string) ([]*srouter.Service, error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
services := make([]*srouter.Service, 0) | ||
|
||
members, err := c.rpc.Members(ctx, &srouter.MembersRequest{ | ||
Group: group, | ||
}) | ||
if err != nil { | ||
return services, err | ||
} | ||
for _, member := range members.Services { | ||
services = append(services, srouter.NewService(member.Id, member.Group, member.Addr)) | ||
} | ||
|
||
return services, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
// SPDX-FileCopyrightText: 2023 werbenhu | ||
// SPDX-FileContributor: werbenhu | ||
|
||
package srouter | ||
|
||
type err struct { | ||
Msg string | ||
Code int | ||
} | ||
|
||
func (e err) String() string { | ||
return e.Msg | ||
} | ||
|
||
func (e err) Error() string { | ||
return e.Msg | ||
} | ||
|
||
var ( | ||
ErrReplicasParam = err{Code: 10000, Msg: "agent replicas param error"} | ||
ErrGroupNameEmpty = err{Code: 10001, Msg: "agent group name empty"} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
// SPDX-FileCopyrightText: 2023 werbenhu | ||
// SPDX-FileContributor: werbenhu | ||
|
||
package srouter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestErrString(t *testing.T) { | ||
c := err{ | ||
Msg: "test", | ||
Code: 0x1, | ||
} | ||
|
||
require.Equal(t, "test", c.String()) | ||
} | ||
|
||
func TestErrErrorr(t *testing.T) { | ||
c := err{ | ||
Msg: "error", | ||
Code: 0x1, | ||
} | ||
|
||
require.Equal(t, "error", error(c).Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters