Skip to content

Commit

Permalink
Merge pull request #202 from heroku/aa-update-go-and-linter
Browse files Browse the repository at this point in the history
Update golang and linter
  • Loading branch information
voidlock committed Oct 30, 2023
2 parents 8918beb + 145ef16 commit 0850add
Show file tree
Hide file tree
Showing 44 changed files with 269 additions and 204 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
matrix:
target: [ test, lint ]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
- name: run ${{ matrix.target }}
Expand Down
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# See https://github.com/golangci/golangci-lint#config-file
run:
deadline: 1m #Default
deadline: 2m #Default
issues-exit-code: 1 #Default
tests: true #Default

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ GOPATH = $(shell go env GOPATH)
TOOLS_DIR = $(TOP_LEVEL)/.tools
TOOLS_BIN = $(TOOLS_DIR)/bin
# Make sure this is in-sync with the version in the circle ci config
GOLANGCI_LINT_VERSION := v1.38.0
GOLANGCI_LINT_VERSION := v1.55.0
PKG_SPEC := ./...
MOD := -mod=readonly
GOTEST := go test $(MOD)
Expand Down
20 changes: 10 additions & 10 deletions cmd/protoc-gen-loggingtags/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
//
// For example, the following proto file:
//
// syntax = "proto3";
// import "github.com/heroku/x/loggingtags/safe.proto";
// syntax = "proto3";
// import "github.com/heroku/x/loggingtags/safe.proto";
//
// package loggingtags.examples;
// package loggingtags.examples;
//
// message Sample {
// string safe = 1 [(heroku.loggingtags.safe) = true];
// string unsafe = 2;
// }
// message Sample {
// string safe = 1 [(heroku.loggingtags.safe) = true];
// string unsafe = 2;
// }
//
// will generate a method on the Sample type which implements
//
// type loggable interface{
// LoggingTags() map[string]interface{}
// }
// type loggable interface{
// LoggingTags() map[string]interface{}
// }
//
// Calling the LoggingTags() method on Sample will return only the name and
// value of the `safe` field.
Expand Down
7 changes: 3 additions & 4 deletions cmd/protoc-gen-loggingtags/golden_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestGenerate(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
workdir, err := ioutil.TempDir("", "proto-test")
workdir, err := os.MkdirTemp("", "proto-test")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -95,7 +94,7 @@ func TestGenerate(t *testing.T) {
}

func TestSample(t *testing.T) {
workdir, err := ioutil.TempDir("", "proto-test")
workdir, err := os.MkdirTemp("", "proto-test")
if err != nil {
t.Fatal(err)
}
Expand All @@ -105,7 +104,7 @@ func TestSample(t *testing.T) {
"--proto_path=" + filepath.Join("internal", "test"),
"--proto_path=.",
"--loggingtags_out=" + workdir,
filepath.Join("sample.proto"),
"sample.proto",
})

goldenPath := filepath.Join("internal", "test", "sample.pb.loggingtags.go")
Expand Down
6 changes: 5 additions & 1 deletion cmd/protoc-gen-loggingtags/internal/gen/generate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/protoc-gen-loggingtags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"flag"
"io/ioutil"
"io"
"log"
"os"
"strings"
Expand All @@ -21,7 +21,7 @@ func main() {
if *file != "stdin" {
f, _ = os.Open("input.txt")
}
input, err := ioutil.ReadAll(f)
input, err := io.ReadAll(f)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/s3env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -59,7 +58,7 @@ PREREQUISITES
S3ENV_AWS_ACCESS_KEY_ID
S3ENV_AWS_REGION
S3ENV_AWS_SECRET_ACCESS_KEY
EXAMPLES
s3env config # show all config vars
s3env config:set FOO=1 BAR=2 # set two vars
Expand Down Expand Up @@ -99,7 +98,6 @@ func loadS3Object() {
"",
),
})

if err != nil {
fmt.Printf("s3env: aws error: %s\n", err)
return
Expand Down Expand Up @@ -166,7 +164,6 @@ func persistVars() error {
Body: bytes.NewReader(buf.Bytes()),
ServerSideEncryption: aws.String(cfg.ServerSideEncryption),
})

