Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: introduced -dltimeout option to control timeout for download. #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 8 additions & 3 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"os"
"regexp"
"strings"
"time"

"github.com/cubicdaiya/nginx-build/command"
"github.com/cubicdaiya/nginx-build/openresty"
)

const DefaultDownloadTimeout = time.Duration(900) * time.Second

type Builder struct {
Version string
DownloadURLPrefix string
Component int
DownloadTimeout time.Duration
// for dependencies such as pcre and zlib and openssl
Static bool
}
Expand Down Expand Up @@ -148,10 +152,11 @@ func (builder *Builder) InstalledVersion() (string, error) {
return string(m[1]), nil
}

func MakeBuilder(component int, version string) Builder {
func MakeBuilder(component int, version string, timeout time.Duration) Builder {
var builder Builder
builder.Component = component
builder.Version = version
builder.DownloadTimeout = timeout
switch component {
case ComponentNginx:
builder.DownloadURLPrefix = NginxDownloadURLPrefix
Expand All @@ -173,8 +178,8 @@ func MakeBuilder(component int, version string) Builder {
return builder
}

func MakeLibraryBuilder(component int, version string, static bool) Builder {
builder := MakeBuilder(component, version)
func MakeLibraryBuilder(component int, version string, timeout time.Duration, static bool) Builder {
builder := MakeBuilder(component, version, timeout)
builder.Static = static
return builder
}
23 changes: 16 additions & 7 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package builder
import (
"fmt"
"testing"
"time"
)

func setupBuilders(t *testing.T) []Builder {
builders := make([]Builder, ComponentMax)
builders[ComponentNginx] = MakeBuilder(ComponentNginx, NginxVersion)
builders[ComponentPcre] = MakeLibraryBuilder(ComponentPcre, PcreVersion, false)
builders[ComponentOpenSSL] = MakeLibraryBuilder(ComponentOpenSSL, OpenSSLVersion, true)
builders[ComponentLibreSSL] = MakeLibraryBuilder(ComponentLibreSSL, LibreSSLVersion, true)
builders[ComponentZlib] = MakeLibraryBuilder(ComponentZlib, ZlibVersion, false)
builders[ComponentOpenResty] = MakeBuilder(ComponentOpenResty, OpenRestyVersion)
builders[ComponentTengine] = MakeBuilder(ComponentTengine, TengineVersion)
builders[ComponentNginx] = MakeBuilder(ComponentNginx, NginxVersion, DefaultDownloadTimeout)
builders[ComponentPcre] = MakeLibraryBuilder(ComponentPcre, PcreVersion, DefaultDownloadTimeout, false)
builders[ComponentOpenSSL] = MakeLibraryBuilder(ComponentOpenSSL, OpenSSLVersion, DefaultDownloadTimeout, true)
builders[ComponentLibreSSL] = MakeLibraryBuilder(ComponentLibreSSL, LibreSSLVersion, DefaultDownloadTimeout, true)
builders[ComponentZlib] = MakeLibraryBuilder(ComponentZlib, ZlibVersion, DefaultDownloadTimeout, false)
builders[ComponentOpenResty] = MakeBuilder(ComponentOpenResty, OpenRestyVersion, DefaultDownloadTimeout)
builders[ComponentTengine] = MakeBuilder(ComponentTengine, TengineVersion, DefaultDownloadTimeout)
return builders
}

Expand Down Expand Up @@ -308,26 +309,31 @@ func TestMakeStaticLibrary(t *testing.T) {
builder Builder
staticLibrary StaticLibrary
version string
timeout time.Duration
}{
{
builder: builders[ComponentPcre],
staticLibrary: MakeStaticLibrary(&builders[ComponentPcre]),
version: PcreVersion,
timeout: DefaultDownloadTimeout,
},
{
builder: builders[ComponentOpenSSL],
staticLibrary: MakeStaticLibrary(&builders[ComponentOpenSSL]),
version: OpenSSLVersion,
timeout: DefaultDownloadTimeout,
},
{
builder: builders[ComponentLibreSSL],
staticLibrary: MakeStaticLibrary(&builders[ComponentLibreSSL]),
version: LibreSSLVersion,
timeout: DefaultDownloadTimeout,
},
{
builder: builders[ComponentZlib],
staticLibrary: MakeStaticLibrary(&builders[ComponentZlib]),
version: ZlibVersion,
timeout: DefaultDownloadTimeout,
},
}

Expand All @@ -341,5 +347,8 @@ func TestMakeStaticLibrary(t *testing.T) {
if test.builder.Version != test.version {
t.Fatalf("not equal version between builder's and default's")
}
if test.builder.DownloadTimeout != test.timeout {
t.Fatalf("not equal download timeout between builder's and default's")
}
}
}
6 changes: 3 additions & 3 deletions configure/configure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

func setupBuilders(t *testing.T) []builder.Builder {
builders := make([]builder.Builder, builder.ComponentMax)
builders[builder.ComponentPcre] = builder.MakeBuilder(builder.ComponentPcre, builder.PcreVersion)
builders[builder.ComponentOpenSSL] = builder.MakeBuilder(builder.ComponentOpenSSL, builder.OpenSSLVersion)
builders[builder.ComponentZlib] = builder.MakeBuilder(builder.ComponentZlib, builder.ZlibVersion)
builders[builder.ComponentPcre] = builder.MakeBuilder(builder.ComponentPcre, builder.PcreVersion, builder.DefaultDownloadTimeout)
builders[builder.ComponentOpenSSL] = builder.MakeBuilder(builder.ComponentOpenSSL, builder.OpenSSLVersion, builder.DefaultDownloadTimeout)
builders[builder.ComponentZlib] = builder.MakeBuilder(builder.ComponentZlib, builder.ZlibVersion, builder.DefaultDownloadTimeout)
return builders
}

Expand Down
5 changes: 1 addition & 4 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ import (
"log"
"net/http"
"os"
"time"

"github.com/cubicdaiya/nginx-build/builder"
"github.com/cubicdaiya/nginx-build/command"
"github.com/cubicdaiya/nginx-build/util"
)

const DefaultDownloadTimeout = time.Duration(900) * time.Second

func extractArchive(path string) error {
return command.Run([]string{"tar", "zxvf", path})
}

func download(b *builder.Builder) error {
c := &http.Client{
Timeout: DefaultDownloadTimeout,
Timeout: b.DownloadTimeout,
}
res, err := c.Get(b.DownloadURL())
if err != nil {
Expand Down
21 changes: 14 additions & 7 deletions nginx-build.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func main() {
nginxBuildOptions.Numbers[k] = v
}

for k, v := range nginxBuildOptions.Times {
v.Value = flag.Duration(k, v.Default, v.Desc)
nginxBuildOptions.Times[k] = v
}

overrideUnableParseFlags()

var (
Expand Down Expand Up @@ -135,6 +140,8 @@ func main() {
tengineVersion := nginxBuildOptions.Values["tengineversion"].Value
patchOption := nginxBuildOptions.Values["patch-opt"].Value

dltimeout := nginxBuildOptions.Times["dltimeout"].Value

// Allow multiple flags for `--patch`
{
tmp := nginxBuildOptions.Values["patch"]
Expand Down Expand Up @@ -191,16 +198,16 @@ func main() {
log.Fatal("select one between '-openssl' and '-libressl'.")
}
if *openResty {
nginxBuilder = builder.MakeBuilder(builder.ComponentOpenResty, *openRestyVersion)
nginxBuilder = builder.MakeBuilder(builder.ComponentOpenResty, *openRestyVersion, *dltimeout)
} else if *tengine {
nginxBuilder = builder.MakeBuilder(builder.ComponentTengine, *tengineVersion)
nginxBuilder = builder.MakeBuilder(builder.ComponentTengine, *tengineVersion, *dltimeout)
} else {
nginxBuilder = builder.MakeBuilder(builder.ComponentNginx, *version)
nginxBuilder = builder.MakeBuilder(builder.ComponentNginx, *version, *dltimeout)
}
pcreBuilder := builder.MakeLibraryBuilder(builder.ComponentPcre, *pcreVersion, *pcreStatic)
openSSLBuilder := builder.MakeLibraryBuilder(builder.ComponentOpenSSL, *openSSLVersion, *openSSLStatic)
libreSSLBuilder := builder.MakeLibraryBuilder(builder.ComponentLibreSSL, *libreSSLVersion, *libreSSLStatic)
zlibBuilder := builder.MakeLibraryBuilder(builder.ComponentZlib, *zlibVersion, *zlibStatic)
pcreBuilder := builder.MakeLibraryBuilder(builder.ComponentPcre, *pcreVersion, *dltimeout, *pcreStatic)
openSSLBuilder := builder.MakeLibraryBuilder(builder.ComponentOpenSSL, *openSSLVersion, *dltimeout, *openSSLStatic)
libreSSLBuilder := builder.MakeLibraryBuilder(builder.ComponentLibreSSL, *libreSSLVersion, *dltimeout, *libreSSLStatic)
zlibBuilder := builder.MakeLibraryBuilder(builder.ComponentZlib, *zlibVersion, *dltimeout, *zlibStatic)

if *idempotent {
builders := []builder.Builder{
Expand Down
21 changes: 21 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"runtime"
"strconv"
"time"

"github.com/cubicdaiya/nginx-build/builder"
)
Expand All @@ -11,6 +12,7 @@ type Options struct {
Values map[string]OptionValue
Bools map[string]OptionBool
Numbers map[string]OptionNumber
Times map[string]OptionTime
}

type OptionValue struct {
Expand All @@ -32,11 +34,18 @@ type OptionNumber struct {
Default int
}

type OptionTime struct {
Desc string
Value *time.Duration
Default time.Duration
}

func makeNginxBuildOptions() Options {
var nginxBuildOptions Options
argsNumber := make(map[string]OptionNumber)
argsBool := make(map[string]OptionBool)
argsString := make(map[string]OptionValue)
argsTime := make(map[string]OptionTime)

argsNumber["j"] = OptionNumber{
Desc: "jobs to build nginx",
Expand Down Expand Up @@ -132,9 +141,15 @@ func makeNginxBuildOptions() Options {
Default: "",
}

argsTime["dltimeout"] = OptionTime{
Desc: "timeout for download",
Default: builder.DefaultDownloadTimeout,
}

nginxBuildOptions.Bools = argsBool
nginxBuildOptions.Values = argsString
nginxBuildOptions.Numbers = argsNumber
nginxBuildOptions.Times = argsTime

return nginxBuildOptions
}
Expand All @@ -149,6 +164,9 @@ func isNginxBuildOption(k string) bool {
if _, ok := nginxBuildOptions.Numbers[k]; ok {
return true
}
if _, ok := nginxBuildOptions.Times[k]; ok {
return true
}
return false
}

Expand All @@ -159,5 +177,8 @@ func defaultStringValue(k string) string {
if _, ok := nginxBuildOptions.Numbers[k]; ok {
return strconv.Itoa(nginxBuildOptions.Numbers[k].Default)
}
if _, ok := nginxBuildOptions.Times[k]; ok {
return nginxBuildOptions.Times[k].Default.String()
}
return ""
}