BalancerForward Forward performs the given http request with round robin algorithm to server and fills the given http response. This method will return a fiber.Handler
(servers []string, clients ...*fasthttp.Client)
| 274 | // BalancerForward Forward performs the given http request with round robin algorithm to server and fills the given http response. |
| 275 | // This method will return a fiber.Handler |
| 276 | func BalancerForward(servers []string, clients ...*fasthttp.Client) fiber.Handler { |
| 277 | if len(servers) == 0 { |
| 278 | panic("Servers cannot be empty") |
| 279 | } |
| 280 | r := &roundrobin{ |
| 281 | current: 0, |
| 282 | pool: servers, |
| 283 | } |
| 284 | return func(c fiber.Ctx) error { |
| 285 | server := r.get() |
| 286 | if !strings.HasPrefix(server, "http") { |
| 287 | server = "http://" + server |
| 288 | } |
| 289 | c.Request().Header.Set("X-Real-IP", c.IP()) |
| 290 | return Do(c, server+c.OriginalURL(), clients...) |
| 291 | } |
| 292 | } |