Skip to content

Commit

Permalink
add missing configuration file support for profiling options
Browse files Browse the repository at this point in the history
  • Loading branch information
mostynb committed Jan 23, 2020
1 parent dbe9aa6 commit a32c60a
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ host: localhost
# The port to use for (experimental) gRPC support:
#grpc_port: 9092
# If profile_port is specified, then serve /debug/pprof/* URLs here:
#profile_host: 127.0.0.1
#profile_port: 7070
# If you want to require simple authentication:
#htpasswd_file: path/to/.htpasswd
Expand Down
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type Config struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
GRPCPort int `yaml:"grpc_port"`
ProfileHost string `yaml:"profile_host"`
ProfilePort int `yaml:"profile_port"`
Dir string `yaml:"dir"`
MaxSize int `yaml:"max_size"`
HtpasswdFile string `yaml:"htpasswd_file"`
Expand All @@ -47,13 +49,16 @@ type Config struct {
}

// New ...
func New(dir string, maxSize int, host string, port int, grpc_port int, htpasswdFile string,
func New(dir string, maxSize int, host string, port int, grpc_port int,
profile_host string, profile_port int, htpasswdFile string,
tlsCertFile string, tlsKeyFile string, idleTimeout time.Duration,
s3 *S3CloudStorageConfig, disable_http_ac_validation bool) (*Config, error) {
c := Config{
Host: host,
Port: port,
GRPCPort: grpc_port,
ProfileHost: profile_host,
ProfilePort: profile_port,
Dir: dir,
MaxSize: maxSize,
HtpasswdFile: htpasswdFile,
Expand Down
40 changes: 40 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,43 @@ s3_proxy:
t.Fatalf("Expected '%+v' but got '%+v'", expectedConfig, config)
}
}

func TestValidProfiling(t *testing.T) {
yaml := `host: localhost
port: 1234
dir: /opt/cache-dir
max_size: 42
profile_port: 7070
`
config, err := newFromYaml([]byte(yaml))
if err != nil {
t.Fatal(err)
}

expectedConfig := &Config{
Host: "localhost",
Port: 1234,
Dir: "/opt/cache-dir",
MaxSize: 42,
ProfilePort: 7070,
ProfileHost: "",
}

if !cmp.Equal(config, expectedConfig) {
t.Fatalf("Expected '%+v' but got '%+v'", expectedConfig, config)
}

yaml += `
profile_host: 192.168.1.1`

expectedConfig.ProfileHost = "192.168.1.1"

config, err = newFromYaml([]byte(yaml))
if err != nil {
t.Fatal(err)
}

if !cmp.Equal(config, expectedConfig) {
t.Fatalf("Expected '%+v' but got '%+v'", expectedConfig, config)
}
}
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ func main() {
ctx.String("host"),
ctx.Int("port"),
ctx.Int("grpc_port"),
ctx.String("profile_host"),
ctx.Int("profile_port"),
ctx.String("htpasswd_file"),
ctx.String("tls_cert_file"),
ctx.String("tls_key_file"),
Expand Down Expand Up @@ -290,11 +292,11 @@ func main() {
}()
}

if ctx.Int("profile_port") > 0 {
if c.ProfilePort > 0 {
go func() {
// Allow access to /debug/pprof/ URLs.
profileAddr := ctx.String("profile_host") + ":" +
strconv.Itoa(ctx.Int("profile_port"))
profileAddr := c.ProfileHost + ":" +
strconv.Itoa(c.ProfilePort)
log.Printf("Starting HTTP server for profiling on address %s",
profileAddr)
log.Fatal(http.ListenAndServe(profileAddr, nil))
Expand Down

0 comments on commit a32c60a

Please sign in to comment.