mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-03-31 06:24:00 -04:00
perf: skip HTTP body reading when debug logging is disabled
The loggingRoundTripper was unconditionally reading entire HTTP request/response bodies via io.ReadAll() on every round trip, even at the default Info log level. The expensive work (body read, string conversion, concatenation) happened before Debug() was called, so the logger's internal level check couldn't prevent it. Now makeLogClient skips wrapping the transport entirely when debug logging is not enabled, eliminating all overhead in the common case. Fixes #1096 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
_ "time/tzdata"
|
||||
@@ -206,7 +207,8 @@ func _main(ctx context.Context, reader *reader.Reader, args []string, logger log
|
||||
hioClient := healthchecksio.New(client, config.Health.HealthchecksioBaseURL,
|
||||
*config.Health.HealthchecksioUUID)
|
||||
|
||||
updater := update.NewUpdater(db, client, shoutrrrClient, logger, timeNow)
|
||||
debugEnabled := strings.EqualFold(config.Logger.Level, "DEBUG")
|
||||
updater := update.NewUpdater(db, client, shoutrrrClient, logger, timeNow, debugEnabled)
|
||||
updaterService := update.NewService(db, updater, ipGetter, config.Update.Period,
|
||||
config.Update.Cooldown, logger, resolver, timeNow, hioClient)
|
||||
|
||||
|
||||
@@ -15,7 +15,11 @@ type DebugLogger interface {
|
||||
Debug(s string)
|
||||
}
|
||||
|
||||
func makeLogClient(client *http.Client, logger DebugLogger) *http.Client {
|
||||
func makeLogClient(client *http.Client, logger DebugLogger, debugEnabled bool) *http.Client {
|
||||
if !debugEnabled {
|
||||
return client
|
||||
}
|
||||
|
||||
transport := client.Transport
|
||||
if transport == nil {
|
||||
transport = http.DefaultTransport
|
||||
|
||||
@@ -102,7 +102,7 @@ func Test_LogClient(t *testing.T) {
|
||||
assert.Regexp(t, testCase.responseLineRegex, s)
|
||||
})
|
||||
|
||||
logClient := makeLogClient(client, logger)
|
||||
logClient := makeLogClient(client, logger, true)
|
||||
|
||||
assert.Same(t, logClient, client)
|
||||
|
||||
|
||||
@@ -22,9 +22,9 @@ type Updater struct {
|
||||
}
|
||||
|
||||
func NewUpdater(db Database, client *http.Client, shoutrrrClient ShoutrrrClient,
|
||||
logger DebugLogger, timeNow func() time.Time,
|
||||
logger DebugLogger, timeNow func() time.Time, debugEnabled bool,
|
||||
) *Updater {
|
||||
client = makeLogClient(client, logger)
|
||||
client = makeLogClient(client, logger, debugEnabled)
|
||||
return &Updater{
|
||||
db: db,
|
||||
client: client,
|
||||
|
||||
Reference in New Issue
Block a user