github.com/bkosm/gompose/v2@v2.3.1/readyonhttp.go (about)

     1  package gompose
     2  
     3  import "net/http"
     4  
     5  // ReadyOnHttp returns a channel that will be closed when the configured http check on the containers is successful.
     6  // An error will be returned if the request is successful and the response verifier returns an error,
     7  // and a ErrWaitTimedOut error will be returned if the timeout is reached.
     8  // When it comes to timing defaults, those from ReadyOnStdout apply here too.
     9  // If the request fails due to a network error, the request will be retried until the timeout is reached.
    10  func ReadyOnHttp(request http.Request, opts ...Option) ReadyOrErrChan {
    11  	verifier := responseVerifier(DefaultResponseVerifier)
    12  	options := timeBased{
    13  		times:        1,
    14  		timeout:      DefaultWaitTimeout,
    15  		pollInterval: DefaultPollInterval,
    16  	}
    17  	for _, opt := range opts {
    18  		if fn := opt.withTimeBasedFunc; fn != nil {
    19  			fn(&options)
    20  		}
    21  		if fn := opt.withResponseVerifierFunc; fn != nil {
    22  			fn(&verifier)
    23  		}
    24  	}
    25  
    26  	readyOrErr := make(chan error)
    27  
    28  	go seekOrTimeout(options.timeout, options.pollInterval, readyOrErr, func() (bool, error) {
    29  		resp, err := http.DefaultClient.Do(&request)
    30  		if err != nil {
    31  			return false, nil // retry it anyways
    32  		}
    33  
    34  		ready, err := verifier(resp)
    35  		if err != nil {
    36  			return false, err // can not proceed with waiting, by custom measure
    37  		}
    38  
    39  		return ready, nil
    40  	})
    41  
    42  	return readyOrErr
    43  }