| 465 | } |
| 466 | |
| 467 | func (s *Server) WaitForReady(ctx context.Context) <-chan struct{} { |
| 468 | c := make(chan struct{}) |
| 469 | detect := func() bool { |
| 470 | u := (&url.URL{ |
| 471 | Scheme: "http", |
| 472 | Host: s.options.httpListener.Addr().String(), |
| 473 | Path: "/api/v1/status", |
| 474 | }).String() |
| 475 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) |
| 476 | if err != nil { |
| 477 | s.log.Error(err, "Failed to create detect request") |
| 478 | return false |
| 479 | } |
| 480 | client := &http.Client{} |
| 481 | _, err = client.Do(req) |
| 482 | if err != nil { |
| 483 | s.log.Info("Detect connection to server failed", "error", err) |
| 484 | } |
| 485 | s.log.Info("Server is ready", "address", s.options.httpListener.Addr().String()) |
| 486 | return true |
| 487 | } |
| 488 | go func() { |
| 489 | defer close(c) |
| 490 | |
| 491 | if detect() { |
| 492 | return |
| 493 | } |
| 494 | // Try to connect to the server |
| 495 | for { |
| 496 | select { |
| 497 | case <-ctx.Done(): |
| 498 | return |
| 499 | case <-time.After(1 * time.Second): |
| 500 | if detect() { |
| 501 | return |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | }() |
| 506 | return c |
| 507 | } |
| 508 | |
| 509 | func (s *Server) Close() error { |
| 510 | s.log.Info("Shutting down function stream server") |