Files
tasks/common/testing/grpcinject/grpcinject.go
Stephan BehnkeandGitHub c9e360a3fb go fix (#9337)
## What changed?

Integrate Go 1.26's new `go fix` into workflow.

NOTE that the changes caused our linter to fire; a [separate
commit](https://github.com/temporalio/temporal/pull/9337/commits/1b23f787aef370098b35829c4d17dcc06230b4c1)
addresses those.

## Why?

Ensure Go code is standardized/modernized.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
2026-02-18 09:12:19 -08:00

63 lines
1.3 KiB
Go

package grpcinject
import (
"context"
"sync/atomic"
"google.golang.org/grpc"
)
// MetadataFunc is a function type that modifies the outgoing context with metadata
type MetadataFunc func(ctx context.Context) context.Context
type Interceptor struct {
current atomic.Value // type MetadataFunc
}
func NewInterceptor() *Interceptor {
return &Interceptor{}
}
func (i *Interceptor) Set(fn MetadataFunc) {
i.current.Store(fn)
}
func (i *Interceptor) currentFunc() MetadataFunc {
if v, ok := i.current.Load().(MetadataFunc); ok && v != nil {
return v
}
return nil
}
func (i *Interceptor) Unary() grpc.UnaryClientInterceptor {
return func(
ctx context.Context,
method string,
req, reply any,
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
if fn, ok := i.current.Load().(MetadataFunc); ok && fn != nil {
ctx = fn(ctx)
}
return invoker(ctx, method, req, reply, cc, opts...)
}
}
func (i *Interceptor) Stream() grpc.StreamClientInterceptor {
return func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
if fn, ok := i.current.Load().(MetadataFunc); ok && fn != nil {
ctx = fn(ctx)
}
return streamer(ctx, desc, cc, method, opts...)
}
}