Skip to content

Modernize go packages #135

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ workflows:
jobs:
build-and-test:
docker:
- image: cimg/go:1.21.5
- image: cimg/go:1.24.2
steps:
- checkout
- run: go vet ./...
- run: go test ./...
govulncheck:
docker:
- image: cimg/go:1.21.5
- image: cimg/go:1.24.2
steps:
- checkout
- run: go install golang.org/x/vuln/cmd/govulncheck@latest
- run: govulncheck ./...
- run: $(go env GOPATH)/bin/govulncheck ./...
37 changes: 37 additions & 0 deletions MODERNIZATION_SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Go Codebase Modernization Summary

## Overview
This document summarizes the modernization changes made to the remind101/pkg repository to bring it up to date with current Go best practices, particularly around HTTP client code and context handling.

## Key Changes

### HTTP Transport Modernization
- Replaced deprecated `http.Transport.Dial` with `DialContext` for context-aware connection establishment
- Added modern connection parameters:
- `MaxIdleConns`: Controls the maximum number of idle connections across all hosts
- `MaxIdleConnsPerHost`: Controls the maximum idle connections per host
- `IdleConnTimeout`: Sets timeout for idle connections
- `TLSHandshakeTimeout`: Sets timeout for TLS handshake

### Context-Aware HTTP Requests
- Replaced `http.NewRequest` with `http.NewRequestWithContext` throughout the codebase
- Added explicit context handling with `req.WithContext(ctx)` where needed
- Ensured proper context propagation for better timeout and cancellation handling

### Updated Files
1. **httpx/http_service.go**: Updated transport configuration with modern parameters
2. **client/client.go**: Changed to use context-aware request creation
3. **example/main.go**: Updated example code to demonstrate proper context usage
4. **service_client/service_client.go**: Added explicit context handling
5. **reporter/hb2/internal/honeybadger-go/server.go**: Updated to use context-aware requests
6. **reporter/hb2/hb2_test.go** and **reporter/reporter_test.go**: Updated test files
7. Various other files throughout the codebase to ensure consistent modernization

## Benefits
- Improved resource management with better connection pooling
- Better timeout and cancellation handling through context propagation
- More reliable network operations with proper context awareness
- Code that follows current Go best practices and idioms

## Testing
All tests have been verified to pass with the modernized code, ensuring backward compatibility while improving the codebase.
13 changes: 6 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Recommended Usage:
//
// type MyClient struct {
// Client *client.Client
// Client *client.Client
// }
//
// type ThingInput struct {
Expand All @@ -13,10 +13,10 @@
// }
//
// func(c MyClient) Thing(input *ThingInput) (*ThingOutput, err) {
// var output ThingOutput
// req := c.Client.NewRequest("GET", "/thing", input, output)
// err := req.Send()
// return output, err
// var output ThingOutput
// req := c.Client.NewRequest("GET", "/thing", input, output)
// err := req.Send()
// return output, err
// }
package client

Expand Down Expand Up @@ -109,8 +109,7 @@ func New(info metadata.ClientInfo, options ...ClientOpt) *Client {
// A request.Request will be initialized with the http.Request, Handlers,
// params and data.
func (c *Client) NewRequest(ctx context.Context, method, path string, params interface{}, data interface{}) *request.Request {
httpReq, _ := http.NewRequest(method, path, nil)
httpReq = httpReq.WithContext(ctx)
httpReq, _ := http.NewRequestWithContext(ctx, method, path, nil)
httpReq.URL, _ = url.Parse(c.Info.Endpoint + path)

r := request.New(httpReq, c.Info, c.Handlers.Copy(), params, data)
Expand Down
9 changes: 4 additions & 5 deletions client/request/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -138,7 +137,7 @@ func handleSendError(r *Request, err error) {
r.HTTPResponse = &http.Response{
StatusCode: int(code),
Status: http.StatusText(int(code)),
Body: ioutil.NopCloser(bytes.NewReader([]byte{})),
Body: io.NopCloser(bytes.NewReader([]byte{})),
}
return
}
Expand All @@ -150,7 +149,7 @@ func handleSendError(r *Request, err error) {
r.HTTPResponse = &http.Response{
StatusCode: int(0),
Status: http.StatusText(int(0)),
Body: ioutil.NopCloser(bytes.NewReader([]byte{})),
Body: io.NopCloser(bytes.NewReader([]byte{})),
}
}

Expand All @@ -173,7 +172,7 @@ var JSONBuilder = Handler{
return
}
r.HTTPRequest.ContentLength = int64(len(raw))
r.HTTPRequest.Body = ioutil.NopCloser(bytes.NewReader(raw))
r.HTTPRequest.Body = io.NopCloser(bytes.NewReader(raw))
}
},
}
Expand All @@ -189,7 +188,7 @@ var JSONDecoder = Handler{
defer func() {
// Read the entire body, including any trailing garbage, so
// this connection is in a good state for reuse.
io.Copy(ioutil.Discard, r.HTTPResponse.Body)
io.Copy(io.Discard, r.HTTPResponse.Body)
r.HTTPResponse.Body.Close()
}()
}
Expand Down
4 changes: 2 additions & 2 deletions counting/probabilistic_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"hash/fnv"
"math"

"github.com/pkg/errors"
"github.com/bits-and-blooms/bitset"
"github.com/pkg/errors"
)

