diff --git a/go/vt/grpcclient/client_auth_static.go b/go/vt/grpcclient/client_auth_static.go index 4c0b8c7bdad..f3783527fef 100644 --- a/go/vt/grpcclient/client_auth_static.go +++ b/go/vt/grpcclient/client_auth_static.go @@ -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 @@ -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