Skip to content

Commit

Permalink
Factor out response error handling into a function
Browse files Browse the repository at this point in the history
This code will be reused by the Batch code.
  • Loading branch information
aaronbee committed Jun 23, 2023
1 parent 6676fa3 commit c9faad0
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ func (c *client) SendRPC(rpc hrpc.Call) (msg proto.Message, err error) {
}
}

func (c *client) handleResultError(err error, reg hrpc.RegionInfo, rc hrpc.RegionClient) {
// Check for errors
switch err.(type) {
case region.NotServingRegionError:
// There's an error specific to this region, but
// our region client is fine. Mark this region as
// unavailable (as opposed to all regions sharing
// the client), and start a goroutine to reestablish
// it.
if reg.MarkUnavailable() {
go c.reestablishRegion(reg)
}
case region.ServerError:
// If it was an unrecoverable error, the region client is
// considered dead.
if reg == c.adminRegionInfo {
// If this is the admin client, mark the region
// as unavailable and start up a goroutine to
// reconnect if it wasn't already marked as such.
if reg.MarkUnavailable() {
go c.reestablishRegion(reg)
}
} else {
c.clientDown(rc)
}
}
}

func sendBlocking(ctx context.Context, rc hrpc.RegionClient, rpc hrpc.Call) (
hrpc.RPCResult, error) {
rc.QueueRPC(rpc)
Expand Down Expand Up @@ -166,30 +194,8 @@ func (c *client) sendRPCToRegion(ctx context.Context, rpc hrpc.Call, reg hrpc.Re
if err != nil {
return nil, err
}
// Check for errors
switch res.Error.(type) {
case region.NotServingRegionError:
// There's an error specific to this region, but
// our region client is fine. Mark this region as
// unavailable (as opposed to all regions sharing
// the client), and start a goroutine to reestablish
// it.
if reg.MarkUnavailable() {
go c.reestablishRegion(reg)
}
case region.ServerError:
// If it was an unrecoverable error, the region client is
// considered dead.
if reg == c.adminRegionInfo {
// If this is the admin client, mark the region
// as unavailable and start up a goroutine to
// reconnect if it wasn't already marked as such.
if reg.MarkUnavailable() {
go c.reestablishRegion(reg)
}
} else {
c.clientDown(client)
}
if res.Error != nil {
c.handleResultError(res.Error, reg, client)
}
return res.Msg, res.Error
}
Expand Down

0 comments on commit c9faad0

Please sign in to comment.