This commit is contained in:
Zheng Kai
2023-04-27 16:02:58 +08:00
parent 92b017d0e1
commit 894b62bf29
10 changed files with 101 additions and 63 deletions

View File

@@ -6,36 +6,40 @@ import (
"net/http"
"project/pb"
"project/util"
"project/zj"
"sync"
"time"
)
type row struct {
hash [16]byte
hr *http.Request
req *pb.Req
rsp []byte
err error
done bool
t time.Time
mux sync.RWMutex
failLog *bytes.Buffer
hash [16]byte
hr *http.Request
req *pb.Req
rsp []byte
err error
done bool
t time.Time
mux sync.RWMutex
log *bytes.Buffer
}
func (pr *row) run() {
pr.t = time.Now()
s := fmt.Sprintf(`%x, %s`, pr.hash, pr.t.Format(`2006-01-02 15:04:05`))
zj.J(`new`, s)
pr.startLog()
if pr.req.Method != http.MethodGet {
// pr.mux.Unlock()
// return
}
var ok bool
pr.rsp, ok, pr.err = pr.fetchRemote()
if pr.err == nil && ok {
pr.failLog.Reset()
// pr.failLog.Reset()
go writeFailLog(pr.hash, pr.log.Bytes())
} else {
go writeFailLog(pr.hash, pr.failLog.Bytes())
go writeFailLog(pr.hash, pr.log.Bytes())
}
go pr.saveFile()
@@ -56,3 +60,24 @@ func (pr *row) saveFile() {
rspFile := rspCacheFile(pr.req)
util.WriteFile(rspFile, pr.rsp)
}
func (pr *row) startLog() {
var b bytes.Buffer
pr.log = &b
b.WriteString(time.Now().Format("2006-01-02 15:04:05.000\n"))
b.WriteString(pr.hr.Method + ` ` + pr.hr.URL.String())
b.WriteString("\n\nreq header:\n\n")
for k, v := range pr.hr.Header {
fmt.Fprintf(&b, "\t%s: %v\n", k, v)
}
b.WriteString("\n")
body := pr.req.Body
fmt.Fprintf(&b, "req body: %d\n\n", len(body))
if len(body) > 0 {
b.Write(body)
b.WriteString("\n\n")
}
}