diff options
Diffstat (limited to 'utilities')
| -rw-r--r-- | utilities/transport.go | 46 | ||||
| -rw-r--r-- | utilities/utilities.go | 12 |
2 files changed, 55 insertions, 3 deletions
diff --git a/utilities/transport.go b/utilities/transport.go new file mode 100644 index 0000000..2d70989 --- /dev/null +++ b/utilities/transport.go @@ -0,0 +1,46 @@ +/* + * This file is part of Go Responsiveness. + * + * Go Responsiveness is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software Foundation, + * either version 2 of the License, or (at your option) any later version. + * Go Responsiveness is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with Go Responsiveness. If not, see <https://www.gnu.org/licenses/>. + */ + +package utilities + +import ( + "context" + "net" + "net/http" + "time" + + "golang.org/x/net/http2" +) + +func OverrideHostTransport(transport *http.Transport, connectToAddr string) { + dialer := &net.Dialer{ + Timeout: 10 * time.Second, + } + + transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { + _, port, err := net.SplitHostPort(addr) + if err != nil { + return nil, err + } + + if len(connectToAddr) > 0 { + addr = net.JoinHostPort(connectToAddr, port) + } + + return dialer.DialContext(ctx, network, addr) + } + + http2.ConfigureTransport(transport) + +} diff --git a/utilities/utilities.go b/utilities/utilities.go index 57b4a90..377be56 100644 --- a/utilities/utilities.go +++ b/utilities/utilities.go @@ -28,6 +28,11 @@ import ( "golang.org/x/exp/constraints" ) +var ( + // GitVersion is the Git revision hash + GitVersion = "dev" +) + func Iota(low int, high int) (made []int) { made = make([]int, high-low) @@ -46,9 +51,6 @@ func SignedPercentDifference[T constraints.Float | constraints.Integer]( current T, previous T, ) (difference float64) { - //return ((current - previous) / (float64(current+previous) / 2.0)) * float64( - //100, - // ) fCurrent := float64(current) fPrevious := float64(previous) return ((fCurrent - fPrevious) / fPrevious) * 100.0 @@ -203,3 +205,7 @@ func ApproximatelyEqual[T float32 | float64](truth T, maybe T, fudge T) bool { diff := math.Abs((bTruth - bMaybe)) return diff < bFudge } + +func UserAgent() string { + return fmt.Sprintf("goresponsiveness/%s", GitVersion) +} |
