Skip to content

Commit

Permalink
chore(*): enable contextcheck、noctx、gomoddirectives linter (#405)
Browse files Browse the repository at this point in the history
enable linters:

- contextcheck
- noctx
- gomoddirectives

---------

Signed-off-by: Soren Yang <[email protected]>
  • Loading branch information
lsytj0413 authored Dec 11, 2023
1 parent 1f0213e commit d9b5f4e
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 345 deletions.
17 changes: 13 additions & 4 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ linters:
- asasalint
- asciicheck
- bodyclose
# - contextcheck
- contextcheck
- errname
- errorlint
- exportloopref
# - gocritic
- godot
# - gomoddirectives
- gomoddirectives
- gosec
- nakedret
- nilerr
- nilnil
# - noctx
- noctx
- nolintlint
- prealloc
- predeclared
Expand Down Expand Up @@ -80,4 +80,13 @@ issues:
- path: _test\.go
linters:
- unparam
- gosec
- gosec
- contextcheck
- noctx
- path: \.go
linters:
- contextcheck
text: "Non-inherited new context"
- linters:
- gomoddirectives
text: "github.com/samber/slog-common"
29 changes: 17 additions & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"context"
"fmt"
"os"

Expand Down Expand Up @@ -73,16 +74,20 @@ func configureLogLevel(cmd *cobra.Command, args []string) error {
}

func main() {
common.DoWithLabels(map[string]string{
"oxia": "main",
}, func() {
if _, err := maxprocs.Set(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
})
common.DoWithLabels(
context.Background(),
map[string]string{
"oxia": "main",
},
func() {
if _, err := maxprocs.Set(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
)
}
29 changes: 17 additions & 12 deletions common/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package container

import (
"context"
"io"
"log/slog"
"net"
Expand Down Expand Up @@ -81,18 +82,22 @@ func newDefaultGrpcProvider(name, bindAddress string, registerFunc func(grpc.Ser
slog.String("bindAddress", listener.Addr().String()),
)

go common.DoWithLabels(map[string]string{
"oxia": name,
"bind": listener.Addr().String(),
}, func() {
if err := c.server.Serve(listener); err != nil {
c.log.Error(
"Failed to start serving",
slog.Any("error", err),
)
os.Exit(1)
}
})
go common.DoWithLabels(
context.Background(),
map[string]string{
"oxia": name,
"bind": listener.Addr().String(),
},
func() {
if err := c.server.Serve(listener); err != nil {
c.log.Error(
"Failed to start serving",
slog.Any("error", err),
)
os.Exit(1)
}
},
)

c.log.Info("Started Grpc server")

Expand Down
27 changes: 16 additions & 11 deletions common/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package metrics

import (
"context"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -110,17 +111,21 @@ func Start(bindAddress string) (*PrometheusMetrics, error) {

slog.Info(fmt.Sprintf("Serving Prometheus metrics at http://localhost:%d/metrics", p.port))

go common.DoWithLabels(map[string]string{
"oxia": "metrics",
}, func() {
if err = p.server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error(
"Failed to serve metrics",
slog.Any("error", err),
)
os.Exit(1)
}
})
go common.DoWithLabels(
context.Background(),
map[string]string{
"oxia": "metrics",
},
func() {
if err = p.server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error(
"Failed to serve metrics",
slog.Any("error", err),
)
os.Exit(1)
}
},
)

return p, nil
}
Expand Down
31 changes: 17 additions & 14 deletions common/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ var (

// DoWithLabels attaches the labels to the current go-routine Pprof context,
// for the duration of the call to f.
func DoWithLabels(labels map[string]string, f func()) {
func DoWithLabels(ctx context.Context, labels map[string]string, f func()) {
l := make([]string, 0, len(labels)*2)
for k, v := range labels {
l = append(l, k, v)
}

pprof.Do(
context.Background(),
ctx,
pprof.Labels(l...),
func(_ context.Context) {
f()
Expand Down Expand Up @@ -68,18 +68,21 @@ func RunProfiling() io.Closer {
slog.Info(fmt.Sprintf(" use `go tool pprof http://%s/debug/pprof/heap` to get inuse_space file", s.Addr))
slog.Info("")

go DoWithLabels(map[string]string{
"oxia": "pprof",
}, func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error(
"Unable to start debug profiling server",
slog.Any("error", err),
slog.String("component", "pprof"),
)
os.Exit(1)
}
})
go DoWithLabels(
context.Background(),
map[string]string{
"oxia": "pprof",
},
func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
slog.Error(
"Unable to start debug profiling server",
slog.Any("error", err),
slog.String("component", "pprof"),
)
os.Exit(1)
}
})

return s
}
10 changes: 7 additions & 3 deletions coordinator/impl/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ func NewCoordinator(metadataProvider MetadataProvider,
}
}

go common.DoWithLabels(map[string]string{
"oxia": "coordinator-wait-for-events",
}, c.waitForExternalEvents)
go common.DoWithLabels(
c.ctx,
map[string]string{
"oxia": "coordinator-wait-for-events",
},
c.waitForExternalEvents,
)

return c, nil
}
Expand Down
72 changes: 42 additions & 30 deletions coordinator/impl/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,23 @@ func newNodeController(addr model.ServerAddress,
return 0
})

go common.DoWithLabels(map[string]string{
"oxia": "node-controller",
"addr": nc.addr.Internal,
}, nc.healthCheckWithRetries)

go common.DoWithLabels(map[string]string{
"oxia": "node-controller-send-updates",
"addr": nc.addr.Internal,
}, nc.sendAssignmentsUpdatesWithRetries)
go common.DoWithLabels(
nc.ctx,
map[string]string{
"oxia": "node-controller",
"addr": nc.addr.Internal,
},
nc.healthCheckWithRetries,
)

go common.DoWithLabels(
nc.ctx,
map[string]string{
"oxia": "node-controller-send-updates",
"addr": nc.addr.Internal,
},
nc.sendAssignmentsUpdatesWithRetries,
)
return nc
}

Expand Down Expand Up @@ -163,30 +171,34 @@ func (n *nodeController) healthCheck(backoff backoff.BackOff) error {
ctx, cancel := context.WithCancel(n.ctx)
defer cancel()

go common.DoWithLabels(map[string]string{
"oxia": "node-controller-health-check-ping",
"addr": n.addr.Internal,
}, func() {
ticker := time.NewTicker(healthCheckProbeInterval)

for {
select {
case <-ticker.C:
pingCtx, pingCancel := context.WithTimeout(ctx, healthCheckProbeTimeout)

res, err := health.Check(pingCtx, &grpc_health_v1.HealthCheckRequest{Service: ""})
pingCancel()
if err2 := n.processHealthCheckResponse(res, err); err2 != nil {
n.log.Warn("Node stopped responding to ping")
cancel()
go common.DoWithLabels(
n.ctx,
map[string]string{
"oxia": "node-controller-health-check-ping",
"addr": n.addr.Internal,
},
func() {
ticker := time.NewTicker(healthCheckProbeInterval)

for {
select {
case <-ticker.C:
pingCtx, pingCancel := context.WithTimeout(ctx, healthCheckProbeTimeout)

res, err := health.Check(pingCtx, &grpc_health_v1.HealthCheckRequest{Service: ""})
pingCancel()
if err2 := n.processHealthCheckResponse(res, err); err2 != nil {
n.log.Warn("Node stopped responding to ping")
cancel()
return
}

case <-ctx.Done():
return
}

case <-ctx.Done():
return
}
}
})
},
)

watch, err := health.Watch(ctx, &grpc_health_v1.HealthCheckRequest{Service: ""})
if err != nil {
Expand Down
Loading

0 comments on commit d9b5f4e

Please sign in to comment.