Created
July 15, 2016 15:13
-
-
Save prashantv/8afc18b58304b855e257cedc84807b9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"log" | |
"net/http" | |
"time" | |
"github.com/pkg/errors" | |
"github.com/uber/tchannel-go" | |
thttp "github.com/uber/tchannel-go/http" | |
) | |
type handler struct { | |
sc *tchannel.SubChannel | |
} | |
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
if err := h.handleRequest(w, r); err != nil { | |
log.Printf("Failed to handle request: %v", err) | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
} | |
func (h handler) handleRequest(w http.ResponseWriter, r *http.Request) error { | |
ctx, cancel := tchannel.NewContext(300 * time.Second) | |
defer cancel() | |
call, err := h.sc.BeginCall(ctx, "_pprof", &tchannel.CallOptions{ | |
Format: tchannel.HTTP, | |
}) | |
if err != nil { | |
return errors.Wrap(err, "Failed to start call to _pprof") | |
} | |
if err := thttp.WriteRequest(call, r); err != nil { | |
return errors.Wrap(err, "failed to write HTTP request to TChannel") | |
} | |
resp, err := thttp.ReadResponse(call.Response()) | |
if err != nil { | |
return errors.Wrap(err, "failed to read HTTP response from TChannel") | |
} | |
defer resp.Body.Close() | |
for k, vs := range resp.Header { | |
for _, v := range vs { | |
w.Header().Add(k, v) | |
} | |
} | |
w.WriteHeader(resp.StatusCode) | |
if _, err := io.Copy(w, resp.Body); err != nil { | |
return errors.Wrap(err, "failed to copy HTTP response body from TChannel") | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment