@@ -9,48 +9,45 @@ import (
99)
1010
1111// Scale creates or destroys units to match the desired number
12- func (c * FleetClient ) Scale (component string , requested int ) error {
13-
14- outchan := make (chan string )
15- errchan := make (chan error )
16- var wg sync.WaitGroup
12+ func (c * FleetClient ) Scale (
13+ component string , requested int , wg * sync.WaitGroup , outchan chan string , errchan chan error ) {
1714
1815 if requested < 0 {
19- return errors .New ("cannot scale below 0" )
16+ errchan <- errors .New ("cannot scale below 0" )
2017 }
2118 // check how many currently exist
2219 components , err := c .Units (component )
2320 if err != nil {
2421 // skip checking the first time; we just want a tally
2522 if ! strings .Contains (err .Error (), "could not find unit" ) {
26- return err
23+ errchan <- err
24+ return
2725 }
2826 }
2927
3028 timesToScale := int (math .Abs (float64 (requested - len (components ))))
3129 if timesToScale == 0 {
32- return nil
30+ return
3331 }
3432 if requested - len (components ) > 0 {
35- return scaleUp (c , component , len (components ), timesToScale , & wg , outchan , errchan )
33+ scaleUp (c , component , len (components ), timesToScale , wg , outchan , errchan )
34+ } else {
35+ scaleDown (c , component , len (components ), timesToScale , wg , outchan , errchan )
3636 }
37- return scaleDown (c , component , len (components ), timesToScale , & wg , outchan , errchan )
3837}
3938
4039func scaleUp (c * FleetClient , component string , numExistingContainers , numTimesToScale int ,
41- wg * sync.WaitGroup , outchan chan string , errchan chan error ) error {
40+ wg * sync.WaitGroup , outchan chan string , errchan chan error ) {
4241 for i := 0 ; i < numTimesToScale ; i ++ {
4342 target := component + "@" + strconv .Itoa (numExistingContainers + i + 1 )
4443 c .Create ([]string {target }, wg , outchan , errchan )
4544 }
46- return nil
4745}
4846
4947func scaleDown (c * FleetClient , component string , numExistingContainers , numTimesToScale int ,
50- wg * sync.WaitGroup , outchan chan string , errchan chan error ) error {
48+ wg * sync.WaitGroup , outchan chan string , errchan chan error ) {
5149 for i := 0 ; i < numTimesToScale ; i ++ {
5250 target := component + "@" + strconv .Itoa (numExistingContainers - i )
5351 c .Destroy ([]string {target }, wg , outchan , errchan )
5452 }
55- return nil
5653}
0 commit comments