-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mesh: storing bound reference pointers on a ComputedRoutes resource
- Loading branch information
Showing
15 changed files
with
1,192 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package routes | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/hashicorp/consul/internal/resource" | ||
"github.com/hashicorp/consul/proto-public/pbresource" | ||
) | ||
|
||
type sectionRefKey struct { | ||
resource.ReferenceKey | ||
Section string | ||
} | ||
|
||
type BoundReferenceCollector struct { | ||
refs map[sectionRefKey]*pbresource.Reference | ||
} | ||
|
||
func NewBoundReferenceCollector() *BoundReferenceCollector { | ||
return &BoundReferenceCollector{ | ||
refs: make(map[sectionRefKey]*pbresource.Reference), | ||
} | ||
} | ||
|
||
func (c *BoundReferenceCollector) List() []*pbresource.Reference { | ||
if len(c.refs) == 0 { | ||
return nil | ||
} | ||
|
||
out := make([]*pbresource.Reference, 0, len(c.refs)) | ||
for _, ref := range c.refs { | ||
out = append(out, ref) | ||
} | ||
|
||
sort.Slice(out, func(i, j int) bool { | ||
return resource.LessReference(out[i], out[j]) | ||
}) | ||
|
||
return out | ||
} | ||
|
||
func (c *BoundReferenceCollector) AddRefOrID(ref resource.ReferenceOrID) { | ||
if c == nil { | ||
return | ||
} | ||
c.AddRef(resource.ReferenceFromReferenceOrID(ref)) | ||
} | ||
|
||
func (c *BoundReferenceCollector) AddRef(ref *pbresource.Reference) { | ||
if c == nil { | ||
return | ||
} | ||
srk := sectionRefKey{ | ||
ReferenceKey: resource.NewReferenceKey(ref), | ||
Section: ref.Section, | ||
} | ||
|
||
if _, ok := c.refs[srk]; ok { | ||
return | ||
} | ||
|
||
c.refs[srk] = ref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.