Skip to content

Commit

Permalink
Allows cluster filtering in account subject mapping (#4175)
Browse files Browse the repository at this point in the history
- [X] Branch rebased on top of current main (`git pull --rebase origin
main`)
- [X] Changes squashed to a single commit (described
[here](http://gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html))
 - [ ] Build is green in Travis CI
- [X] You have certified that the contribution is your original work and
that you license the work to the project under the [Apache 2
license](https://github.com/nats-io/nats-server/blob/main/LICENSE)

### Changes proposed in this pull request:

Allows (exposes) the existing cluster filtering option of account level
subject mapping, thereby allowing you to define different mapping
destination per cluster (e.g. to insert the cluster name as a subject
token).

Example:
```
mappings {
	"foo":[
		{destination:"foo-east", cluster: "east", weight:100},
		{destination:"foo-west", cluster: "west", weight:100},
	]
}
```
  • Loading branch information
derekcollison committed May 18, 2023
2 parents fbfa593 + 2b843ba commit 75e8df1
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions server/accounts.go
Expand Up @@ -597,7 +597,6 @@ func (a *Account) AddMapping(src, dest string) error {
}

// AddWeightedMappings will add in a weighted mappings for the destinations.
// TODO(dlc) - Allow cluster filtering
func (a *Account) AddWeightedMappings(src string, dests ...*MapDest) error {
a.mu.Lock()
defer a.mu.Unlock()
Expand All @@ -614,7 +613,7 @@ func (a *Account) AddWeightedMappings(src string, dests ...*MapDest) error {
m := &mapping{src: src, wc: subjectHasWildcard(src), dests: make([]*destination, 0, len(dests)+1)}
seen := make(map[string]struct{})

var tw uint8
var tw = make(map[string]uint8)
for _, d := range dests {
if _, ok := seen[d.Subject]; ok {
return fmt.Errorf("duplicate entry for %q", d.Subject)
Expand All @@ -623,8 +622,8 @@ func (a *Account) AddWeightedMappings(src string, dests ...*MapDest) error {
if d.Weight > 100 {
return fmt.Errorf("individual weights need to be <= 100")
}
tw += d.Weight
if tw > 100 {
tw[d.Cluster] += d.Weight
if tw[d.Cluster] > 100 {
return fmt.Errorf("total weight needs to be <= 100")
}
err := ValidateMappingDestination(d.Subject)
Expand Down

0 comments on commit 75e8df1

Please sign in to comment.