func BenchmarkDefaultLoadBalancer(b *testing.B) { discovery := dnsdisco.NewDiscovery("jabber", "tcp", "registro.br") discovery.SetHealthChecker(dnsdisco.HealthCheckerFunc(func(target string, port uint16, proto string) (ok bool, err error) { return true, nil })) discovery.SetRetriever(dnsdisco.RetrieverFunc(func(service, proto, name string) ([]*net.SRV, error) { return []*net.SRV{ { Target: "server1.example.com.", Port: 1111, Weight: 10, Priority: 20, }, { Target: "server2.example.com.", Port: 2222, Weight: 70, Priority: 10, }, { Target: "server3.example.com.", Port: 3333, Weight: 100, Priority: 20, }, { Target: "server4.example.com.", Port: 4444, Weight: 1, Priority: 15, }, { Target: "server5.example.com.", Port: 5555, Weight: 40, Priority: 60, }, }, nil })) // Retrieve the servers if err := discovery.Refresh(); err != nil { fmt.Println(err) return } for i := 0; i < b.N; i++ { discovery.Choose() } }
// ExampleHealthCheckerFunc tests HTTP fetching the homepage and checking the // HTTP status code. func ExampleHealthCheckerFunc() { discovery := dnsdisco.NewDiscovery("http", "tcp", "pantz.org") discovery.SetHealthChecker(dnsdisco.HealthCheckerFunc(func(target string, port uint16, proto string) (ok bool, err error) { response, err := http.Get("http://www.pantz.org") if err != nil { return false, err } return response.StatusCode == http.StatusOK, nil })) // Retrieve the servers if err := discovery.Refresh(); err != nil { fmt.Println(err) return } target, port := discovery.Choose() fmt.Printf("Target: %s\nPort: %d\n", target, port) // Output: // Target: www.pantz.org. // Port: 80 }
Target: "server3.example.com.", Port: 3333, Priority: 15, Weight: 20, }, { Target: "server4.example.com.", Port: 4444, Priority: 10, Weight: 10, }, }, nil }) }(), healthChecker: dnsdisco.HealthCheckerFunc(func(target string, port uint16, proto string) (ok bool, err error) { return true, nil }), }, { description: "it should fail to retrieve the SRV records", service: "jabber", proto: "tcp", name: "registro.br", expectedTarget: "server1.example.com.", expectedPort: 1111, refreshInterval: 100 * time.Millisecond, retriever: func() dnsdisco.RetrieverFunc { calls := 0 return dnsdisco.RetrieverFunc(func(service, proto, name string) ([]*net.SRV, error) { calls++ if calls == 1 {
{ Target: "server1.example.com.", Port: 1111, Priority: 10, Weight: 20, }, { Target: "server2.example.com.", Port: 2222, Priority: 10, Weight: 10, }, }, nil }), healthChecker: dnsdisco.HealthCheckerFunc(func(target string, port uint16, proto string) (ok bool, err error) { return target == "server2.example.com.", nil }), expectedTarget: "server2.example.com.", expectedPort: 2222, }, { description: "it should fallback to other priority group by health check", service: "jabber", proto: "tcp", name: "registro.br", retriever: dnsdisco.RetrieverFunc(func(service, proto, name string) ([]*net.SRV, error) { return []*net.SRV{ { Target: "server1.example.com.", Port: 1111, Priority: 10,