Skip to content

Commit

Permalink
feat: no need init client, use ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Dec 18, 2024
1 parent cfd3e20 commit 14166a2
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 107 deletions.
63 changes: 0 additions & 63 deletions service/aiproxy/common/client/init.go

This file was deleted.

11 changes: 0 additions & 11 deletions service/aiproxy/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,10 @@ func SetRetryTimes(times int64) {

var DisableAutoMigrateDB = os.Getenv("DISABLE_AUTO_MIGRATE_DB") == "true"

var RelayTimeout = env.Int("RELAY_TIMEOUT", 0) // unit is second

var RateLimitKeyExpirationDuration = 20 * time.Minute

var OnlyOneLogFile = env.Bool("ONLY_ONE_LOG_FILE", false)

var (
// 代理地址
RelayProxy = env.String("RELAY_PROXY", "")
// 用户内容请求代理地址
UserContentRequestProxy = env.String("USER_CONTENT_REQUEST_PROXY", "")
// 用户内容请求超时时间,单位为秒
UserContentRequestTimeout = env.Int("USER_CONTENT_REQUEST_TIMEOUT", 30)
)

var AdminKey = env.String("ADMIN_KEY", "")

var (
Expand Down
6 changes: 2 additions & 4 deletions service/aiproxy/common/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

// import webp decoder
_ "golang.org/x/image/webp"

"github.com/labring/sealos/service/aiproxy/common/client"
)

// Regex to match data URL pattern
Expand All @@ -37,7 +35,7 @@ func GetImageSizeFromURL(url string) (width int, height int, err error) {
if err != nil {
return 0, 0, err
}
resp, err := client.UserContentRequestHTTPClient.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
Expand Down Expand Up @@ -70,7 +68,7 @@ func GetImageFromURL(ctx context.Context, url string) (string, string, error) {
if err != nil {
return "", "", err
}
resp, err := client.UserContentRequestHTTPClient.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", "", err
}
Expand Down
7 changes: 0 additions & 7 deletions service/aiproxy/common/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"strings"
"testing"

"github.com/labring/sealos/service/aiproxy/common/client"

img "github.com/labring/sealos/service/aiproxy/common/image"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -44,11 +42,6 @@ var cases = []struct {
{"https://upload.wikimedia.org/wikipedia/commons/6/62/102Cervus.jpg", "jpeg", 270, 230},
}

func TestMain(m *testing.M) {
client.Init()
m.Run()
}

func TestDecode(t *testing.T) {
// Bytes read: varies sometimes
// jpeg: 1063892
Expand Down
14 changes: 3 additions & 11 deletions service/aiproxy/controller/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,11 @@ func Relay(c *gin.Context) {
}

func shouldRetry(_ *gin.Context, statusCode int) bool {
if statusCode == http.StatusTooManyRequests {
if statusCode == http.StatusTooManyRequests ||
statusCode == http.StatusGatewayTimeout {
return true
}
if statusCode/100 == 5 {
return true
}
if statusCode == http.StatusBadRequest {
return false
}
if statusCode/100 == 2 {
return false
}
return true
return false
}

func RelayNotImplemented(c *gin.Context) {
Expand Down
2 changes: 0 additions & 2 deletions service/aiproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
_ "github.com/joho/godotenv/autoload"
"github.com/labring/sealos/service/aiproxy/common"
"github.com/labring/sealos/service/aiproxy/common/balance"
"github.com/labring/sealos/service/aiproxy/common/client"
"github.com/labring/sealos/service/aiproxy/common/config"
"github.com/labring/sealos/service/aiproxy/middleware"
"github.com/labring/sealos/service/aiproxy/model"
Expand All @@ -43,7 +42,6 @@ func initializeServices() error {
return err
}

client.Init()
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions service/aiproxy/relay/adaptor/baidu/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"

json "github.com/json-iterator/go"
"github.com/labring/sealos/service/aiproxy/common/client"
"github.com/labring/sealos/service/aiproxy/relay/utils"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -66,7 +66,7 @@ func getBaiduAccessTokenHelper(ctx context.Context, apiKey string) (*AccessToken
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
res, err := client.ImpatientHTTPClient.Do(req)
res, err := utils.DoRequest(req)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions service/aiproxy/relay/adaptor/openai/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

json "github.com/json-iterator/go"
"github.com/labring/sealos/service/aiproxy/common/client"
"github.com/labring/sealos/service/aiproxy/model"
"github.com/labring/sealos/service/aiproxy/relay/adaptor"
)
Expand All @@ -29,7 +28,7 @@ func GetBalance(channel *model.Channel) (float64, error) {
return 0, err
}
req1.Header.Set("Authorization", "Bearer "+channel.Key)
res1, err := client.HTTPClient.Do(req1)
res1, err := http.DefaultClient.Do(req1)
if err != nil {
return 0, err
}
Expand All @@ -51,7 +50,7 @@ func GetBalance(channel *model.Channel) (float64, error) {
return 0, err
}
req2.Header.Set("Authorization", "Bearer "+channel.Key)
res2, err := client.HTTPClient.Do(req2)
res2, err := http.DefaultClient.Do(req2)
if err != nil {
return 0, err
}
Expand Down
3 changes: 1 addition & 2 deletions service/aiproxy/relay/adaptor/siliconflow/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"
"strconv"

"github.com/labring/sealos/service/aiproxy/common/client"
"github.com/labring/sealos/service/aiproxy/model"
"github.com/labring/sealos/service/aiproxy/relay/adaptor"
)
Expand All @@ -25,7 +24,7 @@ func (a *Adaptor) GetBalance(channel *model.Channel) (float64, error) {
return 0, err
}
req.Header.Set("Authorization", "Bearer "+channel.Key)
res, err := client.HTTPClient.Do(req)
res, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
Expand Down
4 changes: 4 additions & 0 deletions service/aiproxy/relay/controller/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"sync"
Expand Down Expand Up @@ -260,6 +261,9 @@ func DoHelper(a adaptor.Adaptor, c *gin.Context, meta *meta.Meta) (*relaymodel.U

resp, err := a.DoRequest(meta, c, req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, &detail, openai.ErrorWrapperWithMessage("do request failed: "+err.Error(), "do_request_failed", http.StatusGatewayTimeout)
}
return nil, &detail, openai.ErrorWrapperWithMessage("do request failed: "+err.Error(), "do_request_failed", http.StatusBadRequest)
}

Expand Down
3 changes: 1 addition & 2 deletions service/aiproxy/relay/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/labring/sealos/service/aiproxy/common"
"github.com/labring/sealos/service/aiproxy/common/client"
relaymodel "github.com/labring/sealos/service/aiproxy/relay/model"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ func UnmarshalMap(req *http.Request) (map[string]any, error) {
}

func DoRequest(req *http.Request) (*http.Response, error) {
resp, err := client.HTTPClient.Do(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 14166a2

Please sign in to comment.