Skip to content

Commit

Permalink
Add pkg/loadbalancing/dnslb.Block() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
rgooch committed Apr 6, 2021
1 parent a74f5d9 commit 4e44852
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/loadbalancing/dnslb/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ func New(config Config, params Params) (*LoadBalancer, error) {
return newLoadBalancer(config, params)
}

// Block will block a server instance with the specified IP address from
// adding itself to DNS for the specified time or until a message is received on
// cancelChannel.
func Block(config Config, params Params, ip string, duration time.Duration,
cancelChannel <-chan struct{}, logger log.DebugLogger) error {
return block(config, params, ip, duration, cancelChannel, logger)
}

// RollingReplace will use the provided configuration and will roll through all
// server instances in the specified region triggering replacements by removing
// each server from DNS, destroying it and waiting for (some other mechanism) to
Expand Down
45 changes: 45 additions & 0 deletions pkg/loadbalancing/dnslb/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dnslb

import (
crand "crypto/rand"
"encoding/hex"
"fmt"
"time"

"github.com/Cloud-Foundations/golib/pkg/log"
)

func block(config Config, params Params, ip string, duration time.Duration,
cancelChannel <-chan struct{}, logger log.DebugLogger) error {
if duration < time.Minute {
return fmt.Errorf("duration: %s is under a minute", duration)
} else if duration > time.Hour {
return fmt.Errorf("duration: %s is over an hour", duration)
}
lb := &LoadBalancer{
config: config,
p: params,
}
crandData := make([]byte, 4)
if _, err := crand.Read(crandData); err != nil {
return err
}
myId := hex.EncodeToString(crandData)
stopTime := time.Now().Add(duration)
for keepGoing := true; keepGoing; {
if time.Until(stopTime) <= 0 {
break
}
if err := lb.block(myId, ip, time.Minute); err != nil {
return err
}
timer := time.NewTimer(time.Minute)
select {
case <-cancelChannel:
keepGoing = false
timer.Stop()
case <-timer.C:
}
}
return lb.cleanupBlock()
}

0 comments on commit 4e44852

Please sign in to comment.