forwardToLeader forwards the current request to the core's leader process. It propagates the same credentials used in the current request. For that reason, it cannot be used outside of a request- handling context.
(ctx context.Context, path string, body interface{}, resp interface{})
| 350 | // request. For that reason, it cannot be used outside of a request- |
| 351 | // handling context. |
| 352 | func (a *API) forwardToLeader(ctx context.Context, path string, body interface{}, resp interface{}) error { |
| 353 | addr, err := a.leader.Address(ctx) |
| 354 | if err != nil { |
| 355 | return errors.Wrap(err) |
| 356 | } |
| 357 | |
| 358 | // Don't infinite loop if the leader's address is our own address. |
| 359 | // This is possible if we just became the leader. The client should |
| 360 | // just retry. |
| 361 | if addr == a.addr { |
| 362 | return leader.ErrNoLeader |
| 363 | } |
| 364 | |
| 365 | l := &rpc.Client{ |
| 366 | BaseURL: "http://" + addr, |
| 367 | Client: a.httpClient, |
| 368 | } |
| 369 | if a.useTLS { |
| 370 | l.BaseURL = "https://" + addr |
| 371 | } |
| 372 | |
| 373 | // Forward the request credentials if we have them. |
| 374 | // TODO(jackson): Don't use the incoming request's credentials and |
| 375 | // have an alternative authentication scheme between processes of the |
| 376 | // same Core. For now, we only call the leader for the purpose of |
| 377 | // forwarding a request, so this is OK. |
| 378 | req := httpjson.Request(ctx) |
| 379 | user, pass, ok := req.BasicAuth() |
| 380 | if ok { |
| 381 | l.AccessToken = fmt.Sprintf("%s:%s", user, pass) |
| 382 | } |
| 383 | |
| 384 | return l.Call(ctx, path, body, resp) |
| 385 | } |
| 386 | |
| 387 | func healthHandler(handler http.Handler) http.Handler { |
| 388 | return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |