Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Configurable image deadline
Browse files Browse the repository at this point in the history
- Use WithDeadline rather than WithTimeout
- Make deadline configurable

Fixes #713
  • Loading branch information
philwinder committed Aug 24, 2017
1 parent c7fd6fa commit a50f60a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
4 changes: 3 additions & 1 deletion cmd/fluxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func main() {
registryPollInterval = fs.Duration("registry-poll-interval", 5*time.Minute, "period at which to poll registry for new images")
registryRPS = fs.Int("registry-rps", 200, "maximum registry requests per second per host")
registryBurst = fs.Int("registry-burst", defaultRemoteConnections, "maximum registry request burst per host (default matched to number of http worker goroutines)")
imageTimeout = fs.Duration("registry-image-timeout", 10*time.Second, "maximum time allowed for all requests related to a single image")
registryTimeout = fs.Duration("registry-timeout", 10*time.Second, "maximum time allowed for individual remote registry requests")
// k8s-secret backed ssh keyring configuration
k8sSecretName = fs.String("k8s-secret-name", "flux-git-deploy", "Name of the k8s secret used to store the private SSH key")
k8sSecretVolumeMountPath = fs.String("k8s-secret-volume-mount-path", "/etc/fluxd/ssh", "Mount location of the k8s secret storing the private SSH key")
Expand Down Expand Up @@ -236,7 +238,7 @@ func main() {
remoteFactory := registry.NewRemoteClientFactory(registryLogger, registryMiddleware.RateLimiterConfig{
RPS: *registryRPS,
Burst: *registryBurst,
})
}, *imageTimeout, *registryTimeout)

// Warmer
warmerLogger := log.NewContext(logger).With("component", "warmer")
Expand Down
19 changes: 12 additions & 7 deletions registry/client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ type ClientFactory interface {

// ---
// A new ClientFactory for a Remote.
func NewRemoteClientFactory(l log.Logger, rlc middleware.RateLimiterConfig) ClientFactory {
func NewRemoteClientFactory(l log.Logger, rlc middleware.RateLimiterConfig, imgTimeout, rmtTimeout time.Duration) ClientFactory {
return &remoteClientFactory{
Logger: l,
rlConf: rlc,
Logger: l,
rlConf: rlc,
ImageTimeout: imgTimeout,
RemoteTimeout: rmtTimeout,
}
}

type remoteClientFactory struct {
Logger log.Logger
rlConf middleware.RateLimiterConfig
Logger log.Logger
rlConf middleware.RateLimiterConfig
ImageTimeout time.Duration // Max amount of time per image (all requests)
RemoteTimeout time.Duration // Max amount of time per HTTP request
}

func (f *remoteClientFactory) ClientFor(host string, creds Credentials) (Client, error) {
Expand All @@ -56,7 +60,8 @@ func (f *remoteClientFactory) ClientFor(host string, creds Credentials) (Client,
// A context we'll use to cancel requests on error
ctx, cancel := context.WithCancel(context.Background())
// Add a timeout to the request
ctx, cancel = context.WithTimeout(ctx, requestTimeout)
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(f.ImageTimeout))
println(time.Now().String(), time.Now().Add(f.ImageTimeout).String())

// Use the wrapper to fix headers for quay.io, and remember bearer tokens
var transport http.RoundTripper
Expand All @@ -76,7 +81,7 @@ func (f *remoteClientFactory) ClientFor(host string, creds Credentials) (Client,
Client: &http.Client{
Transport: transport,
Jar: jar,
Timeout: requestTimeout,
Timeout: f.RemoteTimeout,
},
Logf: dockerregistry.Quiet,
},
Expand Down
2 changes: 2 additions & 0 deletions registry/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func TestWarming_WarmerWriteCacheRead(t *testing.T) {
remote := NewRemoteClientFactory(
logger.With("component", "client"),
middleware.RateLimiterConfig{200, 10},
10*time.Second,
10*time.Second,
)

cache := NewCacheClientFactory(
Expand Down
4 changes: 2 additions & 2 deletions registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestRemoteFactory_RawClient(t *testing.T) {
fact := NewRemoteClientFactory(log.NewNopLogger(), middleware.RateLimiterConfig{
RPS: 200,
Burst: 1,
})
}, 10*time.Second, 10*time.Second)

// Refresh tags first
var tags []string
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestRemoteFactory_RawClient(t *testing.T) {
}

func TestRemoteFactory_InvalidHost(t *testing.T) {
fact := NewRemoteClientFactory(log.NewNopLogger(), middleware.RateLimiterConfig{})
fact := NewRemoteClientFactory(log.NewNopLogger(), middleware.RateLimiterConfig{}, time.Second, time.Second)
invalidId, err := flux.ParseImageID("invalid.host/library/alpine:latest")
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 4 additions & 5 deletions registry/warming.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package registry
import (
"context"
"encoding/json"
"net"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -141,10 +140,10 @@ func (w *Warmer) warm(id flux.ImageID, creds Credentials) {
// Get the image from the remote
img, err := client.Manifest(imageID)
if err != nil {
if err, ok := errors.Cause(err).(net.Error); ok && err.Timeout() {
// This was due to a context timeout, don't bother logging
return
}
//if err, ok := errors.Cause(err).(net.Error); ok && err.Timeout() {
// // This was due to a context timeout, don't bother logging
// return
//}
w.Logger.Log("err", errors.Wrap(err, "requesting manifests"))
return
}
Expand Down

0 comments on commit a50f60a

Please sign in to comment.