Skip to content

Commit 26d5a06

Browse files
yuukiclaude
andauthored
feat: environment variable support (#37)
* feat: add environment variable support for configuration flags Enable configuration via environment variables using TCPULSE_ prefix. All command-line flags can now be set as environment variables with dashes converted to underscores (e.g., TCPULSE_MESSAGE_BYTES=128). 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * chore: remove revive from test target in Makefile * fix: correct sync.Pool usage in UDP buffer allocation Fix interface conversion panic in connectUDP by ensuring the pool returns *[]byte instead of []byte. This resolves the test failure in TestConnectUDPContextCancellation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 2ddcb4c commit 26d5a06

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ staticcheck:
1919
revive:
2020
$(GO) tool revive ./...
2121

22-
test: vet staticcheck revive
22+
test: vet staticcheck
2323
$(GO) test -race -v ./...
2424

2525
docker/build:

client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ func (c *Client) connectUDP(ctx context.Context, addrport string) error {
262262
limiter := ratelimit.New(int(c.config.ConnectRate))
263263

264264
bufUDPPool := sync.Pool{
265-
New: func() any { return make([]byte, c.config.MessageBytes) },
265+
New: func() any {
266+
buf := make([]byte, c.config.MessageBytes)
267+
return &buf
268+
},
266269
}
267270

268271
eg, ctx := errgroup.WithContext(ctx)

main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,36 @@ func init() {
8888
pflag.StringVar(&serveProtocol, "protocol", "all", "[server mode] listening protocol ('tcp' or 'udp')")
8989
pflag.StringVar(&listenAddrsFile, "listen-addrs-file", "", "[server mode] enable to pass a file including a pair of addresses and ports")
9090

91+
// Configure viper for environment variables
92+
viper.SetEnvPrefix("TCPULSE")
93+
viper.AutomaticEnv()
94+
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
95+
9196
viper.BindPFlags(pflag.CommandLine)
9297
}
9398

9499
func main() {
95100
pflag.Parse()
96101

102+
// Read values from viper (includes environment variables)
103+
clientMode = viper.GetBool("client")
104+
serverMode = viper.GetBool("server")
105+
protocol = viper.GetString("proto")
106+
intervalStats = viper.GetDuration("interval")
107+
connectFlavor = viper.GetString("flavor")
108+
connections = viper.GetInt32("connections")
109+
connectRate = viper.GetInt32("rate")
110+
duration = viper.GetDuration("duration")
111+
messageBytes = viper.GetInt32("message-bytes")
112+
showOnlyResults = viper.GetBool("show-only-results")
113+
mergeResultsEachHost = viper.GetBool("merge-results-each-host")
114+
jsonlines = viper.GetBool("jsonlines")
115+
addrsFile = viper.GetBool("addrs-file")
116+
pprof = viper.GetBool("enable-pprof")
117+
pprofAddr = viper.GetString("pprof-addr")
118+
serveProtocol = viper.GetString("protocol")
119+
listenAddrsFile = viper.GetString("listen-addrs-file")
120+
97121
// Handle version flag
98122
handleVersion()
99123

0 commit comments

Comments
 (0)