Skip to content

Commit

Permalink
move sync.once to global
Browse files Browse the repository at this point in the history
  • Loading branch information
tanjinx committed Jan 22, 2024
1 parent 96b3512 commit 95c480c
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions go/vt/grpcclient/client_auth_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ var (
// StaticAuthClientCreds implements client interface to be able to WithPerRPCCredentials
_ credentials.PerRPCCredentials = (*StaticAuthClientCreds)(nil)

credsData = ([]byte)(nil)
clientCreds = (*StaticAuthClientCreds)(nil)

once sync.Once
)

// StaticAuthClientCreds holder for client credentials
Expand Down Expand Up @@ -63,22 +65,27 @@ func AppendStaticAuth(opts []grpc.DialOption) ([]grpc.DialOption, error) {
return opts, nil
}

var err error
if credsData == nil {
var once sync.Once
if clientCreds == nil {
var err error
once.Do(func() {
var credsData []byte
credsData, err = os.ReadFile(*credsFile)
if err != nil {
return
}

clientCreds = &StaticAuthClientCreds{}
err = json.Unmarshal(credsData, clientCreds)
if err != nil {
return
}
})

if err != nil {
return nil, err
}
}

clientCreds := &StaticAuthClientCreds{}
err = json.Unmarshal(credsData, clientCreds)
if err != nil {
return nil, err
}
creds := grpc.WithPerRPCCredentials(clientCreds)
opts = append(opts, creds)
return opts, nil
Expand Down

0 comments on commit 95c480c

Please sign in to comment.