Skip to content

Commit

Permalink
pkg/identity: protect LabelsSHA256 against concurrent initializations
Browse files Browse the repository at this point in the history
LabelsSHA256 can be protected with a sync.Once and it makes sure this
field is only initialized once.

Fixes: a77ba96 ("create identity package")
Signed-off-by: André Martins <andre@cilium.io>
  • Loading branch information
aanm committed Jun 3, 2020
1 parent 78d99a5 commit 3958c70
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/identity/identity.go
Expand Up @@ -17,6 +17,7 @@ package identity
import (
"fmt"
"net"
"sync"

"github.com/cilium/cilium/pkg/labels"
)
Expand All @@ -28,6 +29,9 @@ type Identity struct {
ID NumericIdentity `json:"id"`
// Set of labels that belong to this Identity.
Labels labels.Labels `json:"labels"`

// onceLabelSHA256 makes sure LabelsSHA256 is only set once
onceLabelSHA256 sync.Once
// SHA256 of labels.
LabelsSHA256 string `json:"labelsSHA256"`

Expand Down Expand Up @@ -92,9 +96,11 @@ func (id *Identity) Sanitize() {
// GetLabelsSHA256 returns the SHA256 of the labels associated with the
// identity. The SHA is calculated if not already cached.
func (id *Identity) GetLabelsSHA256() string {
if id.LabelsSHA256 == "" {
id.LabelsSHA256 = id.Labels.SHA256Sum()
}
id.onceLabelSHA256.Do(func() {
if id.LabelsSHA256 == "" {
id.LabelsSHA256 = id.Labels.SHA256Sum()
}
})

return id.LabelsSHA256
}
Expand Down

0 comments on commit 3958c70

Please sign in to comment.