Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disable_http_keep_alive option #1035

Merged
merged 8 commits into from
Dec 26, 2024

Conversation

appare45
Copy link
Collaborator

@appare45 appare45 commented Nov 29, 2024

  • Added disable_http_keep_alive option
  • The default value for disable_http_keep_alive is false
  • When disable_http_keep_alive is true, mkr requests mackerel without KeepAlive

@appare45 appare45 force-pushed the feat-disable-keep-alive branch from 495b925 to 5c8c0d9 Compare December 11, 2024 05:49
config/config.go Outdated
Verbose bool
Silent bool
Diagnostic bool `toml:"diagnostic"`
DisableKeepAlive bool `toml:"disable_keep_alive"`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added DisableKeepAlive here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to DisableHttpKeepAlive

DisableHttpKeepAlive bool `toml:"disable_http_keep_alive"`

mackerel/api.go Outdated
Comment on lines 66 to 69
t := http.DefaultTransport.(*http.Transport).Clone()
print(disableKeepAlive)
t.DisableKeepAlives = disableKeepAlive
c.HTTPClient.Transport = t
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since http.DefaultTransport is made from http.Transport, we can cast it here.
https://pkg.go.dev/net/http#:~:text=DefaultTransport%20is%20the,lowercase%20versions%20thereof

@appare45 appare45 marked this pull request as ready for review December 11, 2024 05:57
@appare45 appare45 changed the title Add keep_alive option Add disable_keep_alive option Dec 11, 2024
mackerel/api.go Outdated
c, err := mkr.NewClientWithOptions(apiKey, rawurl, verbose)
if err != nil {
return nil, err
}
c.PrioritizedLogger = logger
t := http.DefaultTransport.(*http.Transport).Clone()
print(disableKeepAlive)
Copy link
Member

@rmatsuoka rmatsuoka Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to delete your print debugging.

@rmatsuoka
Copy link
Member

rmatsuoka commented Dec 11, 2024

Could you marge (or rebase) master branch onto your appare45:feat-disable-keep-alive? CI failure may be fixed.

@appare45 appare45 force-pushed the feat-disable-keep-alive branch from d0b2804 to c127806 Compare December 13, 2024 02:12
@appare45
Copy link
Collaborator Author

Could you marge (or rebase) master branch onto your appare45:feat-disable-keep-alive? CI failure may be fixed.

@rmatsuoka Thank you, I've rebased it and passed the CI

Copy link
Member

@rmatsuoka rmatsuoka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good! I added a note, but you can make changes or not.

Copy link
Member

@rmatsuoka rmatsuoka Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[may]
In all the tests that call NewMackerelClient, you set the disableKeepAlive parameter to true. However, in the previous version, this parameter was implicitly set to false.
While I believe this change does not introduce any issues, it is preferable not to introduce such changes unnecessarily.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it 7022f7c

@rmatsuoka
Copy link
Member

rmatsuoka commented Dec 18, 2024

Have you run the modified mackerel-agent and confirmed whether it sends metrics to Mackerel when disableKeepAlive is set to both true and false? If not, please test it.

@appare45 appare45 changed the title Add disable_keep_alive option Add disable_http_keep_alive option Dec 20, 2024
@appare45
Copy link
Collaborator Author

Have you run the modified mackerel-agent and confirmed whether it sends metrics to Mackerel when disableKeepAlive is set to both true and false? If not, please test it.

I've captured the packets between mackerel api and mackerel agent with mitmproxy.

Without disable_http_keep_alive (Default behaviour)

Since there is no connection header, HTTP/1.1 (mackerel api) reuses the connections 1.
スクリーンショット 2024-12-20 11 40 16

With disable_http_keep_alive

Since Connection header is set to close, the connection won't be reused.
image

Footnotes

  1. https://developer.mozilla.org/en-US/docs/Web/HTTP/Connection_management_in_HTTP_1.x#short-lived_connections:~:text=This%20model%20is%20the%20default%20model%20used%20in%20HTTP/1.0%20(if%20there%20is%20no%20Connection%20header%2C%20or%20if%20its%20value%20is%20set%20to%20close).%20In%20HTTP/1.1%2C%20this%20model%20is%20only%20used%20when%20the%20Connection%20header%20is%20sent%20with%20a%20value%20of%20close.

@appare45 appare45 force-pushed the feat-disable-keep-alive branch from 12e1b6e to 05dcf6b Compare December 20, 2024 02:44
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Go conventions, all letters in acronyms should be uppercase. Therefore, it is preferable to use disableHTTPKeepAlive across all your changes.

(ref. https://go.dev/wiki/CodeReviewComments#initialisms)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it 4474546

config/config.go Outdated
Comment on lines 451 to 453
if !config.DisableHttpKeepAlive {
config.DisableHttpKeepAlive = DefaultConfig.DisableHttpKeepAlive
}
Copy link
Member

@rmatsuoka rmatsuoka Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line is redundant: if config.DisableHTTPKeepAlive is already false, there is no need to set it to false (from DefaultConfig.DisableHTTPKeepAlive) again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it 78589bd

Copy link
Member

@rmatsuoka rmatsuoka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@rmatsuoka rmatsuoka merged commit 6e910c9 into mackerelio:master Dec 26, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants