Repository: buchgr/bazel-remote
Pull request #167 (2020-01-23 21:57:11)

add missing configuration file support for profiling options


Patch:
diff --git a/README.md b/README.md
index ecbdc8d..15533c2 100644
--- a/README.md
+++ b/README.md
@@ -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
 
diff --git a/config/config.go b/config/config.go
index 1652162..0a16631 100644
--- a/config/config.go
+++ b/config/config.go
@@ -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"`
@@ -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,
diff --git a/main.go b/main.go
index 7dcd0b4..ac20b27 100644
--- a/main.go
+++ b/main.go
@@ -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"),
@@ -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))


Test patch:
diff --git a/config/config_test.go b/config/config_test.go
index 8767704..b48f72e 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -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)
+	}
+}