Skip to content

Commit

Permalink
Merge pull request #86 from jwilder/jw-fixes
Browse files Browse the repository at this point in the history
HTTP wait fixes
  • Loading branch information
jwilder authored Apr 4, 2017
2 parents aa855b4 + 0a48b87 commit fe57903
Showing 1 changed file with 36 additions and 18 deletions.
54 changes: 36 additions & 18 deletions dockerize.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"golang.org/x/net/context"
)

const defaultWaitRetryInterval = time.Second

type sliceVar []string
type hostFlagsVar []string

Expand All @@ -41,18 +43,19 @@ var (
poll bool
wg sync.WaitGroup

templatesFlag sliceVar
templateDirsFlag sliceVar
stdoutTailFlag sliceVar
stderrTailFlag sliceVar
headersFlag sliceVar
delimsFlag string
delims []string
headers []HttpHeader
urls []url.URL
waitFlag hostFlagsVar
waitTimeoutFlag time.Duration
dependencyChan chan struct{}
templatesFlag sliceVar
templateDirsFlag sliceVar
stdoutTailFlag sliceVar
stderrTailFlag sliceVar
headersFlag sliceVar
delimsFlag string
delims []string
headers []HttpHeader
urls []url.URL
waitFlag hostFlagsVar
waitRetryInterval time.Duration
waitTimeoutFlag time.Duration
dependencyChan chan struct{}

ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -81,7 +84,7 @@ func waitForDependencies() {

go func() {
for _, u := range urls {
log.Println("Waiting for host:", u.Host)
log.Println("Waiting for:", u.String())

switch u.Scheme {
case "tcp", "tcp4", "tcp6":
Expand All @@ -91,19 +94,33 @@ func waitForDependencies() {
case "http", "https":
wg.Add(1)
go func(u url.URL) {
client := &http.Client{
Timeout: waitTimeoutFlag,
}

defer wg.Done()
for {
client := &http.Client{}
req, _ := http.NewRequest("GET", u.String(), nil)
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
log.Printf("Problem with dial: %v. Sleeping %s\n", err.Error(), waitRetryInterval)
time.Sleep(waitRetryInterval)
}
if len(headers) > 0 {
for _, header := range headers {
req.Header.Add(header.name, header.value)
}
}

resp, err := client.Do(req)
if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
if err != nil {
log.Printf("Problem with request: %s. Sleeping %s\n", err.Error(), waitRetryInterval)
time.Sleep(waitRetryInterval)
} else if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
log.Printf("Received %d from %s\n", resp.StatusCode, u.String())
return
} else {
log.Printf("Received %d from %s. Sleeping %s\n", resp.StatusCode, u.String(), waitRetryInterval)
time.Sleep(waitRetryInterval)
}
}
}(u)
Expand Down Expand Up @@ -131,8 +148,8 @@ func waitForSocket(scheme, addr string, timeout time.Duration) {
for {
conn, err := net.DialTimeout(scheme, addr, waitTimeoutFlag)
if err != nil {
log.Printf("Problem with dial: %v. Sleeping 5s\n", err.Error())
time.Sleep(5 * time.Second)
log.Printf("Problem with dial: %v. Sleeping %s\n", err.Error(), waitRetryInterval)
time.Sleep(waitRetryInterval)
}
if conn != nil {
log.Printf("Connected to %s://%s\n", scheme, addr)
Expand Down Expand Up @@ -180,6 +197,7 @@ func main() {
flag.Var(&headersFlag, "wait-http-header", "HTTP headers, colon separated. e.g \"Accept-Encoding: gzip\". Can be passed multiple times")
flag.Var(&waitFlag, "wait", "Host (tcp/tcp4/tcp6/http/https/unix) to wait for before this container starts. Can be passed multiple times. e.g. tcp://db:5432")
flag.DurationVar(&waitTimeoutFlag, "timeout", 10*time.Second, "Host wait timeout")
flag.DurationVar(&waitRetryInterval, "wait-retry-interval", defaultWaitRetryInterval, "Duration to wait before retrying")

flag.Usage = usage
flag.Parse()
Expand Down

0 comments on commit fe57903

Please sign in to comment.