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