fix error result cache

This commit is contained in:
Zheng Kai
2023-06-25 18:49:24 +08:00
parent 05fe88de8b
commit 645873a6d1
4 changed files with 50 additions and 32 deletions

View File

@@ -4,6 +4,7 @@ import "sync"
// Core ...
type Core struct {
serial int
pool map[[16]byte]*row
mux sync.Mutex
}

View File

@@ -11,14 +11,14 @@ import (
"time"
)
func (pr *row) fetchRemote() (ab []byte, ok bool, err error) {
func (pr *row) fetchRemote() (ab []byte, err error) {
r := pr.req
b := pr.log
u, err := url.Parse(config.OpenAIBase + r.Url)
if err != nil {
return nil, false, err
return nil, err
}
// zj.J(`real url`, u.String())
@@ -39,9 +39,7 @@ func (pr *row) fetchRemote() (ab []byte, ok bool, err error) {
return
}
if rsp.StatusCode >= 200 || rsp.StatusCode < 300 {
ok = true
} else {
if rsp.StatusCode < 200 || rsp.StatusCode >= 300 {
err = fmt.Errorf(`status code fail: %d`, rsp.StatusCode)
b.WriteString(err.Error())
}

View File

@@ -19,11 +19,14 @@ func (c *Core) add(req *pb.Req, hr *http.Request) (pr *row, cached bool) {
return
}
c.serial++
pr = &row{
serial: c.serial,
hr: hr,
hash: hash,
req: req,
t: time.Now(),
core: c,
}
pr.mux.Lock()
go pr.run()
@@ -31,3 +34,12 @@ func (c *Core) add(req *pb.Req, hr *http.Request) (pr *row, cached bool) {
c.mux.Unlock()
return
}
func (c *Core) delete(r *row) {
c.mux.Lock()
pr, ok := c.pool[r.hash]
if ok && pr == r {
delete(c.pool, r.hash)
}
c.mux.Unlock()
}

View File

@@ -11,6 +11,7 @@ import (
)
type row struct {
serial int
hash [16]byte
hr *http.Request
req *pb.Req
@@ -20,6 +21,12 @@ type row struct {
t time.Time
mux sync.RWMutex
log *bytes.Buffer
core *Core
}
func (pr *row) suicide() {
pr.core.delete(pr)
pr.core = nil
}
func (pr *row) run() {
@@ -33,20 +40,20 @@ func (pr *row) run() {
// return
}
var ok bool
pr.rsp, ok, pr.err = pr.fetchRemote()
if pr.err == nil && ok {
// pr.failLog.Reset()
go writeFailLog(pr.hash, pr.log.Bytes())
} else {
go writeFailLog(pr.hash, pr.log.Bytes())
}
go pr.saveFile()
// go pr.metrics()
pr.rsp, pr.err = pr.fetchRemote()
pr.done = true
pr.mux.Unlock()
// go pr.metrics()
if pr.err == nil {
// pr.failLog.Reset()
// go writeFailLog(pr.hash, pr.log.Bytes())
go pr.saveFile()
} else {
go writeFailLog(pr.hash, pr.log.Bytes())
go pr.suicide()
}
}
func (pr *row) wait() {