Merge branch 'feat/logging' (pull/3)

* feat/logging:
  fix: allow ResponseReader.Reader to be wrapped by transcoders
  Add logging for startup and transcoded bytes
This commit is contained in:
Barna Csorogi 2017-01-14 03:48:36 +01:00
commit 92f55287f4
3 changed files with 42 additions and 7 deletions

View file

@ -6,6 +6,9 @@ import (
"github.com/barnacs/compy/proxy" "github.com/barnacs/compy/proxy"
tc "github.com/barnacs/compy/transcoder" tc "github.com/barnacs/compy/transcoder"
"log" "log"
"os"
"os/signal"
"sync/atomic"
) )
var ( var (
@ -55,6 +58,20 @@ func main() {
p.AddTranscoder("application/javascript", ttc) p.AddTranscoder("application/javascript", ttc)
p.AddTranscoder("application/x-javascript", ttc) p.AddTranscoder("application/x-javascript", ttc)
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
read := atomic.LoadUint64(&p.ReadCount)
written := atomic.LoadUint64(&p.WriteCount)
log.Printf("compy exiting, total transcoded: %d -> %d (%3.1f%%)",
read, written, float64(written)/float64(read)*100)
os.Exit(0)
}
}()
log.Printf("compy listening on %s", *host)
var err error var err error
if *cert != "" { if *cert != "" {
err = p.StartTLS(*host, *cert, *key) err = p.StartTLS(*host, *cert, *key)

View file

@ -5,11 +5,14 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"sync/atomic"
) )
type Proxy struct { type Proxy struct {
transcoders map[string]Transcoder transcoders map[string]Transcoder
ml *mitmListener ml *mitmListener
ReadCount uint64
WriteCount uint64
} }
type Transcoder interface { type Transcoder interface {
@ -47,6 +50,7 @@ func (p *Proxy) StartTLS(host, cert, key string) error {
} }
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("serving request: %s", r.URL)
if err := p.handle(w, r); err != nil { if err := p.handle(w, r); err != nil {
log.Printf("%s while serving request: %s", err, r.URL) log.Printf("%s while serving request: %s", err, r.URL)
} }
@ -62,7 +66,15 @@ func (p *Proxy) handle(w http.ResponseWriter, r *http.Request) error {
return fmt.Errorf("error forwarding request: %s", err) return fmt.Errorf("error forwarding request: %s", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
return p.proxyResponse(newResponseWriter(w), newResponseReader(resp)) rw := newResponseWriter(w)
rr := newResponseReader(resp)
err = p.proxyResponse(rw, rr)
read := rr.counter.Count()
written := rw.rw.Count()
log.Printf("transcoded: %d -> %d (%3.1f%%)", read, written, float64(written)/float64(read)*100)
atomic.AddUint64(&p.ReadCount, read)
atomic.AddUint64(&p.WriteCount, written)
return err
} }
func forward(r *http.Request) (*http.Response, error) { func forward(r *http.Request) (*http.Response, error) {

View file

@ -4,17 +4,22 @@ import (
"io" "io"
"mime" "mime"
"net/http" "net/http"
"github.com/miolini/datacounter"
) )
type ResponseReader struct { type ResponseReader struct {
io.Reader io.Reader
r *http.Response counter *datacounter.ReaderCounter
r *http.Response
} }
func newResponseReader(r *http.Response) *ResponseReader { func newResponseReader(r *http.Response) *ResponseReader {
counter := datacounter.NewReaderCounter(r.Body)
return &ResponseReader{ return &ResponseReader{
Reader: r.Body, Reader: counter,
r: r, counter: counter,
r: r,
} }
} }
@ -34,15 +39,16 @@ func (r *ResponseReader) Request() *http.Request {
type ResponseWriter struct { type ResponseWriter struct {
io.Writer io.Writer
rw http.ResponseWriter rw *datacounter.ResponseWriterCounter
statusCode int statusCode int
headersDone bool headersDone bool
} }
func newResponseWriter(w http.ResponseWriter) *ResponseWriter { func newResponseWriter(w http.ResponseWriter) *ResponseWriter {
rw := datacounter.NewResponseWriterCounter(w)
return &ResponseWriter{ return &ResponseWriter{
Writer: w, Writer: rw,
rw: w, rw: rw,
} }
} }