Skip to content

Commit a731625

Browse files
committed
Review comments #1.
1 parent 7a81015 commit a731625

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

credentials/tls/certprovider/distributor.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import (
3131
//
3232
// Distributor implements the KeyMaterialReader interface. Provider
3333
// implementations may choose to do the following:
34-
// - return a distributor as part of their KeyMaterialReader() method.
35-
// - invoke the distributor's Set() method whenever they have new key material.
36-
// - watch the channel returned by the distributor's Done() method to get
37-
// notified when the distributor is closed.
34+
// - return a Distributor as part of their KeyMaterialReader() method.
35+
// - invoke the Distributor's Set() method whenever they have new key material.
36+
// - watch the channel returned by the Distributor.Done() method to get
37+
// notified when the Distributor is closed.
3838
type Distributor struct {
3939
// mu protects the underlying key material.
4040
mu sync.Mutex
@@ -45,7 +45,7 @@ type Distributor struct {
4545
// availability of key material.
4646
ready *grpcsync.Event
4747
// done channel to notify provider implementations and unblock any
48-
// KeyMaterial() calls, once the distributor is closed.
48+
// KeyMaterial() calls, once the Distributor is closed.
4949
closed *grpcsync.Event
5050
}
5151

@@ -57,15 +57,15 @@ func NewDistributor() *Distributor {
5757
}
5858
}
5959

60-
// Set updates the key material in the distributor with km.
60+
// Set updates the key material in the Distributor with km.
6161
//
62-
// Provider implementations which use the distributor must not modify the
62+
// Provider implementations which use the Distributor must not modify the
6363
// contents of the KeyMaterial struct pointed to by km.
6464
//
6565
// A non-nil err value indicates the error that the provider implementation ran
6666
// into when trying to fetch key material, and makes it possible to surface the
67-
// error to the user. A non-nil error value passed here causes distributor's
68-
// KeyMaterial() method to return nil key material.
67+
// error to the user. A non-nil error value passed here causes the
68+
// Distributor.KeyMaterial() method to return nil key material.
6969
func (d *Distributor) Set(km *KeyMaterial, err error) {
7070
d.mu.Lock()
7171
d.km = km
@@ -78,7 +78,7 @@ func (d *Distributor) Set(km *KeyMaterial, err error) {
7878
d.mu.Unlock()
7979
}
8080

81-
// KeyMaterial returns the most recent key material provided to the distributor.
81+
// KeyMaterial returns the most recent key material provided to the Distributor.
8282
// If no key material was provided at the time of this call, it will block until
8383
// the deadline on the context expires or fresh key material arrives.
8484
func (d *Distributor) KeyMaterial(ctx context.Context) (*KeyMaterial, error) {
@@ -106,13 +106,13 @@ func (d *Distributor) keyMaterial() (*KeyMaterial, error) {
106106
return d.km, d.pErr
107107
}
108108

109-
// Close turns down the distributor, releases allocated resources and fails any
109+
// Close turns down the Distributor, releases allocated resources and fails any
110110
// active KeyMaterial() call waiting for new key material.
111111
func (d *Distributor) Close() {
112112
d.closed.Fire()
113113
}
114114

115-
// Done returns a channel which will be closed when the distributor is closed.
115+
// Done returns a channel which will be closed when the Distributor is closed.
116116
func (d *Distributor) Done() <-chan struct{} {
117117
return d.closed.Done()
118118
}

credentials/tls/certprovider/provider.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type Provider interface {
9090
// KeyMaterialReader returns a reader to read key material sourced by the
9191
// provider. Users should call the Close() method on the returned reader
9292
// when they are no longer interested in the underlying key material.
93-
KeyMaterialReader(opts KeyMaterialOptions) (KeyMaterialReader, error)
93+
KeyMaterialReader(KeyMaterialOptions) (KeyMaterialReader, error)
9494

9595
// Close cleans up resources allocated by the provider.
9696
Close()
@@ -100,8 +100,7 @@ type Provider interface {
100100
// Implementations must be thread-safe.
101101
type KeyMaterialReader interface {
102102
// Returns the key material when they are ready.
103-
// Implementations must honor any deadline set in the context.
104-
KeyMaterial(ctx context.Context) (*KeyMaterial, error)
103+
KeyMaterial(context.Context) (*KeyMaterial, error)
105104

106105
// Close provides a way for callers to indicate that they are no longer
107106
// interested in the underlying key material.

credentials/tls/certprovider/store.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type providerKey struct {
4141
config string
4242
}
4343

44-
// distributorKey acts as the key to map of distributors maintained by the
44+
// distributorKey acts as the key to a map of distributors maintained by the
4545
// store. These distributor instances are returned by the provider
4646
// implementation's GetKeyMaterial() method.
4747
type distributorKey struct {
@@ -83,12 +83,26 @@ func GetKeyMaterialReader(name string, config interface{}, opts KeyMaterialOptio
8383
if err != nil {
8484
return nil, err
8585
}
86+
87+
// Look for an existing distributor for the (name+config+opts) combo.
88+
dk := distributorKey{
89+
name: name,
90+
config: string(stableConfig.Canonical()),
91+
options: opts,
92+
}
93+
if wd, ok := provStore.distributors[dk]; ok {
94+
wd.refCount++
95+
return wd, nil
96+
}
97+
98+
// We didn't find an existing distributor. So we need to look up the
99+
// provider for the (name+config) combo, and if don't find one, we need to
100+
// create a new one. Once we have the provider, we need to create a
101+
// distributor on it.
86102
pk := providerKey{
87103
name: name,
88104
config: string(stableConfig.Canonical()),
89105
}
90-
91-
// Create a new provider for the (name+config) combo or use an existing one.
92106
var wp *wrappedProvider
93107
wp, ok := provStore.providers[pk]
94108
if !ok {
@@ -103,31 +117,20 @@ func GetKeyMaterialReader(name string, config interface{}, opts KeyMaterialOptio
103117
}
104118
provStore.providers[pk] = wp
105119
}
106-
107-
// Create a new distributor for the (name+config+opts) combo or use an
108-
// existing one. The reference count for the provider is incremented only
109-
// when creating a new distributor.
110-
dk := distributorKey{
111-
name: name,
112-
config: string(stableConfig.Canonical()),
113-
options: opts,
114-
}
115-
if wd, ok := provStore.distributors[dk]; ok {
116-
wd.refCount++
117-
return wd, nil
118-
}
119-
distributor, err := wp.prov.KeyMaterialReader(opts)
120+
dist, err := wp.prov.KeyMaterialReader(opts)
120121
if err != nil {
121122
return nil, err
122123
}
123124
wd := &wrappedDistributor{
124-
KeyMaterialReader: distributor,
125+
KeyMaterialReader: dist,
125126
refCount: 1,
126127
wp: wp,
127128
storeKey: dk,
128129
store: provStore,
129130
}
130131
provStore.distributors[dk] = wd
132+
// The reference count for the wrappedProvider is incremented only when
133+
// creating a new distributor.
131134
wp.refCount++
132135
return wd, nil
133136
}

0 commit comments

Comments
 (0)