mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-10 02:23:43 +08:00
fix: Refactor relay/channel, upgrade deps, improve request handling and error messages.
* Updated relay/channel/gemini package to use gin-gonic/gin for routing * Added timeouts, environment variable for proxy, and error handling for HTTP clients in relay/util/init.go * Improved error handling, URL path cases, and channel selection logic in middleware/distributor.go * Added Content-Type header, closed request bodies, and added context to requests in relay/channel/common.go * Upgraded various dependencies in go.mod * Modified the GetRequestURL method in relay/channel/gemini/adaptor.go to include a "key" parameter in the URL and set a default version of "v1beta" * Added io and net/http packages in relay/channel/gemini/adaptor.go and relay/channel/gemini/main.go * Added a struct for GeminiStreamResp and modified response handling in relay/channel/gemini/main.go * Imported packages for io and net/http added, gin-gonic/gin package added, and error handling improved in relay/channel/gemini/main.go
This commit is contained in:
@@ -1,33 +1,36 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
gutils "github.com/Laisky/go-utils/v4"
|
||||
"github.com/songquanpeng/one-api/common/config"
|
||||
)
|
||||
|
||||
var HTTPClient *http.Client
|
||||
var ImpatientHTTPClient *http.Client
|
||||
|
||||
func init() {
|
||||
var opts []gutils.HTTPClientOptFunc
|
||||
|
||||
tp := &http.Transport{
|
||||
IdleConnTimeout: time.Duration(config.IdleTimeout) * time.Second,
|
||||
timeout := time.Duration(max(config.IdleTimeout, 30)) * time.Second
|
||||
opts = append(opts, gutils.WithHTTPClientTimeout(timeout))
|
||||
if os.Getenv("RELAY_PROXY") != "" {
|
||||
opts = append(opts, gutils.WithHTTPClientProxy(os.Getenv("RELAY_PROXY")))
|
||||
}
|
||||
|
||||
if config.RelayTimeout == 0 {
|
||||
HTTPClient = &http.Client{
|
||||
Transport: tp,
|
||||
}
|
||||
} else {
|
||||
HTTPClient = &http.Client{
|
||||
Transport: tp,
|
||||
Timeout: time.Duration(config.RelayTimeout) * time.Second,
|
||||
}
|
||||
var err error
|
||||
HTTPClient, err = gutils.NewHTTPClient(opts...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ImpatientHTTPClient = &http.Client{
|
||||
Transport: tp,
|
||||
Timeout: 5 * time.Second,
|
||||
ImpatientHTTPClient, err = gutils.NewHTTPClient(
|
||||
gutils.WithHTTPClientTimeout(5 * time.Second),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user