Skip to content

Commit

Permalink
Merge pull request #1035 from appare45/feat-disable-keep-alive
Browse files Browse the repository at this point in the history
Add disable_http_keep_alive option
  • Loading branch information
rmatsuoka authored Dec 26, 2024
2 parents ce8ba21 + 78589bd commit 6e910c9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 33 deletions.
6 changes: 3 additions & 3 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,8 @@ func buildUA(ver, rev string) string {
}

// NewMackerelClient returns Mackerel API client for mackerel-agent
func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool) (*mackerel.API, error) {
api, err := mackerel.NewAPI(apibase, apikey, verbose)
func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableHTTPKeepAlive bool) (*mackerel.API, error) {
api, err := mackerel.NewAPI(apibase, apikey, verbose, disableHTTPKeepAlive)
if err != nil {
return nil, err
}
Expand All @@ -708,7 +708,7 @@ func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool) (*mackere
// Prepare sets up API and registers the host data to the Mackerel server.
// Use returned values to call Run().
func Prepare(conf *config.Config, ameta *AgentMeta) (*App, error) {
api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose)
api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.DisableHTTPKeepAlive)
if err != nil {
return nil, fmt.Errorf("failed to prepare an api: %s", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion command/command_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAPIRequestHeader(t *testing.T) {
}))
defer ts.Close()

api, err := NewMackerelClient(ts.URL, apiKey, ver, rev, false)
api, err := NewMackerelClient(ts.URL, apiKey, ver, rev, false, false)
if err != nil {
t.Errorf("something went wrong while creating new mackerel client: %+v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func TestLoop(t *testing.T) {
},
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true)
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -442,7 +442,7 @@ func TestLoop_NetworkError(t *testing.T) {
},
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true)
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -524,7 +524,7 @@ func TestLoop_NetworkErrorWithRecovery(t *testing.T) {
},
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true)
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -585,7 +585,7 @@ func TestReportCheckMonitors(t *testing.T) {
return tc.Status, jsonObject{}
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true)
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -643,7 +643,7 @@ func TestReportCheckMonitors_NetworkError(t *testing.T) {
return http.StatusSeeOther, jsonObject{}
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true)
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -708,7 +708,7 @@ func TestReportCheckMonitors_NetworkErrorWithRecovery(t *testing.T) {
return http.StatusOK, jsonObject{}
}

api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true)
api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func doRetire(fs *flag.FlagSet, argv []string) error {
return fmt.Errorf("hostID file is not found or empty")
}

api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose)
api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.DisableHTTPKeepAlive)
if err != nil {
return fmt.Errorf("faild to create api client: %s", err)
}
Expand Down
36 changes: 18 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,24 @@ func (c *CloudPlatform) UnmarshalText(text []byte) error {

// Config represents mackerel-agent's configuration file.
type Config struct {
Apibase string
Apikey string
Root string
Pidfile string
Conffile string
Roles []string
Verbose bool
Silent bool
Diagnostic bool `toml:"diagnostic"`
DisplayName string `toml:"display_name"`
HostStatus HostStatus `toml:"host_status" conf:"parent"`
Disks Disks `toml:"disks" conf:"parent"`
Filesystems Filesystems `toml:"filesystems" conf:"parent"`
Interfaces Interfaces `toml:"interfaces" conf:"parent"`
HTTPProxy string `toml:"http_proxy"`
HTTPSProxy string `toml:"https_proxy"`
CloudPlatform CloudPlatform `toml:"cloud_platform"`
Apibase string
Apikey string
Root string
Pidfile string
Conffile string
Roles []string
Verbose bool
Silent bool
Diagnostic bool `toml:"diagnostic"`
DisableHTTPKeepAlive bool `toml:"disable_http_keep_alive"`
DisplayName string `toml:"display_name"`
HostStatus HostStatus `toml:"host_status" conf:"parent"`
Disks Disks `toml:"disks" conf:"parent"`
Filesystems Filesystems `toml:"filesystems" conf:"parent"`
Interfaces Interfaces `toml:"interfaces" conf:"parent"`
HTTPProxy string `toml:"http_proxy"`
HTTPSProxy string `toml:"https_proxy"`
CloudPlatform CloudPlatform `toml:"cloud_platform"`

// This Plugin field is used to decode the toml file. After reading the
// configuration from file, this field is set to nil.
Expand Down Expand Up @@ -447,7 +448,6 @@ func LoadConfig(conffile string) (*Config, error) {
if !config.Diagnostic {
config.Diagnostic = DefaultConfig.Diagnostic
}

return config, err
}

Expand Down
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ func TestLoadConfig(t *testing.T) {
if config.Filesystems.UseMountpoint != false {
t.Error("should be false (default value should be used)")
}

if config.DisableHTTPKeepAlive != false {
t.Error("should be false (default value should be used)")
}
}

var sampleConfigWithHostStatus = `
Expand Down
7 changes: 6 additions & 1 deletion mackerel/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mackerel
import (
"errors"
"fmt"
"net/http"
"net/url"

"github.com/mackerelio/golib/logging"
Expand Down Expand Up @@ -56,12 +57,16 @@ func infoError(msg string) *InfoError {
}

// NewAPI creates a new instance of API.
func NewAPI(rawurl string, apiKey string, verbose bool) (*API, error) {
func NewAPI(rawurl string, apiKey string, verbose bool, disableHTTPKeepAlive bool) (*API, error) {
c, err := mkr.NewClientWithOptions(apiKey, rawurl, verbose)
if err != nil {
return nil, err
}
c.PrioritizedLogger = logger
t := http.DefaultTransport.(*http.Transport).Clone()
t.DisableKeepAlives = disableHTTPKeepAlive
c.HTTPClient.Transport = t

return &API{Client: c}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion mackerel/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestNewAPI(t *testing.T) {
"http://example.com",
"dummy-key",
true,
false,
)

if err != nil {
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestFindHostByCustomIdentifier(t *testing.T) {
}))
defer ts.Close()

api, _ := NewAPI(ts.URL, "dummy-key", false)
api, _ := NewAPI(ts.URL, "dummy-key", false, false)

var tests = []struct {
customIdentifier string
Expand Down
4 changes: 2 additions & 2 deletions mackerel/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestReportCheckMonitors(t *testing.T) {
}))
defer ts.Close()

api, _ := NewAPI(ts.URL, "dummy-key", false)
api, _ := NewAPI(ts.URL, "dummy-key", false, false)

err := api.ReportCheckMonitors("9rxGOHfVF8F", []*checks.Report{
{
Expand Down Expand Up @@ -153,7 +153,7 @@ func TestReportCheckMonitorsCompat(t *testing.T) {
maxAttemts: 0,
},
}
api, _ := NewAPI(ts.URL, "dummy-key", false)
api, _ := NewAPI(ts.URL, "dummy-key", false, false)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
api.ReportCheckMonitors("xxx", []*checks.Report{
Expand Down

0 comments on commit 6e910c9

Please sign in to comment.