feat: 配置优化

This commit is contained in:
tbphp
2025-07-03 17:53:56 +08:00
parent 80662af9de
commit 0bd6a9289c
6 changed files with 77 additions and 82 deletions

View File

@@ -2,25 +2,29 @@ package channel
import (
"fmt"
"gpt-load/internal/config"
"gpt-load/internal/models"
"net/http"
"net/url"
"time"
"gorm.io/datatypes"
)
// GetChannel returns a channel proxy based on the group's channel type.
func GetChannel(group *models.Group) (ChannelProxy, error) {
switch group.ChannelType {
case "openai":
return NewOpenAIChannel(group.Upstreams)
return NewOpenAIChannel(group.Upstreams, group.Config)
case "gemini":
return NewGeminiChannel(group.Upstreams)
return NewGeminiChannel(group.Upstreams, group.Config)
default:
return nil, fmt.Errorf("unsupported channel type: %s", group.ChannelType)
}
}
// newBaseChannelWithUpstreams is a helper function to create and configure a BaseChannel.
func newBaseChannelWithUpstreams(name string, upstreams []string) (BaseChannel, error) {
func newBaseChannelWithUpstreams(name string, upstreams []string, groupConfig datatypes.JSONMap) (BaseChannel, error) {
if len(upstreams) == 0 {
return BaseChannel{}, fmt.Errorf("at least one upstream is required for %s channel", name)
}
@@ -34,9 +38,21 @@ func newBaseChannelWithUpstreams(name string, upstreams []string) (BaseChannel,
upstreamURLs = append(upstreamURLs, u)
}
// Get effective settings by merging system and group configs
settingsManager := config.GetSystemSettingsManager()
effectiveSettings := settingsManager.GetEffectiveConfig(groupConfig)
// Configure the HTTP client with the effective timeouts
httpClient := &http.Client{
Transport: &http.Transport{
IdleConnTimeout: time.Duration(effectiveSettings.IdleConnTimeout) * time.Second,
},
Timeout: time.Duration(effectiveSettings.RequestTimeout) * time.Second,
}
return BaseChannel{
Name: name,
Upstreams: upstreamURLs,
HTTPClient: &http.Client{},
HTTPClient: httpClient,
}, nil
}

View File

@@ -5,14 +5,15 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/datatypes"
)
type GeminiChannel struct {
BaseChannel
}
func NewGeminiChannel(upstreams []string) (*GeminiChannel, error) {
base, err := newBaseChannelWithUpstreams("gemini", upstreams)
func NewGeminiChannel(upstreams []string, config datatypes.JSONMap) (*GeminiChannel, error) {
base, err := newBaseChannelWithUpstreams("gemini", upstreams, config)
if err != nil {
return nil, err
}

View File

@@ -5,14 +5,15 @@ import (
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/datatypes"
)
type OpenAIChannel struct {
BaseChannel
}
func NewOpenAIChannel(upstreams []string) (*OpenAIChannel, error) {
base, err := newBaseChannelWithUpstreams("openai", upstreams)
func NewOpenAIChannel(upstreams []string, config datatypes.JSONMap) (*OpenAIChannel, error) {
base, err := newBaseChannelWithUpstreams("openai", upstreams, config)
if err != nil {
return nil, err
}