mirror of
https://github.com/zhengkai/orca.git
synced 2026-02-04 14:42:26 +08:00
fix error result cache something else
This commit is contained in:
@@ -2,11 +2,15 @@ package core
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"project/config"
|
||||
"project/pb"
|
||||
"project/util"
|
||||
"time"
|
||||
)
|
||||
@@ -20,7 +24,6 @@ func (pr *row) fetchRemote() (ab []byte, err error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// zj.J(`real url`, u.String())
|
||||
|
||||
req, err := http.NewRequest(r.Method, u.String(), bytes.NewReader(r.Body))
|
||||
if err != nil {
|
||||
@@ -40,6 +43,7 @@ func (pr *row) fetchRemote() (ab []byte, err error) {
|
||||
}
|
||||
|
||||
if rsp.StatusCode < 200 || rsp.StatusCode >= 300 {
|
||||
pr.httpCode = rsp.StatusCode
|
||||
err = fmt.Errorf(`status code fail: %d`, rsp.StatusCode)
|
||||
b.WriteString(err.Error())
|
||||
}
|
||||
@@ -52,16 +56,24 @@ func (pr *row) fetchRemote() (ab []byte, err error) {
|
||||
|
||||
ab, err = io.ReadAll(rsp.Body)
|
||||
fmt.Fprintf(b, "rsp body: %d %v\n\n", len(ab), err)
|
||||
if err == nil {
|
||||
b.Write(ab)
|
||||
}
|
||||
b.Write(ab)
|
||||
|
||||
rsp.Body.Close()
|
||||
|
||||
if err == nil {
|
||||
e := &pb.OpenAIError{}
|
||||
json.Unmarshal(ab, e)
|
||||
if e.GetError() != nil {
|
||||
err = fmt.Errorf(`openai error: %s`, e.GetError().GetMessage())
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func writeFailLog(hash [16]byte, ab []byte) {
|
||||
date := time.Now().Format(`0102-150405`)
|
||||
date := time.Now().Format(`0102/150405`)
|
||||
file := fmt.Sprintf(`fail/%s-%x.txt`, date, hash)
|
||||
os.MkdirAll(path.Dir(util.StaticFile(file)), 0755)
|
||||
util.WriteFile(file, ab)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"project/util"
|
||||
)
|
||||
|
||||
func (c *Core) getAB(p *pb.Req, r *http.Request) (ab []byte, cached bool, err error) {
|
||||
func (c *Core) getAB(p *pb.Req, r *http.Request) (ab []byte, cached bool, pr *row, err error) {
|
||||
|
||||
canCache := p.Method != http.MethodGet && p.Method != http.MethodDelete
|
||||
|
||||
@@ -22,7 +22,7 @@ func (c *Core) getAB(p *pb.Req, r *http.Request) (ab []byte, cached bool, err er
|
||||
}
|
||||
}
|
||||
|
||||
pr, cached := c.add(p, r)
|
||||
pr, cached = c.add(p, r)
|
||||
|
||||
if canCache {
|
||||
go func() {
|
||||
@@ -51,6 +51,7 @@ func req(w http.ResponseWriter, r *http.Request) (p *pb.Req, err error) {
|
||||
|
||||
if url == `/favicon.ico` {
|
||||
err = errSkip
|
||||
err500(w)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -6,22 +6,24 @@ import (
|
||||
"net/http"
|
||||
"project/pb"
|
||||
"project/util"
|
||||
"project/zj"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type row struct {
|
||||
serial int
|
||||
hash [16]byte
|
||||
hr *http.Request
|
||||
req *pb.Req
|
||||
rsp []byte
|
||||
err error
|
||||
done bool
|
||||
t time.Time
|
||||
mux sync.RWMutex
|
||||
log *bytes.Buffer
|
||||
core *Core
|
||||
serial int
|
||||
hash [16]byte
|
||||
hr *http.Request
|
||||
req *pb.Req
|
||||
rsp []byte
|
||||
err error
|
||||
httpCode int
|
||||
done bool
|
||||
t time.Time
|
||||
mux sync.RWMutex
|
||||
log *bytes.Buffer
|
||||
core *Core
|
||||
}
|
||||
|
||||
func (pr *row) suicide() {
|
||||
@@ -44,16 +46,19 @@ func (pr *row) run() {
|
||||
pr.done = true
|
||||
pr.mux.Unlock()
|
||||
|
||||
if pr.err != nil {
|
||||
zj.W(pr.err)
|
||||
}
|
||||
|
||||
// go pr.metrics()
|
||||
|
||||
go writeFailLog(pr.hash, pr.log.Bytes())
|
||||
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()
|
||||
pr.saveFile()
|
||||
}
|
||||
pr.suicide()
|
||||
}
|
||||
|
||||
func (pr *row) wait() {
|
||||
|
||||
@@ -19,21 +19,24 @@ func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if err != errSkip {
|
||||
metrics.ReqFailCount()
|
||||
}
|
||||
err500(w)
|
||||
return
|
||||
}
|
||||
|
||||
metrics.ReqBytes(len(p.Body))
|
||||
|
||||
ab, cached, err := c.getAB(p, r)
|
||||
ab, cached, pr, err := c.getAB(p, r)
|
||||
if err != nil {
|
||||
err500(w)
|
||||
return
|
||||
if pr.httpCode != 0 {
|
||||
pr.httpCode = http.StatusInternalServerError
|
||||
}
|
||||
w.WriteHeader(pr.httpCode)
|
||||
}
|
||||
// zj.J(`cached`, cached)
|
||||
|
||||
w.Header().Add(`Content-Type`, `application/json`)
|
||||
w.Write(ab)
|
||||
if len(ab) > 0 {
|
||||
w.Header().Add(`Content-Type`, `application/json`)
|
||||
w.Write(ab)
|
||||
}
|
||||
|
||||
go doMetrics(ab, cached, r, p)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user