github.com/weaviate/weaviate@v1.24.6/modules/multi2vec-bind/clients/startup.go (about)

     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  
    12  package clients
    13  
    14  import (
    15  	"context"
    16  	"net/http"
    17  	"time"
    18  
    19  	"github.com/pkg/errors"
    20  )
    21  
    22  func (v *vectorizer) WaitForStartup(initCtx context.Context,
    23  	interval time.Duration,
    24  ) error {
    25  	t := time.NewTicker(interval)
    26  	defer t.Stop()
    27  	expired := initCtx.Done()
    28  	var lastErr error
    29  	for {
    30  		select {
    31  		case <-t.C:
    32  			lastErr = v.checkReady(initCtx)
    33  			if lastErr == nil {
    34  				return nil
    35  			}
    36  			v.logger.
    37  				WithField("action", "multi2vec_remote_wait_for_startup").
    38  				WithError(lastErr).Warnf("multi2vec-bind inference service not ready")
    39  		case <-expired:
    40  			return errors.Wrapf(lastErr, "init context expired before remote was ready")
    41  		}
    42  	}
    43  }
    44  
    45  func (v *vectorizer) 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
    48  	requestCtx, cancel := context.WithTimeout(initCtx, 500*time.Millisecond)
    49  	defer cancel()
    50  
    51  	req, err := http.NewRequestWithContext(requestCtx, http.MethodGet,
    52  		v.url("/.well-known/ready"), nil)
    53  	if err != nil {
    54  		return errors.Wrap(err, "create check ready request")
    55  	}
    56  
    57  	res, err := v.httpClient.Do(req)
    58  	if err != nil {
    59  		return errors.Wrap(err, "send check ready request")
    60  	}
    61  
    62  	defer res.Body.Close()
    63  	if res.StatusCode > 299 {
    64  		return errors.Errorf("not ready: status %d", res.StatusCode)
    65  	}
    66  
    67  	return nil
    68  }