Enhance Vault API client to auto retry upon rate limit.
package vault
import (
+ "context"
"crypto/tls"
"fmt"
"io/ioutil"
@@ -11,6 +12,7 @@ import (
"time"
"code.cloudfoundry.org/lager"
+ "github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/go-rootcerts"
vaultapi "github.com/hashicorp/vault/api"
)
@@ -218,6 +220,18 @@ func (ac *APIClient) baseClient() (*vaultapi.Client, error) {
return nil, err
}
+ config.CheckRetry = func(ctx context.Context, resp *http.Response, err error) (bool, error) {
+ retryable, err := retryablehttp.DefaultRetryPolicy(ctx, resp, err)
+ if err != nil || retryable {
+ return retryable, err
+ }
+ // We also want to retry when hitting Vault rate limit.
+ if resp.StatusCode == 429 {
+ return true, nil
+ }
+ return false, nil
+ }
+
client, err := vaultapi.NewClient(config)
if err != nil {
return nil, err
