|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2021 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Adam Janikowski |
| 21 | +// |
| 22 | + |
| 23 | +package test |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "net/http" |
| 28 | + |
| 29 | + "github.com/arangodb/go-driver" |
| 30 | +) |
| 31 | + |
| 32 | +type driverErrorCheckFunc func(err error) (bool, error) |
| 33 | +type driverErrorChecker func(ctx context.Context, client driver.Client) error |
| 34 | + |
| 35 | +func driverErrorCheck(ctx context.Context, c driver.Client, checker driverErrorChecker, checks ...driverErrorCheckFunc) retryFunc { |
| 36 | + return func() error { |
| 37 | + err := checker(ctx, c) |
| 38 | + |
| 39 | + for _, check := range checks { |
| 40 | + if valid, err := check(err); err != nil { |
| 41 | + return err |
| 42 | + } else if !valid { |
| 43 | + return nil |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return interrupt{} |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func driverErrorCheckRetry503(err error) (bool, error) { |
| 52 | + if err == nil { |
| 53 | + return true, nil |
| 54 | + } |
| 55 | + |
| 56 | + if ae, ok := driver.AsArangoError(err); !ok { |
| 57 | + return false, err |
| 58 | + } else { |
| 59 | + if !ae.HasError { |
| 60 | + return true, nil |
| 61 | + } |
| 62 | + switch ae.Code { |
| 63 | + case http.StatusServiceUnavailable: |
| 64 | + return false, nil |
| 65 | + default: |
| 66 | + return true, err |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments