Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2024.3-2025.1]: feature: headers in case of worker error #136

Merged
merged 8 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/roadrunner-server/pool/pool/static_pool"
"github.com/roadrunner-server/pool/worker"
"go.opentelemetry.io/otel/propagation"
"go.uber.org/zap"
"golang.org/x/net/context"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
Expand All @@ -33,6 +34,7 @@ const (
peerAuthType string = ":peer.auth-type"
delimiter string = "|:|"
apiErr string = "error"
headers string = "headers"
)

type Pool interface {
Expand Down Expand Up @@ -65,6 +67,7 @@ type rpcContext struct {
// Proxy manages GRPC/RoadRunner bridge.
type Proxy struct {
mu *sync.RWMutex
log *zap.Logger
prop propagation.TextMapPropagator
grpcPool Pool
name string
Expand All @@ -75,8 +78,9 @@ type Proxy struct {
}

// NewProxy creates a new service proxy object.
func NewProxy(name string, metadata string, grpcPool Pool, mu *sync.RWMutex, prop propagation.TextMapPropagator) *Proxy {
func NewProxy(name string, metadata string, log *zap.Logger, grpcPool Pool, mu *sync.RWMutex, prop propagation.TextMapPropagator) *Proxy {
return &Proxy{
log: log,
mu: mu,
prop: prop,
grpcPool: grpcPool,
Expand Down Expand Up @@ -192,6 +196,7 @@ func (p *Proxy) invoke(ctx context.Context, method string, in *codec.RawMessage)
if err != nil {
return nil, err
}

ctx = metadata.NewIncomingContext(ctx, md)
err = grpc.SetHeader(ctx, md)
if err != nil {
Expand All @@ -217,12 +222,26 @@ func (p *Proxy) responseMetadata(resp *payload.Payload) (metadata.MD, error) {
if len(rpcMetadata) > 0 {
md = metadata.New(rpcMetadata)

if len(md.Get(headers)) > 0 {
mdh := metadata.New(make(map[string]string))
err = json.Unmarshal([]byte(md.Get(headers)[0]), &mdh)
if err != nil {
// we don't need to return this error, log it
p.log.Error("error unmarshalling headers", zap.Error(err))
goto api
}

// join with the main metadata
md = metadata.Join(md, mdh)
}

/*
we have an error
actually, if code is OK, status.ErrorProto will be nil
actually if the code is OK, status.ErrorProto will be nil
but, we use this only in case of PHP exception happened

*/
api:
if len(md.Get(apiErr)) > 0 {
st := &spb.Status{}

Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (p *Plugin) createGRPCserver(interceptors map[string]common.Interceptor) (*
}

for _, service := range services {
px := proxy.NewProxy(fmt.Sprintf("%s.%s", service.Package, service.Name), p.config.Proto[i], p.gPool, p.mu, p.prop)
px := proxy.NewProxy(fmt.Sprintf("%s.%s", service.Package, service.Name), p.config.Proto[i], p.log.Named(service.Name), p.gPool, p.mu, p.prop)
for _, m := range service.Methods {
px.RegisterMethod(m.Name)
}
Expand Down
Loading