Skip to content

Commit

Permalink
more 'not ready' conditions added
Browse files Browse the repository at this point in the history
  • Loading branch information
rschmied committed May 17, 2023
1 parent 99d2f1e commit 96c9bd3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Lists the changes in the gocmlclient package.

## Version 0.0.20 and 0.0.21

- added more cases where an error results in ErrSystemNotReady
- use http.ProxyFromEnvironment for API requests

## Version 0.0.19

fix a stupid regression
Expand Down
10 changes: 10 additions & 0 deletions apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"log"
"net/http"
"net/url"
"strings"
"sync/atomic"
"syscall"
Expand Down Expand Up @@ -77,6 +78,11 @@ retry:
}
res, err := c.httpClient.Do(req)
if err != nil {
if urlError, ok := (err).(*url.Error); ok {
if urlError.Timeout() {
return nil, ErrSystemNotReady
}
}
if errors.Is(err, syscall.ECONNREFUSED) {
return nil, ErrSystemNotReady
}
Expand Down Expand Up @@ -122,6 +128,10 @@ retry:
case http.StatusCreated:
return body, err
case http.StatusBadGateway:
fallthrough
case http.StatusServiceUnavailable:
fallthrough
case http.StatusGatewayTimeout:
return nil, ErrSystemNotReady
}
return nil, fmt.Errorf(
Expand Down
29 changes: 16 additions & 13 deletions cmd/ctest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package main

import (
"context"
"encoding/json"
"fmt"
"errors"
"log"
"os"

Expand Down Expand Up @@ -34,10 +33,10 @@ func main() {
return
}
ctx := context.Background()
client := cmlclient.New(host, false, false)
if err := client.Ready(ctx); err != nil {
log.Fatal(err)
}
client := cmlclient.New(host, true, false)
// if err := client.Ready(ctx); err != nil {
// log.Fatal(err)
// }
if token_found {
client.SetToken(token)
} else {
Expand Down Expand Up @@ -85,15 +84,19 @@ func main() {
// return
// }

result, err := client.UserGroups(ctx, "cc42bd56-1dc6-445c-b7e7-569b0a8b0c94")
if err != nil {
// result, err := client.UserGroups(ctx, "cc42bd56-1dc6-445c-b7e7-569b0a8b0c94")
err := client.Ready(ctx)
if errors.Is(err, cmlclient.ErrSystemNotReady) {
log.Println("it is not ready")
}
if err != nil && !errors.Is(err, cmlclient.ErrSystemNotReady) {
log.Println(err)
return
}

je, err := json.Marshal(result)
if err != nil {
log.Println(err)
}
fmt.Println(string(je))
// je, err := json.Marshal(result)
// if err != nil {
// log.Println(err)
// }
// fmt.Println(string(je))
}

0 comments on commit 96c9bd3

Please sign in to comment.