Skip to content

Commit

Permalink
合并拉取请求 #9
Browse files Browse the repository at this point in the history
optimize
  • Loading branch information
aurorax-neo authored Apr 6, 2024
2 parents 7519572 + 21570d6 commit c081095
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
6 changes: 3 additions & 3 deletions chat/gpt35.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func (G *Gpt35) getNewSession() error {
// 创建请求
request, err := G.NewRequest("POST", SessionUrl, body)
if err != nil {
return nil
return err
}
// 设置请求头
request.Header.Set("oai-device-id", G.Session.OaiDeviceId)
// 发送 POST 请求
response, err := G.Client.Do(request)
if err != nil {
return nil
return err
}
if response.StatusCode != 200 {
return fmt.Errorf("StatusCode: %d", response.StatusCode)
Expand All @@ -99,7 +99,7 @@ func (G *Gpt35) getNewSession() error {
_ = Body.Close()
}(response.Body)
if err := json.NewDecoder(response.Body).Decode(&G.Session); err != nil {
return nil
return err
}
return nil
}
Expand Down
68 changes: 39 additions & 29 deletions pool/gpt35pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"free-gpt3.5-2api/config"
"github.com/aurorax-neo/go-logger"
"sync"
"time"
)

func init() {
Expand All @@ -23,7 +24,6 @@ type Gpt35Pool struct {
Index int
MaxCount int
LiveCount int
mutex sync.Mutex
}

func GetGpt35PoolInstance() *Gpt35Pool {
Expand All @@ -35,57 +35,67 @@ func GetGpt35PoolInstance() *Gpt35Pool {
MaxCount: config.CONFIG.PoolMaxCount,
}
logger.Logger.Info(fmt.Sprint("PoolMaxCount: ", config.CONFIG.PoolMaxCount, ", AuthUseCount: ", config.CONFIG.AuthUseCount, ", AuthExpirationDate: ", config.CONFIG.AuthED, ", Init Gpt35Pool..."))
// 启动一个 goroutine 刷新 Gpt35Pool
go gpt35PoolInstance.flushGpt35Pool()
// 定时刷新 Gpt35Pool
go gpt35PoolInstance.timingFlushGpt35Pool(60)
})
return gpt35PoolInstance
}

func (G *Gpt35Pool) GetGpt35(retry int) *chat.Gpt35 {
G.mutex.Lock()
defer G.mutex.Unlock()
// 索引加 1
G.Index++
// 如果索引等于最大数量,则重置为 0
if G.Index >= G.MaxCount {
if G.Index < 0 || G.Index >= G.MaxCount {
G.Index = 0
}
// 返回索引对应的 Gpt35 实例
gpt35 := G.Gpt35s[G.Index]
// 如果 Gpt35 实例为空且重试次数大于 0,则重新获取 Gpt35 实例
if gpt35 == nil && retry > 0 {
if G._isLive(G.Index) {
gpt35 := G.Gpt35s[G.Index]
gpt35.MaxUseCount--
return G.Gpt35s[G.Index]
} else if retry > 0 { // 如果 Gpt35 实例为空且重试次数大于 0,则重新获取 Gpt35 实例
G.raGpt35AtIndex(G.Index)
retry--
go G.raGpt35AtIndex(G.Index)
G.Index--
return G.GetGpt35(retry)
}
gpt35.MaxUseCount--
return gpt35
return nil
}

func (G *Gpt35Pool) flushGpt35Pool() {
for i := 0; i < G.MaxCount; i++ {
//判断是否为空
if G.Gpt35s[i] == nil || //空的
G.Gpt35s[i].MaxUseCount <= 0 || //可使用次数为0
G.Gpt35s[i].IsLapse || //失效
G.Gpt35s[i].ExpiresIn <= common.GetTimestampSecond(0) { //过期
//重新初始化
G.raGpt35AtIndex(i)
} else {
G.LiveCount++
}
if i == G.MaxCount-1 {
G.LiveCount = 0
i = -1
func (G *Gpt35Pool) timingFlushGpt35Pool(sec int) {
ticker := time.NewTicker(time.Duration(sec) * time.Second)
defer ticker.Stop()
G._flushGpt35Pool()
for {
select {
case <-ticker.C:
G._flushGpt35Pool()
}
}
}

func (G *Gpt35Pool) raGpt35AtIndex(index int) {
G.mutex.Lock()
defer G.mutex.Unlock()
if index < 0 || index >= len(G.Gpt35s) {
return
}
G.Gpt35s[index] = chat.NewGpt35()
}

func (G *Gpt35Pool) _flushGpt35Pool() {
for i := 0; i < G.MaxCount; i++ {
if !G._isLive(i) { //过期
G.raGpt35AtIndex(i)
}
}
}

func (G *Gpt35Pool) _isLive(index int) bool {
//判断是否为空
if G.Gpt35s[index] == nil || //空的
G.Gpt35s[index].MaxUseCount <= 0 || //可使用次数为0
G.Gpt35s[index].IsLapse || //失效
G.Gpt35s[index].ExpiresIn <= common.GetTimestampSecond(0) {
return false
}
return true
}
1 change: 0 additions & 1 deletion v1/chat/gpt35.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func gpt35(c *gin.Context, apiReq *reqmodel.ApiReq) {
instance := pool.GetGpt35PoolInstance().GetGpt35(3)
if instance == nil {
logger.Logger.Error("Pool GetGpt35 is empty")
instance.IsLapse = true
v1.ErrorResponse(c, http.StatusInternalServerError, "Pool GetGpt35 is empty", nil)
return
}
Expand Down

0 comments on commit c081095

Please sign in to comment.