diff --git a/go.sum b/go.sum index 5ae7e15..23736ca 100644 --- a/go.sum +++ b/go.sum @@ -37,6 +37,7 @@ github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I= github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -167,6 +168,7 @@ gopkg.in/ini.v1 v1.57.0 h1:9unxIsFcTt4I55uWluz+UmL95q4kdJ0buvQ1ZIqVQww= gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/s3.go b/s3.go index eb648d7..bc7030f 100644 --- a/s3.go +++ b/s3.go @@ -21,10 +21,11 @@ import ( var keyRegex = regexp.MustCompile("[^a-zA-Z0-9]+") type s3Store struct { - client *minio.Client - opts store.Options - endpoint string - mopts *minio.Options + client *minio.Client + opts store.Options + endpoint string + mopts *minio.Options + connected bool } func getBucket(ctx context.Context) string { @@ -43,15 +44,18 @@ func NewStore(opts ...store.Option) store.Store { } func (s *s3Store) Connect(ctx context.Context) error { - if s.client == nil { - client, err := minio.New(s.endpoint, s.mopts) - if err != nil { - return fmt.Errorf("Error connecting to store: %w", err) - } - - s.client = client + if s.connected { + return nil } + client, err := minio.New(s.endpoint, s.mopts) + if err != nil { + return fmt.Errorf("Error connecting to store: %w", err) + } + + s.client = client + s.connected = true + return nil } @@ -65,7 +69,7 @@ func (s *s3Store) Init(opts ...store.Option) error { } var akey, skey string - var endpoint string + endpoint := s.endpoint if s.opts.Context != nil { if v, ok := s.opts.Context.Value(accessKey{}).(string); ok && v != "" { diff --git a/s3_test.go b/s3_test.go index 1a42901..d639102 100644 --- a/s3_test.go +++ b/s3_test.go @@ -5,6 +5,8 @@ import ( "context" "os" "testing" + + "github.com/unistack-org/micro/v3" ) func TestStore(t *testing.T) { @@ -24,16 +26,29 @@ func TestStore(t *testing.T) { t.Fatal(err) } + if err := s.Init(); err != nil { + t.Fatalf("double init test failed: %v", err) + } + if err := s.Connect(ctx); err != nil { t.Fatal(err) } + if err := s.Connect(ctx); err != nil { + t.Fatalf("double connect test failed: %v", err) + } + defer func() { if err := s.Disconnect(ctx); err != nil { t.Fatal(err) } }() + svc := micro.NewService(micro.Store(s)) + if err := svc.Init(); err != nil { + t.Fatalf("service init failed: %v", err) + } + val := []byte("test") key := "key"