Skip to content

Commit

Permalink
chore, storage: update dep and add token cache for gcs
Browse files Browse the repository at this point in the history
Signed-off-by: huanghaoyuan <haoyuan.huang@zilliz.com>
  • Loading branch information
huanghaoyuanhhy committed Mar 13, 2023
1 parent 1b494f3 commit 92a8dcb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 65 deletions.
26 changes: 15 additions & 11 deletions core/storage/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pkg/errors"
"go.uber.org/atomic"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)

// WrapHTTPTransport wraps http.Transport, add an auth header to support GCP native auth
type WrapHTTPTransport struct {
tokenSrc oauth2.TokenSource
backend transport
tokenSrc oauth2.TokenSource
backend transport
currentToken atomic.Pointer[oauth2.Token]
}

// transport abstracts http.Transport to simplify test
Expand All @@ -30,19 +32,21 @@ func NewWrapHTTPTransport(secure bool) (*WrapHTTPTransport, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to create default transport")
}
return &WrapHTTPTransport{
tokenSrc: tokenSrc,
backend: backend,
}, nil
return &WrapHTTPTransport{tokenSrc: tokenSrc, backend: backend}, nil
}

// RoundTrip implements http.RoundTripper
// RoundTrip wraps original http.RoundTripper by Adding a Bearer token acquired from tokenSrc
func (t *WrapHTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) {
token, err := t.tokenSrc.Token()
if err != nil {
return nil, errors.Wrap(err, "failed to acquire token")
// here Valid() means the token won't be expired in 10 sec
// so the http client timeout shouldn't be longer, or we need to change the default `expiryDelta` time
if !t.currentToken.Load().Valid() {
newToken, err := t.tokenSrc.Token()
if err != nil {
return nil, errors.Wrap(err, "failed to acquire token")
}
t.currentToken.Store(newToken)
}
req.Header.Set("Authorization", "Bearer "+token.AccessToken)
req.Header.Set("Authorization", "Bearer "+t.currentToken.Load().AccessToken)
return t.backend.RoundTrip(req)
}

Expand Down
89 changes: 70 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module github.com/zilliztech/milvus-backup

go 1.16
go 1.18

require (
github.com/blang/semver/v4 v4.0.0
github.com/gin-gonic/gin v1.8.1
github.com/golang/protobuf v1.5.2
github.com/google/btree v1.0.1
github.com/google/uuid v1.1.2
github.com/lingdor/stackerror v0.0.0-20191119040541-976d8885ed76
github.com/milvus-io/milvus-sdk-go/v2 v2.2.0
github.com/minio/minio-go/v7 v7.0.17
github.com/pkg/errors v0.9.1
Expand All @@ -15,32 +17,81 @@ require (
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.8.1
github.com/swaggo/files v1.0.0
github.com/swaggo/gin-swagger v1.5.3
github.com/swaggo/swag v1.8.10
github.com/uber/jaeger-client-go v2.25.0+incompatible
go.etcd.io/etcd/client/v3 v3.5.0
go.uber.org/atomic v1.10.0
go.uber.org/zap v1.17.0
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
google.golang.org/grpc v1.48.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

require (
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/go-openapi/spec v0.20.7 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-playground/validator/v10 v10.11.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/google/uuid v1.3.0
github.com/lingdor/stackerror v0.0.0-20191119040541-976d8885ed76
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.3
github.com/swaggo/swag v1.8.8
golang.org/x/crypto v0.3.0 // indirect
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602
golang.org/x/sync v0.1.0
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
golang.org/x/tools v0.3.0 // indirect
cloud.google.com/go v0.81.0 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.5 // indirect
github.com/klauspost/cpuid v1.3.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/milvus-io/milvus-proto/go-api v0.0.0-20230105121931-9f9303dcc729 // indirect
github.com/minio/md5-simd v1.1.0 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/xid v1.2.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
go.etcd.io/etcd/api/v3 v3.5.0 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220503193339-ba3ae3f07e29 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/milvus-io/milvus-sdk-go/v2 => github.com/wayblink/milvus-sdk-go/v2 v2.2.16
Loading

0 comments on commit 92a8dcb

Please sign in to comment.