Skip to content

Commit

Permalink
Rename handler internals to clarify stream v unary
Browse files Browse the repository at this point in the history
I'm not sure exactly how this will shape up, but introducing streaming
will be clearer if we differentiate the unary handler implementation
from the streaming one.
  • Loading branch information
akshayjshah committed Feb 28, 2022
1 parent 5bf43f0 commit 79d4bf4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
48 changes: 24 additions & 24 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ func ServeTwirp(enable bool) HandlerOption {
// To see an example of how Handler is used in the generated code, see the
// internal/pingpb/v0 package.
type Handler struct {
methodFQN string
serviceFQN string
packageFQN string
newRequest func() proto.Message
implementation Func
// rawGRPC is used only for our hand-rolled reflection handler, which needs
// bidi streaming
rawGRPC func(
methodFQN string
serviceFQN string
packageFQN string
newRequest func() proto.Message
config handlerCfg

// Handlers must either unary or stream, but not both.
unary Func
stream func(
context.Context,
http.ResponseWriter,
*http.Request,
string, // request compression
string, // response compression
*Hooks,
)
config handlerCfg
}

// NewHandler constructs a Handler. The supplied method, service, and package
Expand Down Expand Up @@ -117,12 +117,12 @@ func NewHandler(
reg.register(serviceFQN)
}
return &Handler{
methodFQN: methodFQN,
serviceFQN: serviceFQN,
packageFQN: packageFQN,
newRequest: newRequest,
implementation: impl,
config: cfg,
methodFQN: methodFQN,
serviceFQN: serviceFQN,
packageFQN: packageFQN,
newRequest: newRequest,
unary: impl,
config: cfg,
}
}

Expand Down Expand Up @@ -247,17 +247,17 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

ctx := NewHandlerContext(r.Context(), *spec, r.Header, w.Header())
var implementation Func
var unary Func
if failed != nil {
implementation = Func(func(context.Context, proto.Message) (proto.Message, error) {
unary = Func(func(context.Context, proto.Message) (proto.Message, error) {
return nil, failed
})
} else if spec.ContentType == TypeJSON || spec.ContentType == TypeProtoTwirp {
implementation = h.implementationTwirp(w, r, spec)
unary = h.implementationTwirp(w, r, spec)
} else {
implementation = h.implementationGRPC(w, r, spec)
unary = h.implementationGRPC(w, r, spec)
}
res, err := h.wrap(implementation)(ctx, h.newRequest())
res, err := h.wrap(unary)(ctx, h.newRequest())
h.writeResult(r.Context(), w, spec, res, err)
}

Expand Down Expand Up @@ -287,20 +287,20 @@ func (h *Handler) implementationTwirp(w http.ResponseWriter, r *http.Request, sp
return nil, wrap(CodeInvalidArgument, newMalformedError("can't unmarshal Twirp protobuf body"))
}
}
return h.implementation(ctx, req)
return h.unary(ctx, req)
})
}

func (h *Handler) implementationGRPC(w http.ResponseWriter, r *http.Request, spec *Specification) Func {
return Func(func(ctx context.Context, req proto.Message) (proto.Message, error) {
if raw := h.rawGRPC; raw != nil {
raw(ctx, w, r, spec.RequestCompression, spec.ResponseCompression, h.config.Hooks)
if s := h.stream; s != nil {
s(ctx, w, r, spec.RequestCompression, spec.ResponseCompression, h.config.Hooks)
return nil, nil
}
if err := unmarshalLPM(r.Body, req, spec.RequestCompression, h.config.MaxRequestBytes); err != nil {
return nil, errorf(CodeInvalidArgument, "can't unmarshal protobuf body")
}
return h.implementation(ctx, req)
return h.unary(ctx, req)
})
}

Expand Down
2 changes: 1 addition & 1 deletion reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewReflectionHandler(reg *Registrar) (string, *http.ServeMux) {
ServeTwirp(false), // no reflection in Twirp
)
raw := &rawReflectionHandler{reg}
h.rawGRPC = raw.rawGRPC
h.stream = raw.rawGRPC

mux := http.NewServeMux()
mux.Handle(methodPath, h)
Expand Down

0 comments on commit 79d4bf4

Please sign in to comment.