32 lines
655 B
Go
32 lines
655 B
Go
package webhookutil
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
const (
|
|
TimeoutConfigName = "WEBHOOK_TIMEOUT"
|
|
DefaultTimeout = 5 * time.Second
|
|
)
|
|
|
|
func Timeout(ctx context.Context, db *gorm.DB) time.Duration {
|
|
if db == nil {
|
|
return DefaultTimeout
|
|
}
|
|
var cfg model.InstallationConfig
|
|
if err := db.WithContext(ctx).Where("name = ?", TimeoutConfigName).First(&cfg).Error; err != nil {
|
|
return DefaultTimeout
|
|
}
|
|
seconds, err := strconv.Atoi(strings.TrimSpace(cfg.Value))
|
|
if err != nil || seconds <= 0 {
|
|
return DefaultTimeout
|
|
}
|
|
return time.Duration(seconds) * time.Second
|
|
}
|