refactor: common optimizations
1. rename GetOrDefault to GetOrDefaultInt 2. refactor method with variadic parameter declaration.
This commit is contained in:
parent
d062bc60e4
commit
91260bff65
@ -91,12 +91,12 @@ var IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
|
|||||||
var requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL"))
|
var requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL"))
|
||||||
var RequestInterval = time.Duration(requestInterval) * time.Second
|
var RequestInterval = time.Duration(requestInterval) * time.Second
|
||||||
|
|
||||||
var SyncFrequency = GetOrDefault("SYNC_FREQUENCY", 10*60) // unit is second
|
var SyncFrequency = GetOrDefaultInt("SYNC_FREQUENCY", 10*60) // unit is second
|
||||||
|
|
||||||
var BatchUpdateEnabled = false
|
var BatchUpdateEnabled = false
|
||||||
var BatchUpdateInterval = GetOrDefault("BATCH_UPDATE_INTERVAL", 5)
|
var BatchUpdateInterval = GetOrDefaultInt("BATCH_UPDATE_INTERVAL", 5)
|
||||||
|
|
||||||
var RelayTimeout = GetOrDefault("RELAY_TIMEOUT", 0) // unit is second
|
var RelayTimeout = GetOrDefaultInt("RELAY_TIMEOUT", 0) // unit is second
|
||||||
|
|
||||||
var GeminiSafetySetting = GetOrDefaultString("GEMINI_SAFETY_SETTING", "BLOCK_NONE")
|
var GeminiSafetySetting = GetOrDefaultString("GEMINI_SAFETY_SETTING", "BLOCK_NONE")
|
||||||
|
|
||||||
@ -127,10 +127,10 @@ var (
|
|||||||
// All duration's unit is seconds
|
// All duration's unit is seconds
|
||||||
// Shouldn't larger then RateLimitKeyExpirationDuration
|
// Shouldn't larger then RateLimitKeyExpirationDuration
|
||||||
var (
|
var (
|
||||||
GlobalApiRateLimitNum = GetOrDefault("GLOBAL_API_RATE_LIMIT", 180)
|
GlobalApiRateLimitNum = GetOrDefaultInt("GLOBAL_API_RATE_LIMIT", 180)
|
||||||
GlobalApiRateLimitDuration int64 = 3 * 60
|
GlobalApiRateLimitDuration int64 = 3 * 60
|
||||||
|
|
||||||
GlobalWebRateLimitNum = GetOrDefault("GLOBAL_WEB_RATE_LIMIT", 60)
|
GlobalWebRateLimitNum = GetOrDefaultInt("GLOBAL_WEB_RATE_LIMIT", 60)
|
||||||
GlobalWebRateLimitDuration int64 = 3 * 60
|
GlobalWebRateLimitDuration int64 = 3 * 60
|
||||||
|
|
||||||
UploadRateLimitNum = 10
|
UploadRateLimitNum = 10
|
||||||
|
@ -8,7 +8,7 @@ func Password2Hash(password string) (string, error) {
|
|||||||
return string(hashedPassword), err
|
return string(hashedPassword), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func ValidatePasswordAndHash(password string, hash string) bool {
|
func ValidatePasswordAndHash(password, hash string) bool {
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
@ -4,4 +4,4 @@ var UsingSQLite = false
|
|||||||
var UsingPostgreSQL = false
|
var UsingPostgreSQL = false
|
||||||
|
|
||||||
var SQLitePath = "one-api.db"
|
var SQLitePath = "one-api.db"
|
||||||
var SQLiteBusyTimeout = GetOrDefault("SQLITE_BUSY_TIMEOUT", 3000)
|
var SQLiteBusyTimeout = GetOrDefaultInt("SQLITE_BUSY_TIMEOUT", 3000)
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SendEmail(subject string, receiver string, content string) error {
|
func SendEmail(subject, receiver, content string) error {
|
||||||
if SMTPFrom == "" { // for compatibility
|
if SMTPFrom == "" { // for compatibility
|
||||||
SMTPFrom = SMTPAccount
|
SMTPFrom = SMTPAccount
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ type embedFileSystem struct {
|
|||||||
http.FileSystem
|
http.FileSystem
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e embedFileSystem) Exists(prefix string, path string) bool {
|
func (e embedFileSystem) Exists(prefix, path string) bool {
|
||||||
_, err := e.Open(path)
|
_, err := e.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
|
@ -121,7 +121,7 @@ func UnescapeHTML(x string) interface{} {
|
|||||||
return template.HTML(x)
|
return template.HTML(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
func IntMax(a int, b int) int {
|
func IntMax(a, b int) int {
|
||||||
if a >= b {
|
if a >= b {
|
||||||
return a
|
return a
|
||||||
} else {
|
} else {
|
||||||
@ -176,7 +176,7 @@ func GetTimeString() string {
|
|||||||
return fmt.Sprintf("%s%d", now.Format("20060102150405"), now.UnixNano()%1e9)
|
return fmt.Sprintf("%s%d", now.Format("20060102150405"), now.UnixNano()%1e9)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Max(a int, b int) int {
|
func Max(a, b int) int {
|
||||||
if a >= b {
|
if a >= b {
|
||||||
return a
|
return a
|
||||||
} else {
|
} else {
|
||||||
@ -184,7 +184,7 @@ func Max(a int, b int) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOrDefault(env string, defaultValue int) int {
|
func GetOrDefaultInt(env string, defaultValue int) int {
|
||||||
if env == "" || os.Getenv(env) == "" {
|
if env == "" || os.Getenv(env) == "" {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
@ -196,14 +196,14 @@ func GetOrDefault(env string, defaultValue int) int {
|
|||||||
return num
|
return num
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOrDefaultString(env string, defaultValue string) string {
|
func GetOrDefaultString(env, defaultValue string) string {
|
||||||
if env == "" || os.Getenv(env) == "" {
|
if env == "" || os.Getenv(env) == "" {
|
||||||
return defaultValue
|
return defaultValue
|
||||||
}
|
}
|
||||||
return os.Getenv(env)
|
return os.Getenv(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessageWithRequestId(message string, id string) string {
|
func MessageWithRequestId(message, id string) string {
|
||||||
return fmt.Sprintf("%s (request id: %s)", message, id)
|
return fmt.Sprintf("%s (request id: %s)", message, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ func GenerateVerificationCode(length int) string {
|
|||||||
return code[:length]
|
return code[:length]
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterVerificationCodeWithKey(key string, code string, purpose string) {
|
func RegisterVerificationCodeWithKey(key, code, purpose string) {
|
||||||
verificationMutex.Lock()
|
verificationMutex.Lock()
|
||||||
defer verificationMutex.Unlock()
|
defer verificationMutex.Unlock()
|
||||||
verificationMap[purpose+key] = verificationValue{
|
verificationMap[purpose+key] = verificationValue{
|
||||||
@ -43,7 +43,7 @@ func RegisterVerificationCodeWithKey(key string, code string, purpose string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyCodeWithKey(key string, code string, purpose string) bool {
|
func VerifyCodeWithKey(key, code, purpose string) bool {
|
||||||
verificationMutex.Lock()
|
verificationMutex.Lock()
|
||||||
defer verificationMutex.Unlock()
|
defer verificationMutex.Unlock()
|
||||||
value, okay := verificationMap[purpose+key]
|
value, okay := verificationMap[purpose+key]
|
||||||
@ -54,7 +54,7 @@ func VerifyCodeWithKey(key string, code string, purpose string) bool {
|
|||||||
return code == value.code
|
return code == value.code
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteKey(key string, purpose string) {
|
func DeleteKey(key, purpose string) {
|
||||||
verificationMutex.Lock()
|
verificationMutex.Lock()
|
||||||
defer verificationMutex.Unlock()
|
defer verificationMutex.Unlock()
|
||||||
delete(verificationMap, purpose+key)
|
delete(verificationMap, purpose+key)
|
||||||
|
@ -77,9 +77,9 @@ func InitDB() (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sqlDB.SetMaxIdleConns(common.GetOrDefault("SQL_MAX_IDLE_CONNS", 100))
|
sqlDB.SetMaxIdleConns(common.GetOrDefaultInt("SQL_MAX_IDLE_CONNS", 100))
|
||||||
sqlDB.SetMaxOpenConns(common.GetOrDefault("SQL_MAX_OPEN_CONNS", 1000))
|
sqlDB.SetMaxOpenConns(common.GetOrDefaultInt("SQL_MAX_OPEN_CONNS", 1000))
|
||||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetOrDefault("SQL_MAX_LIFETIME", 60)))
|
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetOrDefaultInt("SQL_MAX_LIFETIME", 60)))
|
||||||
|
|
||||||
if !common.IsMasterNode {
|
if !common.IsMasterNode {
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user