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 8c65376 commit 24009f7
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions client/xclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,14 @@ 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) {
c.mu.Lock()
if c.option.Sticky && c.stickyRPCClient != nil {
c.mu.Unlock()
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 {
fn = c.Plugins.DoWrapSelect(fn)
Expand All @@ -273,11 +263,10 @@ func (c *xClient) selectOneClient(ctx context.Context, servicePath, serviceMetho

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

// if err != nil {
// c.mu.Lock()
// c.unstableServers[k] = time.Now() // ๆญคๆœๅŠกๅ™จๆœ‰้—ฎ้ข˜๏ผŒๆš‚ๆ—ถๅฑ่”ฝ
// c.mu.Unlock()
// }
if c.option.Sticky && client != nil {
c.stickyK = k
c.stickyRPCClient = client
}

return k, client, err
}
Expand Down Expand Up @@ -378,6 +367,11 @@ func (c *xClient) deleteCachedClient(client RPCClient, k, servicePath, serviceMe

func (c *xClient) removeClient(k, servicePath, serviceMethod string, client RPCClient) {
c.mu.Lock()
if c.option.Sticky {
c.stickyK = ""
c.stickyRPCClient = nil
}

cl := c.findCachedClient(k, servicePath, serviceMethod)
if cl == client {
c.deleteCachedClient(client, k, servicePath, serviceMethod)
Expand Down

0 comments on commit 24009f7

Please sign in to comment.