Skip to content

Commit

Permalink
add sticky feature
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Jan 2, 2024
1 parent bb9fb77 commit 8c65376
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ type Option struct {
TCPKeepAlivePeriod time.Duration
// bidirectional mode, if true serverMessageChan will block to wait message for consume. default false.
BidirectionalBlock bool

// alaways use the selected server until it is bad
Sticky bool
}

// Call represents an active RPC.
Expand Down
19 changes: 18 additions & 1 deletion client/xclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type xClient struct {
unstableServers map[string]time.Time // ไธ€ไบ›ๆœๅŠกๅ™จ้‡ๅฏ๏ผŒๅฆ‚ๆžœๅ’Œๅฎƒไปฌๅปบ็ซ‹้“พๆŽฅ๏ผŒๅฏ่ƒฝไผš่€—่ดน้žๅธธ้•ฟ็š„ๆ—ถ้—ด๏ผŒ่ฟ™้‡Œ่ฎฐๅฝ•่ขญๆฅ้œ€่ฆไธดๆ—ถๅฑ่”ฝ
discovery ServiceDiscovery
selector Selector
stickyRPCClient RPCClient
stickyK string

slGroup singleflight.Group

Expand Down Expand Up @@ -240,8 +242,23 @@ func filterByStateAndGroup(group string, servers map[string]string) {
}
}

// selects a client from candidates base on c.selectMode
func (c *xClient) selectClient(ctx context.Context, servicePath, serviceMethod string, args interface{}) (string, RPCClient, error) {
if c.option.Sticky && c.stickyRPCClient != nil {
return c.stickyK, c.stickyRPCClient, nil
}

k, client, err := c.selectOneClient(ctx, servicePath, serviceMethod, args)

if c.option.Sticky {
c.stickyK = k
c.stickyRPCClient = client
}

return k, client, err
}

// selects a client from candidates base on c.selectMode
func (c *xClient) selectOneClient(ctx context.Context, servicePath, serviceMethod string, args interface{}) (string, RPCClient, error) {
c.mu.Lock()
fn := c.selector.Select
if c.Plugins != nil {
Expand Down

0 comments on commit 8c65376

Please sign in to comment.