Skip to content

错误异常

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// https://github.com/afex/hystrix-go/hystrix/hystrix.go

// A CircuitError is an error which models various failure states of execution,
// such as the circuit being open or a timeout.
type CircuitError struct {
    Message string
}

func (e CircuitError) Error() string {
    return "hystrix: " + e.Message
}
var (
    // ErrMaxConcurrency occurs when too many of the same named command are executed at the same time.
    ErrMaxConcurrency = CircuitError{Message: "max concurrency"}
    // ErrCircuitOpen returns when an execution attempt "short circuits". This happens due to the circuit being measured as unhealthy.
    ErrCircuitOpen = CircuitError{Message: "circuit open"}
    // ErrTimeout occurs when the provided function takes too long to execute.
    ErrTimeout = CircuitError{Message: "timeout"}
)

// errorWithFallback triggers the fallback while reporting the appropriate metric events.
func (c *command) errorWithFallback(ctx context.Context, err error) {
    eventType := "failure"
    if err == ErrCircuitOpen {
        eventType = "short-circuit"
    } else if err == ErrMaxConcurrency {
        eventType = "rejected"
    } else if err == ErrTimeout {
        eventType = "timeout"
    } else if err == context.Canceled {
        eventType = "context_canceled"
    } else if err == context.DeadlineExceeded {
        eventType = "context_deadline_exceeded"
    }

    c.reportEvent(eventType)
    fallbackErr := c.tryFallback(ctx, err)
    if fallbackErr != nil {
        c.errChan <- fallbackErr
    }
}

TimeoutException

If the run() or construct() method exceeds the command’s timeout value, the thread will throw a TimeoutException (or a separate timer thread will, if the command itself is not running in its own thread).

latency 很低,为什么会有 hystrix timeout error

1
2
3
4
5
6
7
8
9
Even if the latency is very low, Hystrix timeout error can still occur due to various reasons such as server-overload, network connectivity issue, request queuing, etc.

For example, if there are too many concurrent requests being sent to DynamoDB, this can lead to an overload on the server, which can cause the requests to take longer to complete than expected.

Another possible reason for Hystrix timeout error could be a network connectivity issue.  If there is a problem with the network connectivity between your application and DynamoDB, this can cause delays or timeouts to occur.

Hystrix timeouts can also occur due to a request queuing issue, where there are too many requests in the queue waiting to be processed.

Therefore, it is recommended to diagnose the root cause of the timeout error by analyzing the logs and identifying the bottleneck causing the error.  This can help in taking the appropriate corrective measures to fix the problem.

什么情况下会返回 hystrix failure error

当使用 Hystrix 进行服务调用时,可能会出现以下情况导致 Hystrix 返回 failure error:

  • 超时:当服务调用超过预设的超时时间,Hystrix 会返回超时错误。
  • 熔断:当服务的错误率达到一定阈值时,Hystrix 会触发熔断机制,停止向该服务发起请求,直接返回熔断错误。
  • 信号量:当服务的并发请求超过预设的信号量时,Hystrix 会拒绝请求,并返回信号量错误。
  • 程序异常:当服务调用出现未处理的异常时,Hystrix 会返回程序异常错误。
  • 降级:当服务不可用或者熔断时,Hystrix 会调用预设的降级逻辑,返回降级错误。

什么情况下会返回 hystrix timeout error

以下是一些可能导致Hystrix超时错误的情况:

  • 调用的服务响应时间过长,超过了Hystrix配置的超时时间。
  • 调用的服务出现了故障或宕机,导致无法响应请求。
  • 服务调用过程中网络延迟或网络故障,导致请求超时。
  • 服务的负载过高,导致无法及时响应请求。

  • 要解决Hystrix超时错误,可以尝试以下几种方法:

  • 增加超时时间,以便允许服务有更多的时间来响应请求。

  • 优化服务的性能,以便能够更快地响应请求。
  • 降低服务的负载,以便能够更好地处理请求。
  • 优化网络连接,减少网络延迟和故障的出现。