weaviate
68 строк · 1.6 Кб
1// _ _
2// __ _____ __ ___ ___ __ _| |_ ___
3// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
4// \ V V / __/ (_| |\ V /| | (_| | || __/
5// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
6//
7// Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
8//
9// CONTACT: hello@weaviate.io
10//
11
12package clients
13
14import (
15"context"
16"net/http"
17"time"
18
19"github.com/pkg/errors"
20)
21
22func (c *client) WaitForStartup(initCtx context.Context,
23interval time.Duration,
24) error {
25t := time.NewTicker(interval)
26defer t.Stop()
27expired := initCtx.Done()
28var lastErr error
29for {
30select {
31case <-t.C:
32lastErr = c.checkReady(initCtx)
33if lastErr == nil {
34return nil
35}
36c.logger.
37WithField("action", "gpt4all_remote_wait_for_startup").
38WithError(lastErr).Warnf("gpt4all remote service not ready")
39case <-expired:
40return errors.Wrapf(lastErr, "init context expired before remote was ready")
41}
42}
43}
44
45func (c *client) checkReady(initCtx context.Context) error {
46// spawn a new context (derived on the overall context) which is used to
47// consider an individual request timed out
48requestCtx, cancel := context.WithTimeout(initCtx, 500*time.Millisecond)
49defer cancel()
50
51req, err := http.NewRequestWithContext(requestCtx, http.MethodGet,
52c.url("/.well-known/ready"), nil)
53if err != nil {
54return errors.Wrap(err, "create check ready request")
55}
56
57res, err := c.httpClient.Do(req)
58if err != nil {
59return errors.Wrap(err, "send check ready request")
60}
61
62defer res.Body.Close()
63if res.StatusCode > 299 {
64return errors.Errorf("not ready: status %d", res.StatusCode)
65}
66
67return nil
68}
69