-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persistent Worker: introduce resource modifiers (#764)
- Loading branch information
Showing
13 changed files
with
389 additions
and
24 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
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
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
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
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,60 @@ | ||
package resourcemodifier | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type Modifier struct { | ||
Match map[string]float64 `yaml:"match"` | ||
Append Append `yaml:"append"` | ||
|
||
sync.Mutex | ||
} | ||
|
||
type Append struct { | ||
Run []string `yaml:"run"` | ||
} | ||
|
||
func (modifier *Modifier) Matches(requested map[string]float64) bool { | ||
for matchKey, matchValue := range modifier.Match { | ||
requestedValue, ok := requested[matchKey] | ||
if !ok { | ||
return false | ||
} | ||
|
||
if requestedValue > matchValue { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
type Manager struct { | ||
resourceModifiers []*Modifier | ||
|
||
mtx sync.Mutex | ||
} | ||
|
||
func NewManager(resourceModifiers ...*Modifier) *Manager { | ||
return &Manager{ | ||
resourceModifiers: resourceModifiers, | ||
} | ||
} | ||
|
||
func (manager *Manager) Acquire(resources map[string]float64) *Modifier { | ||
manager.mtx.Lock() | ||
defer manager.mtx.Unlock() | ||
|
||
for _, resourceModifier := range manager.resourceModifiers { | ||
if !resourceModifier.Matches(resources) { | ||
continue | ||
} | ||
|
||
if resourceModifier.TryLock() { | ||
return resourceModifier | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.