Skip to content

Commit

Permalink
熔断consumer错误判断增加response的状态判断 (#106)
Browse files Browse the repository at this point in the history
* 熔断错误判断

* 熔断错误判断

* 熔断判断 + 限流反注册

* resp close

Co-authored-by: zhanglei25 <[email protected]>
  • Loading branch information
reallovelei and reallovelei authored Nov 2, 2022
1 parent 3046b5e commit d7cc5a1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 5 additions & 3 deletions examples/circuitbreaker/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func (svr *PolarisConsumer) runWebServer() {

start := time.Now()
resp, err := http.Get(fmt.Sprintf("http://%s:%d/echo", instance.GetHost(), instance.GetPort()))
if err != nil {
if resp != nil {
defer resp.Body.Close()
}

if err != nil || resp.StatusCode != http.StatusOK {
delay := time.Now().Sub(start)
callRet := &polaris.ServiceCallResult{}
callRet.CalledInstance = instance
Expand All @@ -97,8 +101,6 @@ func (svr *PolarisConsumer) runWebServer() {
continue
}

defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("[error] read resp from %s:%d fail : %s", instance.GetHost(), instance.GetPort(), err)
Expand Down
18 changes: 16 additions & 2 deletions examples/ratelimit/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,21 @@ func (svr *PolarisProvider) registerService() {
log.Printf("register response: instanceId %s", resp.InstanceID)
}

func runMainLoop() {
func (svr *PolarisProvider) deregisterService() {
log.Printf("start to invoke deregister operation")
deregisterRequest := &polaris.InstanceDeRegisterRequest{}
deregisterRequest.Service = service
deregisterRequest.Namespace = namespace
deregisterRequest.Host = svr.host
deregisterRequest.Port = svr.port
deregisterRequest.ServiceToken = token
if err := svr.provider.Deregister(deregisterRequest); err != nil {
log.Fatalf("fail to deregister instance, err is %v", err)
}
log.Printf("deregister successfully.")
}

func (svr *PolarisProvider) runMainLoop() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, []os.Signal{
syscall.SIGINT, syscall.SIGTERM,
Expand Down Expand Up @@ -176,7 +190,7 @@ func main() {

svr.Run()

runMainLoop()
svr.runMainLoop()
}

func getLocalHost(serverAddr string) (string, error) {
Expand Down

0 comments on commit d7cc5a1

Please sign in to comment.