Skip to content

Commit

Permalink
remove unecessary context (#465)
Browse files Browse the repository at this point in the history
* remove unecessary context

* nolint for nil

Co-authored-by: anrs <[email protected]>
  • Loading branch information
CMGS and anrs authored Aug 30, 2021
1 parent 46f170b commit 7e716b6
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 85 deletions.
2 changes: 1 addition & 1 deletion client/interceptor/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *retryStream) RecvMsg(m interface{}) (err error) {
}

return backoff.Retry(func() error {
log.Debug(context.TODO(), "[retryStream] retry on new stream")
log.Debug(nil, "[retryStream] retry on new stream") //nolint
stream, err := s.newStream()
if err != nil {
// even io.EOF triggers retry, and it's what we want!
Expand Down
8 changes: 4 additions & 4 deletions client/resolver/eru/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (r *Resolver) Close() {
}

func (r *Resolver) sync() {
log.Debug(context.TODO(), "[EruResolver] start sync service discovery")
ctx, cancel := context.WithCancel(context.Background())
r.cancel = cancel
defer cancel()
ctx := context.TODO()
log.Debug(ctx, "[EruResolver] start sync service discovery")
ctx, r.cancel = context.WithCancel(ctx)
defer r.cancel()

ch, err := r.discovery.Watch(ctx)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions client/utils/servicepusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (p *EndpointPusher) delOutdated(endpoints []string) {
if _, ok := newEps[ep]; !ok {
cancel()
p.pendingEndpoints.Delete(ep)
log.Debugf(context.TODO(), "[EruResolver] pending endpoint deleted: %s", ep)
log.Debugf(nil, "[EruResolver] pending endpoint deleted: %s", ep) //nolint
}
return true
})
Expand All @@ -66,7 +66,7 @@ func (p *EndpointPusher) delOutdated(endpoints []string) {
}
if _, ok := newEps[ep]; !ok {
p.availableEndpoints.Delete(ep)
log.Debugf(context.TODO(), "[EruResolver] available endpoint deleted: %s", ep)
log.Debugf(nil, "[EruResolver] available endpoint deleted: %s", ep) //nolint
}
return true
})
Expand All @@ -81,16 +81,17 @@ func (p *EndpointPusher) addCheck(endpoints []string) {
continue
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(context.TODO())
p.pendingEndpoints.Store(endpoint, cancel)
go p.pollReachability(ctx, endpoint)
log.Debugf(ctx, "[EruResolver] pending endpoint added: %s", endpoint)
}
}

