Skip to content

Commit

Permalink
Add flagx.Advanced variable and first advanced flag -httpx.tcp-network (
Browse files Browse the repository at this point in the history
#123)

* Add flagx.Advanced flag set
* Add httpx advanced flag to select tcp network
  • Loading branch information
stephen-soltesz authored Jun 19, 2020
1 parent 3abc23e commit fac1167
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
20 changes: 20 additions & 0 deletions flagx/advanced.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package flagx

import "flag"

var (
// Advanced is a *flag.FlagSet for advanced flags. Packages should add flags
// to Advanced when those flags should NOT be included in the default
// flag.CommandLine flag set. Advanced flags may be enabled by calling
// EnableAdvancedFlags _before_ calling flag.Parse().
Advanced = flag.NewFlagSet("advanced", flag.ExitOnError)
)

// EnableAdvancedFlags adds all flags registered with the Advanced flag set to
// the default flag.CommandLine flag set. EnableAdvancedFlags should be called
// before flag.Parse().
func EnableAdvancedFlags() {
Advanced.VisitAll(func(f *flag.Flag) {
flag.Var(f.Value, f.Name, f.Usage)
})
}
47 changes: 47 additions & 0 deletions flagx/advanced_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package flagx

import (
"flag"
"testing"

"github.com/go-test/deep"
)

func TestEnableAdvancedFlags(t *testing.T) {
t.Run("example", func(t *testing.T) {
// Add advanced flags.
e := Enum{
Options: []string{"a", "b"},
Value: "b",
}
s := "advanced"
Advanced.Var(&e, "advanced-enum-flag", "advanced-usage")
Advanced.StringVar(&s, "advanced-string-flag", "", "advanced-usage")

// Add default flags.
// Reset the default command line flag to simplilfy checking tests.
flag.CommandLine = flag.NewFlagSet("default", flag.ContinueOnError)
s2 := "default"
flag.StringVar(&s2, "default-flag", "", "default-usage")

// Add advanced flags to default set.
EnableAdvancedFlags()

found := map[string]bool{}
expected := map[string]bool{
"advanced-enum-flag": true,
"advanced-string-flag": true,
"default-flag": true,
}
// Verify that they are present in the default set now.
flag.CommandLine.VisitAll(
func(f *flag.Flag) {
found[f.Name] = true
t.Logf("Found: %q", f.Name)
},
)
if diff := deep.Equal(found, expected); diff != nil {
t.Errorf("EnableAdvancedFlags() found the wrong advanced flags: %#v", diff)
}
})
}
18 changes: 16 additions & 2 deletions httpx/httpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,24 @@ import (
"net/http"
"strings"
"time"

"github.com/m-lab/go/flagx"
)

var logFatalf = log.Fatalf

// tcpNetwork defines the TCP network used by ListenAndServeAsync and
// ListenAndServeTLSAsync HTTP(S) servers. See https://golang.org/pkg/net/#Dial
var tcpNetwork = flagx.Enum{
Options: []string{"tcp", "tcp4", "tcp6"},
Value: "tcp",
}

func init() {
// Add as an advanced flag.
flagx.Advanced.Var(&tcpNetwork, "httpx.tcp-network", "Controls the TCP stack used by httpx net.Listeners")
}

// The code here is adapted from https://golang.org/src/net/http/server.go?s=85391:85432#L2742

// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
Expand Down Expand Up @@ -56,7 +70,7 @@ func serve(server *http.Server, listener net.Listener) {
// contain the address and port which this server is listening on.
func ListenAndServeAsync(server *http.Server) error {
// Start listening synchronously.
listener, err := net.Listen("tcp", server.Addr)
listener, err := net.Listen(tcpNetwork.Value, server.Addr)
if err != nil {
return err
}
Expand Down Expand Up @@ -87,7 +101,7 @@ func serveTLS(server *http.Server, listener net.Listener, certFile, keyFile stri
// fatal error if the server dies for a reason besides ErrServerClosed.
func ListenAndServeTLSAsync(server *http.Server, certFile, keyFile string) error {
// Start listening synchronously.
listener, err := net.Listen("tcp", server.Addr)
listener, err := net.Listen(tcpNetwork.Value, server.Addr)
if err != nil {
return err
}
Expand Down

0 comments on commit fac1167

Please sign in to comment.