fix double init error

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-01-26 10:41:20 +03:00
parent d5e7969521
commit b1a3401b9b
3 changed files with 33 additions and 12 deletions

2
go.sum
View File

@ -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/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 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= 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/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 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 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/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.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.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.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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

10
s3.go
View File

@ -25,6 +25,7 @@ type s3Store struct {
opts store.Options opts store.Options
endpoint string endpoint string
mopts *minio.Options mopts *minio.Options
connected bool
} }
func getBucket(ctx context.Context) string { func getBucket(ctx context.Context) string {
@ -43,14 +44,17 @@ func NewStore(opts ...store.Option) store.Store {
} }
func (s *s3Store) Connect(ctx context.Context) error { func (s *s3Store) Connect(ctx context.Context) error {
if s.client == nil { if s.connected {
return nil
}
client, err := minio.New(s.endpoint, s.mopts) client, err := minio.New(s.endpoint, s.mopts)
if err != nil { if err != nil {
return fmt.Errorf("Error connecting to store: %w", err) return fmt.Errorf("Error connecting to store: %w", err)
} }
s.client = client s.client = client
} s.connected = true
return nil return nil
} }
@ -65,7 +69,7 @@ func (s *s3Store) Init(opts ...store.Option) error {
} }
var akey, skey string var akey, skey string
var endpoint string endpoint := s.endpoint
if s.opts.Context != nil { if s.opts.Context != nil {
if v, ok := s.opts.Context.Value(accessKey{}).(string); ok && v != "" { if v, ok := s.opts.Context.Value(accessKey{}).(string); ok && v != "" {

View File

@ -5,6 +5,8 @@ import (
"context" "context"
"os" "os"
"testing" "testing"
"github.com/unistack-org/micro/v3"
) )
func TestStore(t *testing.T) { func TestStore(t *testing.T) {
@ -24,16 +26,29 @@ func TestStore(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if err := s.Init(); err != nil {
t.Fatalf("double init test failed: %v", err)
}
if err := s.Connect(ctx); err != nil { if err := s.Connect(ctx); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := s.Connect(ctx); err != nil {
t.Fatalf("double connect test failed: %v", err)
}
defer func() { defer func() {
if err := s.Disconnect(ctx); err != nil { if err := s.Disconnect(ctx); err != nil {
t.Fatal(err) 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") val := []byte("test")
key := "key" key := "key"