if err != nil {
return fmt.Errorf("saving to s3 failed with error: %s", err)
}
Expand All @@ -189,7 +186,7 @@ func input() (io.ReadCloser, error) {
buf := new(bytes.Buffer)
buf.Write([]byte("{}"))

return ioutil.NopCloser(buf), nil
return io.NopCloser(buf), nil
}
return nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions cmdutil/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// trace, profile memory, etc. by running the gops command line connected to
// locahost:9999 like:
//
// $ gops stack localhost:9999
// goroutine 50 [running]:
// runtime/pprof.writeGoroutineStacks(0x4a18a20, 0xc000010138, 0x0, 0x0)
// /usr/local/Cellar/go/1.13.5/libexec/src/runtime/pprof/pprof.go:679 +0x9d
// runtime/pprof.writeGoroutine(0x4a18a20, 0xc000010138, 0x2, 0x0, 0x0)
// ...
// $ gops stack localhost:9999
// goroutine 50 [running]:
// runtime/pprof.writeGoroutineStacks(0x4a18a20, 0xc000010138, 0x0, 0x0)
// /usr/local/Cellar/go/1.13.5/libexec/src/runtime/pprof/pprof.go:679 +0x9d
// runtime/pprof.writeGoroutine(0x4a18a20, 0xc000010138, 0x2, 0x0, 0x0)
// ...
//
// Learn more about gops at https://github.com/google/gops.
package debug
Expand All @@ -24,7 +24,8 @@ import (
// New inializes a debug server listening on the provided port.
//
// Connect to the debug server with gops:
// gops stack localhost:PORT
//
// gops stack localhost:PORT
func New(l logrus.FieldLogger, port int) *Server {
return &Server{
logger: l,
Expand Down
7 changes: 3 additions & 4 deletions cmdutil/hypercmd/hyper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package hypercmd

import (
"encoding/json"
"io/ioutil"
"os"

"github.com/pkg/errors"
Expand All @@ -19,7 +18,7 @@ func New(name string, fn func()) cli.Command {
Name: name,
Usage: "Run " + name,
Before: serviceEnvLoader(name),
Action: func(c *cli.Context) error {
Action: func(_ *cli.Context) error {
fn()
return nil
},
Expand All @@ -29,8 +28,8 @@ func New(name string, fn func()) cli.Command {
// serviceEnvLoader will load JSON-format environment files before executing
// the service function.
func serviceEnvLoader(name string) cli.BeforeFunc {
return func(c *cli.Context) error {
data, err := ioutil.ReadFile(".env." + name)
return func(_ *cli.Context) error {
data, err := os.ReadFile(".env." + name)
if err != nil {
if os.IsNotExist(err) {
return nil
Expand Down
8 changes: 8 additions & 0 deletions cmdutil/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"net/url"
"time"

"github.com/heroku/x/cmdutil/debug"
"github.com/heroku/x/cmdutil/metrics"
Expand Down Expand Up @@ -80,3 +81,10 @@ type spaceCAConfig struct {
// Domain of the service used in the generation of the cert
Domain string `env:"DOMAIN"`
}

type timeoutConfig struct {
Read time.Duration `env:"SERVER_READ_TIMEOUT"`
ReadHeader time.Duration `env:"SERVER_READ_HEADER_TIMEOUT,default=30s"`
Write time.Duration `env:"SERVER_WRITE_TIMEOUT"`
Idle time.Duration `env:"SERVER_IDLE_TIMEOUT"`
}
6 changes: 0 additions & 6 deletions cmdutil/service/standard.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package service

import (
"math/rand"
"strings"
"syscall"
"time"

"github.com/joeshaw/envdecode"
"github.com/oklog/run"
Expand Down Expand Up @@ -37,10 +35,6 @@ type Standard struct {
// If appConfig is non-nil, envdecode.MustStrictDecode will be called on it
// to ensure that it is processed.
func New(appConfig interface{}, ofs ...OptionFunc) *Standard {
// Initialize the pseudo-random number generator with a unique value so we
// get unique sequences across runs.
rand.Seed(time.Now().UnixNano())

var sc standardConfig
envdecode.MustStrictDecode(&sc)

Expand Down
45 changes: 28 additions & 17 deletions cmdutil/service/standard_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type httpConfig struct {
Platform platformConfig
Bypass bypassConfig
Timeouts timeoutConfig
}

// HTTP returns a standard HTTP server for the provided handler. Port, TLS, and
Expand All @@ -44,28 +45,29 @@ func HTTP(l logrus.FieldLogger, m metrics.Provider, h http.Handler, opts ...func
var srvs []cmdutil.Server

if cfg.Platform.Port != 0 {
s := &http.Server{
Handler: h,
Addr: fmt.Sprintf(":%d", cfg.Platform.Port),
}

s := httpServerWithTimeouts(cfg.Timeouts)
s.Handler = h
s.Addr = fmt.Sprintf(":%d", cfg.Platform.Port)
o.configureServer(s)
srvs = append(srvs, standardServer(l, s))
}

if cfg.Platform.AdditionalPort != 0 {
s := &http.Server{
Handler: h,
Addr: fmt.Sprintf(":%d", cfg.Platform.AdditionalPort),
}
s := httpServerWithTimeouts(cfg.Timeouts)

s.Handler = h
s.Addr = fmt.Sprintf(":%d", cfg.Platform.AdditionalPort)
o.configureServer(s)
srvs = append(srvs, standardServer(l, s))
}

if cfg.Bypass.InsecurePort != 0 {
s := &http.Server{
Handler: h,
Addr: fmt.Sprintf(":%d", cfg.Bypass.InsecurePort),
}

s := httpServerWithTimeouts(cfg.Timeouts)
s.Handler = h
s.Addr = fmt.Sprintf(":%d", cfg.Bypass.InsecurePort)

o.configureServer(s)
srvs = append(srvs, bypassServer(l, s))
}
Expand All @@ -76,11 +78,11 @@ func HTTP(l logrus.FieldLogger, m metrics.Provider, h http.Handler, opts ...func
tlsConfig = newTLSConfig(cfg.Bypass.TLS)
}

s := &http.Server{
Handler: h,
Addr: fmt.Sprintf(":%d", cfg.Bypass.SecurePort),
TLSConfig: tlsConfig,
}
s := httpServerWithTimeouts(cfg.Timeouts)
s.Handler = h
s.TLSConfig = tlsConfig
s.Addr = fmt.Sprintf(":%d", cfg.Bypass.SecurePort)

o.configureServer(s)
srvs = append(srvs, bypassServer(l, s))
}
Expand All @@ -94,6 +96,15 @@ func HTTP(l logrus.FieldLogger, m metrics.Provider, h http.Handler, opts ...func
return cmdutil.MultiServer(srvs...)
}

func httpServerWithTimeouts(t timeoutConfig) *http.Server {
return &http.Server{
ReadTimeout: t.Read,
ReadHeaderTimeout: t.ReadHeader,
WriteTimeout: t.Write,
IdleTimeout: t.Idle,
}
}

type httpOptions struct {
skipEnforceHTTPS bool
tlsConfig *tls.Config
Expand Down
Loading

0 comments on commit 0850add

Please sign in to comment.