Skip to content

Commit

Permalink
Add --timeout, avoid HTTP/2, closes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
bbkane committed Apr 5, 2022
1 parent ceafa99 commit e244b13
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
18 changes: 13 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
module go.bbkane.com/grabbit/v4

go 1.16
go 1.18

// Useful for local development
// replace go.bbkane.com/warg => /Users/bbkane/Git/warg

require (
github.com/bbkane/glib v0.1.1
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/vartanbeno/go-reddit/v2 v2.0.1
go.bbkane.com/logos v0.3.0
go.bbkane.com/warg v0.0.13
go.uber.org/zap v1.20.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
go.bbkane.com/gocolor v0.0.4 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.20.0
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
gopkg.in/yaml.v2 v2.4.0 // indirect
)
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ go.bbkane.com/gocolor v0.0.4 h1:UI4ejgjHdC6oupH8src8+FQgbbfcZ6C7BSPZzecBjzs=
go.bbkane.com/gocolor v0.0.4/go.mod h1:7AEOm8kyPlpj9qr/SL9mEXSceh8xM2w+eBm4deMRNHk=
go.bbkane.com/logos v0.3.0 h1:NJZAoUp0dYW5Kl5A2Zxw+uGKGwiG/t7CgxGaM167+28=
go.bbkane.com/logos v0.3.0/go.mod h1:IqUA0K5saG+eDGkY9LQOfJYO1nJOF3nnq8AWeHuViIU=
go.bbkane.com/warg v0.0.12 h1:+Jwf70ey/dlPCDmEREJSd045p1eGTX7r482bT2v3Cwc=
go.bbkane.com/warg v0.0.12/go.mod h1:kfv+iD8YFvnpqv9bUR2zp1PYTHHJWesIikcRcuJTgIc=
go.bbkane.com/warg v0.0.13 h1:BlnLM/gvELYqMpgAlAZCHLDJXKq1/uXKx1tLa+NtqXA=
go.bbkane.com/warg v0.0.13/go.mod h1:kfv+iD8YFvnpqv9bUR2zp1PYTHHJWesIikcRcuJTgIc=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down Expand Up @@ -277,7 +275,6 @@ golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -289,7 +286,6 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
38 changes: 37 additions & 1 deletion grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ func grabSubreddit(ctx context.Context, logger *logos.Logger, client *reddit.Cli

func grab(passedFlags flag.PassedFlags) error {

timeout := passedFlags["--timeout"].(time.Duration)

// retrieve types:
lumberJackLogger := &lumberjack.Logger{
Filename: passedFlags["--log-filename"].(string),
Expand Down Expand Up @@ -304,7 +306,41 @@ func grab(passedFlags flag.PassedFlags) error {
}

ua := runtime.GOOS + ":" + "grabbit" + ":" + getVersion() + " (go.bbkane.com/grabbit)"
client, err := reddit.NewReadonlyClient(reddit.WithUserAgent(ua))

dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}

// The reddit API does not like HTTP/2
// Per https://pkg.go.dev/net/http?utm_source=gopls#pkg-overview ,
// I'm copying http.DefaultTransport and replacing the HTTP/2 stuff
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,

// change from default
ForceAttemptHTTP2: false,

MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,

// use an empty map instead of nil per the link above
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),

ExpectContinueTimeout: 1 * time.Second,
}

httpClient := &http.Client{
Timeout: timeout,
Transport: transport,
}

client, err := reddit.NewReadonlyClient(
reddit.WithUserAgent(ua),
reddit.WithHTTPClient(httpClient),
)
if err != nil {
err = errors.WithStack(err)
logger.Errorw(
Expand Down
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func app() *warg.App {
grab,
command.Flag(
"--subreddit-name",
"subreddit to grab",
"Subreddit to grab",
value.StringSlice,
flag.Alias("-sn"),
flag.Default("earthporn", "wallpapers"),
Expand Down Expand Up @@ -60,13 +60,21 @@ func app() *warg.App {
),
command.Flag(
"--subreddit-limit",
"max number of links to try to download",
"Max number of links to try to download",
value.IntSlice,
flag.Alias("-sl"),
flag.Default("2", "3"),
flag.ConfigPath("subreddits[].limit"),
flag.Required(),
),
command.Flag(
"--timeout",
"Timeout for a single download",
value.Duration,
flag.Alias("-t"),
flag.Default("30s"),
flag.Required(),
),
)

app := warg.New(
Expand All @@ -90,31 +98,31 @@ func app() *warg.App {
),
section.Flag(
"--log-filename",
"log filename",
"Log filename",
value.Path,
flag.Default("~/.config/grabbit.jsonl"),
flag.ConfigPath("lumberjacklogger.filename"),
flag.Required(),
),
section.Flag(
"--log-maxage",
"max age before log rotation in days",
"Max age before log rotation in days",
value.Int,
flag.Default("30"),
flag.ConfigPath("lumberjacklogger.maxage"),
flag.Required(),
),
section.Flag(
"--log-maxbackups",
"num backups for the log",
"Num backups for the log",
value.Int,
flag.Default("0"),
flag.ConfigPath("lumberjacklogger.maxbackups"),
flag.Required(),
),
section.Flag(
"--log-maxsize",
"max size of log in megabytes",
"Max size of log in megabytes",
value.Int,
flag.Default("5"),
flag.ConfigPath("lumberjacklogger.maxsize"),
Expand All @@ -129,7 +137,7 @@ func app() *warg.App {
editConfig,
command.Flag(
"--editor",
"path to editor",
"Path to editor",
value.String,
flag.Alias("-e"),
flag.Default("vi"),
Expand All @@ -142,7 +150,7 @@ func app() *warg.App {
warg.ConfigFlag(
"--config",
yamlreader.New,
"config filepath",
"Config filepath",
flag.Alias("-c"),
flag.Default("~/.config/grabbit.yaml"),
),
Expand Down

0 comments on commit e244b13

Please sign in to comment.