Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 59 additions & 5 deletions api/cloudcontroller/ccv3/ccv3fakes/fake_clock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/cloudcontroller/ccv3/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import "time"

type Clock interface {
Now() time.Time
Sleep(time.Duration)
}
4 changes: 4 additions & 0 deletions api/cloudcontroller/ccv3/internal/real_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ type RealTime struct{}
func (RealTime) Now() time.Time {
return time.Now()
}

func (RealTime) Sleep(d time.Duration) {
time.Sleep(d)
}
24 changes: 22 additions & 2 deletions api/cloudcontroller/ccv3/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ func (client *Client) PollJob(jobURL JobURL) (Warnings, error) {
return client.PollJobForState(jobURL, constant.JobComplete)
}

const initialJobPollingDelay = 250 * time.Millisecond

func jobPollingDelay(elapsed, interval time.Duration) time.Duration {
if interval <= 0 {
return interval
}

next := (elapsed/interval + 1) * interval
for r := initialJobPollingDelay; r < interval; r = 2*r + initialJobPollingDelay {
if r > elapsed {
next = r
break
}
}

return next - elapsed
}

func (client *Client) PollJobForState(jobURL JobURL, state constant.JobState) (Warnings, error) {
if jobURL == "" {
return nil, nil
Expand Down Expand Up @@ -137,7 +155,7 @@ func (client *Client) PollJobForState(jobURL JobURL, state constant.JobState) (W
return allWarnings, nil
}

time.Sleep(client.jobPollingInterval)
client.clock.Sleep(jobPollingDelay(client.clock.Now().Sub(startTime), client.jobPollingInterval))
}

return allWarnings, ccerror.JobTimeoutError{
Expand Down Expand Up @@ -191,7 +209,9 @@ func (client *Client) PollJobToEventStream(jobURL JobURL) chan PollJobEvent {
}

stream <- event
time.Sleep(client.jobPollingInterval)
if !end {
client.clock.Sleep(jobPollingDelay(client.clock.Now().Sub(startTime), client.jobPollingInterval))
}
}

close(stream)
Expand Down
Loading
Loading