fix error result cache something else

This commit is contained in:
Zheng Kai
2023-06-28 16:23:14 +08:00
parent 645873a6d1
commit 770510ccdc
5 changed files with 61 additions and 28 deletions

12
proto/openai.proto Normal file
View File

@@ -0,0 +1,12 @@
syntax = "proto3";
option go_package = "/pb";
package pb;
message OpenAIError {
OpenAIErrorDetail error = 1;
}
message OpenAIErrorDetail {
string message = 1;
string type = 2;
}

View File

@@ -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)
}
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)
}

View File

@@ -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
}

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"project/pb"
"project/util"
"project/zj"
"sync"
"time"
)
@@ -17,6 +18,7 @@ type row struct {
req *pb.Req
rsp []byte
err error
httpCode int
done bool
t time.Time
mux sync.RWMutex
@@ -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() {

View File

@@ -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)
if len(ab) > 0 {
w.Header().Add(`Content-Type`, `application/json`)
w.Write(ab)
}
go doMetrics(ab, cached, r, p)
}