func (p *EndpointPusher) pollReachability(ctx context.Context, endpoint string) {
parts := strings.Split(endpoint, ":")
if len(parts) != 2 {
log.Errorf(context.TODO(), "[EruResolver] wrong format of endpoint: %s", endpoint)
log.Errorf(ctx, "[EruResolver] wrong format of endpoint: %s", endpoint)
return
}

Expand Down Expand Up @@ -118,7 +119,7 @@ func (p *EndpointPusher) pollReachability(ctx context.Context, endpoint string)
func (p *EndpointPusher) checkReachability(host string) (err error) {
pinger, err := ping.NewPinger(host)
if err != nil {
log.Errorf(context.TODO(), "[EruResolver] failed to create pinger: %+v", err)
log.Errorf(nil, "[EruResolver] failed to create pinger: %+v", err) //nolint
return
}
defer pinger.Stop()
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func withImageBuiltChannel(f func(chan *types.BuildImageMessage)) chan *types.Bu

func cleanupNodeImages(ctx context.Context, node *types.Node, ids []string, ttl time.Duration) {
logger := log.WithField("Calcium", "cleanupNodeImages").WithField("node", node).WithField("ids", ids).WithField("ttl", ttl)
ctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.Background()), ttl)
ctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.TODO()), ttl)
defer cancel()
for _, id := range ids {
if _, err := node.Engine.ImageRemove(ctx, id, false, true); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/calcium.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func New(config types.Config, t *testing.T) (*Calcium, error) {
log.Warn("[Calcium] SCM not set, build API disabled")
}
if err != nil {
logger.Errorf(context.TODO(), "[Calcium] SCM failed: %+v", err)
logger.Errorf(nil, "[Calcium] SCM failed: %+v", err) //nolint
return nil, errors.WithStack(err)
}

Expand All @@ -81,7 +81,7 @@ func New(config types.Config, t *testing.T) (*Calcium, error) {
cal := &Calcium{store: store, config: config, scheduler: potassium, source: scm, watcher: watcher}
cal.wal, err = newCalciumWAL(cal)
cal.identifier = config.Identifier()
return cal, logger.Err(context.TODO(), errors.WithStack(err))
return cal, logger.Err(nil, errors.WithStack(err)) //nolint
}

// DisasterRecover .
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio

utils.SentryGo(func() {
defer func() {
cctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.Background()), c.config.GlobalTimeout)
cctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.TODO()), c.config.GlobalTimeout)
for nodename := range deployMap {
if e := c.store.DeleteProcessing(cctx, opts.GetProcessing(nodename)); e != nil {
logger.Errorf(ctx, "[Calcium.doCreateWorkloads] delete processing failed for %s: %+v", nodename, e)
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (c *Calcium) doLock(ctx context.Context, name string, timeout time.Duration
}
defer func() {
if err != nil {
rollbackCtx, cancel := context.WithTimeout(context.Background(), timeout)
rollbackCtx, cancel := context.WithTimeout(context.TODO(), timeout)
defer cancel()
rollbackCtx = utils.InheritTracingInfo(rollbackCtx, ctx)
if e := lock.Unlock(rollbackCtx); e != nil {
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (c *Calcium) remapResource(ctx context.Context, node *types.Node) (ch <-cha

func (c *Calcium) doRemapResourceAndLog(ctx context.Context, logger log.Fields, node *types.Node) {
log.Debugf(ctx, "[doRemapResourceAndLog] remap node %s", node.Name)
ctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.Background()), c.config.GlobalTimeout)
ctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.TODO()), c.config.GlobalTimeout)
defer cancel()
logger = logger.WithField("Calcium", "doRemapResourceAndLog").WithField("nodename", node.Name)
if ch, err := c.remapResource(ctx, node); logger.Err(ctx, err) == nil {
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (h *CreateLambdaHandler) Handle(ctx context.Context, raw interface{}) error

workloadIDs, err := h.getWorkloadIDs(ctx, opts)
if err != nil {
log.Errorf(context.TODO(), "[CreateLambdaHandler.Handle] Get workloads %s/%s/%v failed: %v",
log.Errorf(nil, "[CreateLambdaHandler.Handle] Get workloads %s/%s/%v failed: %v", //nolint
opts.Appname, opts.Entrypoint, opts.Labels, err)
return err
}
Expand Down
22 changes: 11 additions & 11 deletions core.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"context"
"fmt"
"net"
"net/http"
_ "net/http/pprof" // nolint
"os"
"os/signal"
"syscall"
"testing"
"time"

Expand All @@ -20,7 +21,6 @@ import (
"github.com/projecteru2/core/utils"
"github.com/projecteru2/core/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sethvargo/go-signalcontext"
cli "github.com/urfave/cli/v2"
"google.golang.org/grpc"

Expand Down Expand Up @@ -61,13 +61,13 @@ func serve(c *cli.Context) error {
}

if sentryDefer, err := setupSentry(config.SentryDSN); err != nil {
log.Warnf(context.TODO(), "[main] sentry %v", err)
log.Warnf(nil, "[main] sentry %v", err) //nolint
} else if sentryDefer != nil {
defer sentryDefer()
}

if err := metrics.InitMetrics(config); err != nil {
log.Errorf(context.TODO(), "[main] %v", err)
log.Errorf(nil, "[main] %v", err) //nolint
return err
}

Expand All @@ -77,7 +77,7 @@ func serve(c *cli.Context) error {
}
cluster, err := calcium.New(config, t)
if err != nil {
log.Errorf(context.TODO(), "[main] %v", err)
log.Errorf(nil, "[main] %v", err) //nolint
return err
}
defer cluster.Finalizer()
Expand All @@ -87,7 +87,7 @@ func serve(c *cli.Context) error {
vibranium := rpc.New(cluster, config, stop)
s, err := net.Listen("tcp", config.Bind)
if err != nil {
log.Errorf(context.TODO(), "[main] %v", err)
log.Errorf(nil, "[main] %v", err) //nolint
return err
}

Expand All @@ -101,35 +101,35 @@ func serve(c *cli.Context) error {
auth := auth.NewAuth(config.Auth)
opts = append(opts, grpc.StreamInterceptor(auth.StreamInterceptor))
opts = append(opts, grpc.UnaryInterceptor(auth.UnaryInterceptor))
log.Infof(context.TODO(), "[main] Username %s Password %s", config.Auth.Username, config.Auth.Password)
log.Infof(nil, "[main] Username %s Password %s", config.Auth.Username, config.Auth.Password) //nolint
}

grpcServer := grpc.NewServer(opts...)
pb.RegisterCoreRPCServer(grpcServer, vibranium)
utils.SentryGo(func() {
if err := grpcServer.Serve(s); err != nil {
log.Errorf(context.TODO(), "[main] start grpc failed %v", err)
log.Errorf(nil, "[main] start grpc failed %v", err) //nolint
}
})

if config.Profile != "" {
http.Handle("/metrics", metrics.Client.ResourceMiddleware(cluster)(promhttp.Handler()))
utils.SentryGo(func() {
if err := http.ListenAndServe(config.Profile, nil); err != nil {
log.Errorf(context.TODO(), "[main] start http failed %v", err)
log.Errorf(nil, "[main] start http failed %v", err) //nolint
}
})
}

unregisterService, err := cluster.RegisterService(c.Context)
if err != nil {
log.Errorf(context.TODO(), "[main] failed to register service: %v", err)
log.Errorf(nil, "[main] failed to register service: %v", err) //nolint
return err
}
log.Info("[main] Cluster started successfully.")

// wait for unix signals and try to GracefulStop
ctx, cancel := signalcontext.OnInterrupt()
ctx, cancel := signal.NotifyContext(c.Context, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
<-ctx.Done()

Expand Down
4 changes: 2 additions & 2 deletions discovery/helium/helium.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func New(config types.GRPCConfig, stor store.Store) *Helium {
h.config = config
h.stor = stor
h.Do(func() {
h.start(context.TODO()) // rewrite ctx here, because this will run only once!
h.start(context.TODO()) // TODO rewrite ctx here, because this will run only once!
})
return h
}
Expand All @@ -45,7 +45,7 @@ func (h *Helium) Unsubscribe(id uuid.UUID) {
func (h *Helium) start(ctx context.Context) {
ch, err := h.stor.ServiceStatusStream(ctx)
if err != nil {
log.Errorf(context.TODO(), "[WatchServiceStatus] failed to start watch: %v", err)
log.Errorf(nil, "[WatchServiceStatus] failed to start watch: %v", err) //nolint
return
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/go-git/go-git/v5 v5.2.0
github.com/go-ping/ping v0.0.0-20210407214646-e4e642a95741
github.com/go-redis/redis/v8 v8.8.2
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.1.2
github.com/gorilla/mux v1.7.4 // indirect
github.com/jinzhu/configor v1.2.0
Expand All @@ -35,7 +36,6 @@ require (
github.com/projecteru2/libyavirt v0.0.0-20210514093713-959b2e232319
github.com/prometheus/client_golang v1.11.0
github.com/sanity-io/litter v1.5.1
github.com/sethvargo/go-signalcontext v0.1.0
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.2.0
Expand Down
Loading

0 comments on commit 7e716b6

Please sign in to comment.