// ProbabilisticCounter implements an a linear-time counting algorithm, also known as "linear counting".
Expand All @@ -20,7 +20,7 @@ import (
// the number of "0" entries). It then estimates the column cardinality by dividing this count by the bit map size m
// (thus obtaining the fraction of empty bit map entries V_n) and plugging the result into the following equation:
//
// n^ = -m * ln V_n (The symbol ^ denotes an estimator)"
// n^ = -m * ln V_n (The symbol ^ denotes an estimator)"
//
// Therefore, ProbabilisticCounter is able to compute an approximate distinct count for a sufficiently large number of
// string values, with an error rate of less than 1.25% on average. It does so while using a very low amount of memory,
Expand Down
6 changes: 3 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"time"

Expand Down Expand Up @@ -101,7 +101,7 @@ func (e *Error) Error() string {

// ip returns your ip.
func ip(ctx context.Context) (string, error) {
req, err := http.NewRequest("GET", "http://api.ipify.org?format=text", nil)
req, err := http.NewRequestWithContext(ctx, "GET", "http://api.ipify.org?format=text", nil)
if err != nil {
return "", err
}
Expand All @@ -115,7 +115,7 @@ func ip(ctx context.Context) (string, error) {
resp := val.(*http.Response)
defer resp.Body.Close()

raw, err := ioutil.ReadAll(resp.Body)
raw, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
Expand Down
163 changes: 98 additions & 65 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,97 +1,130 @@
module github.com/remind101/pkg

go 1.21.5
go 1.24.2

require (
cloud.google.com/go/profiler v0.4.0
cloud.google.com/go/profiler v0.4.2
github.com/99designs/httpsignatures-go v0.0.0-20170731043157-88528bf4ca7e
github.com/DataDog/datadog-go v4.8.3+incompatible
github.com/aws/aws-sdk-go v1.49.21
github.com/bits-and-blooms/bitset v1.13.0
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/aws/aws-sdk-go v1.55.6
github.com/bits-and-blooms/bitset v1.22.0
github.com/cenkalti/backoff/v4 v4.3.0
github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab
github.com/go-redis/redis v6.15.9+incompatible
github.com/gomodule/redigo v1.8.9
github.com/gomodule/redigo v1.9.2
github.com/gorilla/mux v1.8.1
github.com/newrelic/go-agent v3.14.1+incompatible
github.com/opentracing/opentracing-go v1.2.0
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/remind101/newrelic v0.0.0-20161025005755-2671e3bb51df
github.com/rollbar/rollbar-go v1.4.5
github.com/rollbar/rollbar-go v1.4.6
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
gopkg.in/DataDog/dd-trace-go.v1 v1.58.1
github.com/stretchr/testify v1.10.0
github.com/urfave/cli/v2 v2.27.1
go.uber.org/zap v1.27.0
gopkg.in/DataDog/dd-trace-go.v1 v1.72.2
)

require (
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/DataDog/appsec-internal-go v1.4.0 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.50.2 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.50.2 // indirect
github.com/DataDog/datadog-go/v5 v5.4.0 // indirect
github.com/DataDog/go-libddwaf/v2 v2.2.3 // indirect
github.com/DataDog/go-sqllexer v0.0.10 // indirect
github.com/DataDog/go-tuf v1.0.2-0.5.2 // indirect
github.com/DataDog/sketches-go v1.4.4 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
cloud.google.com/go v0.120.0 // indirect
cloud.google.com/go/auth v0.15.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
github.com/DataDog/appsec-internal-go v1.11.2 // indirect
github.com/DataDog/datadog-agent/comp/core/tagger/origindetection v0.64.2 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.64.2 // indirect
github.com/DataDog/datadog-agent/pkg/proto v0.64.2 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.64.2 // indirect
github.com/DataDog/datadog-agent/pkg/trace v0.64.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/log v0.64.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.64.2 // indirect
github.com/DataDog/datadog-agent/pkg/version v0.64.2 // indirect
github.com/DataDog/datadog-go/v5 v5.6.0 // indirect
github.com/DataDog/go-libddwaf/v3 v3.5.4 // indirect
github.com/DataDog/go-runtime-metrics-internal v0.0.4-0.20241206090539-a14610dc22b6 // indirect
github.com/DataDog/go-sqllexer v0.1.3 // indirect
github.com/DataDog/go-tuf v1.1.0-0.5.2 // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.26.0 // indirect
github.com/DataDog/sketches-go v1.4.7 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/ebitengine/purego v0.5.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/eapache/queue/v2 v2.0.0-20230407133247-75960ed334e4 // indirect
github.com/ebitengine/purego v0.8.2 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/pprof v0.0.0-20231229205709-960ae82b1e42 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 // indirect
github.com/google/s2a-go v0.1.9 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.2.0 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.7 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.14.0 // indirect
github.com/outcaste-io/ristretto v0.2.3 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.8.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/tinylib/msgp v1.1.9 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.9.0 // indirect
github.com/shirou/gopsutil/v4 v4.25.3 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/tinylib/msgp v1.2.5 // indirect
github.com/tklauser/go-sysconf v0.3.15 // indirect
github.com/tklauser/numcpus v0.10.0 // indirect
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/component v1.29.0 // indirect
go.opentelemetry.io/collector/featuregate v1.29.0 // indirect
go.opentelemetry.io/collector/internal/telemetry v0.123.0 // indirect
go.opentelemetry.io/collector/pdata v1.29.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.123.0 // indirect
go.opentelemetry.io/collector/semconv v0.123.0 // indirect
go.opentelemetry.io/contrib/bridges/otelzap v0.10.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/log v0.11.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/sdk v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go4.org/intern v0.0.0-20230525184215-6c62f75575cb // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/api v0.156.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240108191215-35c7eff3a6b1 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/protobuf v1.32.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/oauth2 v0.29.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect
golang.org/x/time v0.11.0 // indirect
golang.org/x/tools v0.32.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
google.golang.org/api v0.228.0 // indirect
google.golang.org/genproto v0.0.0-20250407143221-ac9807e6c755 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250407143221-ac9807e6c755 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250407143221-ac9807e6c755 // indirect
google.golang.org/grpc v1.71.1 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect
)
Loading