cubefs

Форк
0
79 строк · 2.6 Кб
1
/*
2
Package hystrix is a latency and fault tolerance library designed to isolate
3
points of access to remote systems, services and 3rd party libraries, stop
4
cascading failure and enable resilience in complex distributed systems where
5
failure is inevitable.
6

7
Based on the java project of the same name, by Netflix. https://github.com/Netflix/Hystrix
8

9
Execute code as a Hystrix command
10

11
Define your application logic which relies on external systems, passing your function to Go. When that system is healthy this will be the only thing which executes.
12

13
	hystrix.Go("my_command", func() error {
14
		// talk to other services
15
		return nil
16
	}, nil)
17

18
Defining fallback behavior
19

20
If you want code to execute during a service outage, pass in a second function to Go. Ideally, the logic here will allow your application to gracefully handle external services being unavailable.
21

22
This triggers when your code returns an error, or whenever it is unable to complete based on a variety of health checks https://github.com/Netflix/Hystrix/wiki/How-it-Works.
23

24
	hystrix.Go("my_command", func() error {
25
		// talk to other services
26
		return nil
27
	}, func(err error) error {
28
		// do this when services are down
29
		return nil
30
	})
31

32
Waiting for output
33

34
Calling Go is like launching a goroutine, except you receive a channel of errors you can choose to monitor.
35

36
	output := make(chan bool, 1)
37
	errors := hystrix.Go("my_command", func() error {
38
		// talk to other services
39
		output <- true
40
		return nil
41
	}, nil)
42

43
	select {
44
	case out := <-output:
45
		// success
46
	case err := <-errors:
47
		// failure
48
	}
49

50
Synchronous API
51

52
Since calling a command and immediately waiting for it to finish is a common pattern, a synchronous API is available with the Do function which returns a single error.
53

54
	err := hystrix.Do("my_command", func() error {
55
		// talk to other services
56
		return nil
57
	}, nil)
58

59
Configure settings
60

61
During application boot, you can call ConfigureCommand to tweak the settings for each command.
62

63
	hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{
64
		Timeout:               1000,
65
		MaxConcurrentRequests: 100,
66
		ErrorPercentThreshold: 25,
67
	})
68

69
You can also use Configure which accepts a map[string]CommandConfig.
70

71
Enable dashboard metrics
72

73
In your main.go, register the event stream HTTP handler on a port and launch it in a goroutine.  Once you configure turbine for your Hystrix Dashboard https://github.com/Netflix/Hystrix/tree/master/hystrix-dashboard to start streaming events, your commands will automatically begin appearing.
74

75
	hystrixStreamHandler := hystrix.NewStreamHandler()
76
	hystrixStreamHandler.Start()
77
	go http.ListenAndServe(net.JoinHostPort("", "81"), hystrixStreamHandler)
78
*/
79
package hystrix
80

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.