1package fasthttp_test 2 3import ( 4 "fmt" 5 "log" 6 7 "github.com/valyala/fasthttp" 8) 9 10func ExampleLBClient() { 11 // Requests will be spread among these servers. 12 servers := []string{ 13 "google.com:80", 14 "foobar.com:8080", 15 "127.0.0.1:123", 16 } 17 18 // Prepare clients for each server 19 var lbc fasthttp.LBClient 20 for _, addr := range servers { 21 c := &fasthttp.HostClient{ 22 Addr: addr, 23 } 24 lbc.Clients = append(lbc.Clients, c) 25 } 26 27 // Send requests to load-balanced servers 28 var req fasthttp.Request 29 var resp fasthttp.Response 30 for i := 0; i < 10; i++ { 31 url := fmt.Sprintf("http://abcedfg/foo/bar/%d", i) 32 req.SetRequestURI(url) 33 if err := lbc.Do(&req, &resp); err != nil { 34 log.Fatalf("Error when sending request: %s", err) 35 } 36 if resp.StatusCode() != fasthttp.StatusOK { 37 log.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode(), fasthttp.StatusOK) 38 } 39 40 useResponseBody(resp.Body()) 41 } 42} 43