Skip to content

Commit

Permalink
KTOR-7374 Replace timeout for the whole request with connect and resp…
Browse files Browse the repository at this point in the history
…onse timeouts when downloading jdk (#24)
  • Loading branch information
Stexxe authored Sep 3, 2024
1 parent 820c8a1 commit f10f3ae
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 10 deletions.
10 changes: 9 additions & 1 deletion cmd/ktor/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
_ "embed"
"fmt"
"github.com/ktorio/ktor-cli/internal/app/cli"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/ktorio/ktor-cli/internal/app/utils"
"io"
"log"
"net"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -58,7 +60,13 @@ func main() {
cli.WriteUsage(os.Stdout)
case cli.NewCommand:
client := &http.Client{
Timeout: 30 * time.Second,
Transport: &http.Transport{
DialContext: func(_ context.Context, network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, 5*time.Second)
},
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
},
}

projectName := utils.CleanProjectName(args.CommandArgs[0])
Expand Down
2 changes: 2 additions & 0 deletions internal/app/cli/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func HandleAppError(projectDir string, err error) (reportLog bool) {
fmt.Fprintf(os.Stderr, "Unable to download JDK %s for %s %s\n", je.Descriptor.Version, je.Descriptor.Platform, je.Arch)
case app.JdkServerError:
fmt.Fprintf(os.Stderr, "Unexpected error occurred while connecting to a JDK server. Please try again later.\n")
case app.JdkServerDownloadError:
fmt.Fprintf(os.Stderr, "An error occurred while downloading from a JDK server. Please try again later.\n")
case app.JdkVerificationFailed:
fmt.Fprintln(os.Stderr, "Checksum verification for the downloaded JDK failed")
case app.GradlewChmodError:
Expand Down
1 change: 1 addition & 0 deletions internal/app/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
ExtractRootDirExistError
UnableLocateJdkError
JdkServerError
JdkServerDownloadError
JdkVerificationFailed
)

Expand Down
2 changes: 1 addition & 1 deletion internal/app/generate/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Project(client *http.Client, logger *log.Logger, projectDir, project string
len(zipBytes),
logger.Writer() == io.Discard,
)
defer progressBar.Finish()
defer progressBar.Done()

_, err = archive.ExtractZip(reader, int64(len(zipBytes)), projectDir, logger)

Expand Down
11 changes: 9 additions & 2 deletions internal/app/jdk/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ func DownloadJdk(client *http.Client, d *Descriptor, logger *log.Logger) ([]byte
}

reader, progressBar := progress.NewReader(resp.Body, "Downloading JDK... ", utils.ContentLength(resp), true)
defer progressBar.Finish()
return io.ReadAll(reader)
b, err := io.ReadAll(reader)

if err != nil {
progressBar.Stop()
return nil, &app.Error{Err: err, Kind: app.JdkServerDownloadError}
}

progressBar.Done()
return b, nil
}

func hasJdkBuild(d *Descriptor) bool {
Expand Down
4 changes: 2 additions & 2 deletions internal/app/jdk/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func fetch(client *http.Client, d *Descriptor, outDir string, logger *log.Logger
len(jdkBytes),
logger.Writer() == io.Discard,
)
defer progressBar.Finish()
defer progressBar.Done()

extractedDirs, extractErr = archive.ExtractZip(reader, int64(len(jdkBytes)), outDir, logger)
} else {
Expand All @@ -55,7 +55,7 @@ func fetch(client *http.Client, d *Descriptor, outDir string, logger *log.Logger
len(jdkBytes),
logger.Writer() == io.Discard,
)
defer progressBar.Finish()
defer progressBar.Done()

extractedDirs, extractErr = archive.ExtractTarGz(reader, outDir, logger)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/network/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewProject(client *http.Client, payload ProjectPayload) ([]byte, error) {
utils.ContentLength(resp),
true,
)
defer progressBar.Finish()
defer progressBar.Done()

bodyBytes, err := io.ReadAll(reader)
if err != nil {
Expand Down
11 changes: 10 additions & 1 deletion internal/app/progress/percent.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (b *Percent) tick(p []byte) (n int, err error) {
return len(p), nil
}

func (b *Percent) Finish() (err error) {
func (b *Percent) Done() (err error) {
if !b.enabled {
return nil
}
Expand All @@ -77,3 +77,12 @@ func (b *Percent) Finish() (err error) {
fmt.Fprintf(b.Writer, "%s100%%\n", b.prefix)
return
}

func (b *Percent) Stop() (err error) {
if !b.enabled {
return nil
}

fmt.Fprintln(b.Writer)
return
}
4 changes: 2 additions & 2 deletions internal/app/progress/percent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func checkAllWrites(t *testing.T, p *Percent, b *strings.Builder, cases []testCa
for _, test := range cases {
var err error
if test.writeLen == -1 {
err = p.Finish()
err = p.Done()
} else {
_, err = p.Write(make([]byte, test.writeLen))
}
Expand All @@ -93,7 +93,7 @@ func checkAllWrites(t *testing.T, p *Percent, b *strings.Builder, cases []testCa
for _, test := range cases {
var err error
if test.writeLen == -1 {
err = p.Finish()
err = p.Done()
} else {
_, err = p.WriteAt(make([]byte, test.writeLen), test.offset)
}
Expand Down

0 comments on commit f10f3ae

Please sign in to comment.