Skip to content

Commit

Permalink
Add extra context transfer package
Browse files Browse the repository at this point in the history
  • Loading branch information
nicmue committed Aug 25, 2020
1 parent 6506a6d commit c209370
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
22 changes: 22 additions & 0 deletions pkg/context/transfer.go
@@ -0,0 +1,22 @@
package context

import (
"context"

"github.com/pace/bricks/http/oauth2"
"github.com/pace/bricks/locale"
"github.com/pace/bricks/maintenance/errors"
"github.com/pace/bricks/maintenance/log"
)

// Transfer takes the logger, log.Sink, authentication and
// error info from the given context and returns a complete
// new context with all these objects.
func Transfer(in context.Context) context.Context {
// transfer logger, log.Sink, authentication and error info
out := log.Ctx(in).WithContext(context.Background())
out = log.SinkContextTransfer(in, out)
out = oauth2.ContextTransfer(in, out)
out = errors.ContextTransfer(in, out)
return locale.ContextTransfer(in, out)
}
11 changes: 3 additions & 8 deletions pkg/routine/routine.go
Expand Up @@ -13,10 +13,9 @@ import (
"sync/atomic"
"syscall"

"github.com/pace/bricks/http/oauth2"
"github.com/pace/bricks/locale"
"github.com/pace/bricks/maintenance/errors"
"github.com/pace/bricks/maintenance/log"
pkgcontext "github.com/pace/bricks/pkg/context"
)

type options struct {
Expand Down Expand Up @@ -89,12 +88,8 @@ func RunNamed(parentCtx context.Context, name string, routine func(context.Conte
// canceled if the program receives a shutdown signal (SIGINT, SIGTERM), if the
// returned CancelFunc is called, or if the routine returned.
func Run(parentCtx context.Context, routine func(context.Context)) (cancel context.CancelFunc) {
// transfer logger, log.Sink, authentication and error info
ctx := log.Ctx(parentCtx).WithContext(context.Background())
ctx = log.SinkContextTransfer(parentCtx, ctx)
ctx = oauth2.ContextTransfer(parentCtx, ctx)
ctx = errors.ContextTransfer(parentCtx, ctx)
ctx = locale.ContextTransfer(parentCtx, ctx)
ctx := pkgcontext.Transfer(parentCtx)

// add routine number to context and logger
num := atomic.AddInt64(&ctr, 1)
ctx = context.WithValue(ctx, ctxNumKey{}, num)
Expand Down

0 comments on commit c209370

Please sign in to comment.