in proxy/protocol/grpc/reverse_proxy.go [90:141]
func LocalRequestHandler(w http.ResponseWriter, r *http.Request) {
prepareRequest(r)
inv := consumerPreHandler(r)
source := stringutil.SplitFirstSep(r.RemoteAddr, ":")
var err error
h := make(map[string]string)
for k := range r.Header {
h[k] = r.Header.Get(k)
}
//Resolve Destination
if r.URL.Scheme == "" {
r.URL.Scheme = "http"
}
if r.URL.Host == "" {
r.URL.Host = r.Host
}
serviceName, port, err := dr.Resolve(source, "", r.URL.String(), h)
if err != nil {
WriteErrorResponse(inv, w, r, http.StatusBadRequest, err)
return
}
inv.MicroServiceName = serviceName
if port != "" {
h[XForwardedPort] = port
}
//transfer header into ctx
inv.Ctx = context.WithValue(inv.Ctx, chassisCommon.ContextHeaderKey{}, h)
c, err := handler.GetChain(chassisCommon.Consumer, common.ChainConsumerOutgoing)
if err != nil {
WriteErrorResponse(inv, w, r, http.StatusBadGateway, err)
openlog.Error("Get chain failed: " + err.Error())
return
}
defer func(begin time.Time) {
timeTaken := time.Since(begin).Seconds()
serviceLabelValues := map[string]string{metrics.LServiceName: inv.MicroServiceName, metrics.LApp: inv.RouteTags.AppID(), metrics.LVersion: inv.RouteTags.Version()}
metrics.RecordLatency(serviceLabelValues, timeTaken)
}(time.Now())
var invRsp *invocation.Response
c.Next(inv, func(ir *invocation.Response) {
//Send the request to the destination
invRsp = ir
})
resp, err := handleRequest(w, r, inv, invRsp)
if err != nil {
openlog.Error("Handle request failed: " + err.Error())
return
}
RecordStatus(inv, resp.StatusCode)
}