-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance ParseDuration with configurable parsing options
Introduce ParseDuration() as the primary and simplest entrypoint for parsing durations
- Loading branch information
Showing
9 changed files
with
1,324 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ | |
|
||
# Go workspace file | ||
go.work | ||
/haproxytimeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
DATE := $(shell date --iso-8601=seconds) | ||
GOVERSION := $(shell go version) | ||
COMMIT := $(shell git describe --tags --abbrev=8 --dirty --always --long) | ||
|
||
PREFIX := main | ||
LDFLAGS := -X '$(PREFIX).buildCommit=$(COMMIT)' -X '$(PREFIX).buildDate=$(DATE)' -X '$(PREFIX).buildGoVersion=$(GOVERSION)' | ||
|
||
build: test lint | ||
go build -ldflags "$(LDFLAGS)" -o ./haproxytimeout ./cmd/haproxytimeout | ||
|
||
test: | ||
go test ./... | ||
|
||
lint: | ||
golangci-lint run ./... | ||
|
||
benchmark: | ||
go test -bench=. -benchmem -count=5 -benchtime=1s | ||
|
||
clean: | ||
$(RM) ./haproxytimeout ./haproxytimeout.test | ||
|
||
.PHONY: build test clean benchmark lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,19 @@ | ||
# parse time durations, with support for days | ||
# Parse time durations, with support for days | ||
|
||
This is a Go library that parses duration strings in a format similar | ||
to time.ParseDuration, with the additional capability of handling | ||
duration strings specifying a number of days ("d"). This functionality | ||
is not available in the built-in time.ParseDuration function. It also | ||
differs by not accepting negative values. This package was primarily | ||
created for validating HAProxy timeout values. | ||
A Go library to parse time duration values similar to | ||
time.ParseDuration, but with extended functionality. In addition to | ||
the standard duration units like "h", "m", and "s", it also supports | ||
days (represented as "d"), which are unavailable in the built-in | ||
time.ParseDuration function. All durations parsed by this package are | ||
ensured to be non-negative and won't overflow the time.Duration value | ||
type. Furthermore, a parsed duration value cannot exceed HAProxy's | ||
maximum timeout. | ||
|
||
The CLI utility `haproxy-timeout-checker` is an example of using the | ||
package. It validates the time duration using `ParseDuration` and also | ||
checks to see if the duration exceeds HAProxy's maximum. | ||
The command line utility `haproxytimeout` is an example of using the | ||
package but also serves to convert human-readable duration values to | ||
microseconds, suitable for a HAProxy configuration file. | ||
|
||
```console | ||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "9223372036s" | ||
duration 9223372036000ms exceeds HAProxy's maximum value of 2147483647ms | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "2147483647ms" | ||
2147483647 | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "2147483648ms" | ||
duration 2147483648ms exceeds HAProxy's maximum value of 2147483647ms | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "1d" | ||
86400000 | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "1d 1s" | ||
86401000 | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "1d 3h 10m 20s 100ms 9999us" | ||
97820109 | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go 5000 | ||
5000 | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "5000 999999ms" | ||
5000 999999ms | ||
^ | ||
error: invalid unit order | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "1d 1f" | ||
1d 1f | ||
^ | ||
error: invalid unit | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "1d 1d" | ||
1d 1d | ||
^ | ||
error: invalid unit order | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "1d 5m 1230ms" | ||
86701230 | ||
|
||
# Note: Spaces are optional. | ||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "1d5m" | ||
86700000 | ||
|
||
$ go run cmd/haproxy-timeout-checker/haproxy-timeout-checker.go "9223372037s" | ||
9223372037s | ||
^ | ||
error: underflow | ||
$ make | ||
$ ./haproxytimeout -help | ||
``` |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.