feat: throttle variable for imports

pull/20/head
Gabe Farrell 6 months ago
parent 20cc4ce27a
commit f7621d0383

@ -36,6 +36,7 @@ const (
SKIP_IMPORT_ENV = "KOITO_SKIP_IMPORT" SKIP_IMPORT_ENV = "KOITO_SKIP_IMPORT"
ALLOWED_HOSTS_ENV = "KOITO_ALLOWED_HOSTS" ALLOWED_HOSTS_ENV = "KOITO_ALLOWED_HOSTS"
DISABLE_RATE_LIMIT_ENV = "KOITO_DISABLE_RATE_LIMIT" DISABLE_RATE_LIMIT_ENV = "KOITO_DISABLE_RATE_LIMIT"
THROTTLE_IMPORTS_MS = "KOITO_THROTTLE_IMPORTS_MS"
) )
type config struct { type config struct {
@ -60,6 +61,7 @@ type config struct {
allowedHosts []string allowedHosts []string
allowAllHosts bool allowAllHosts bool
disableRateLimit bool disableRateLimit bool
importThrottleMs int
} }
var ( var (
@ -104,6 +106,8 @@ func loadConfig(getenv func(string) string) (*config, error) {
cfg.lbzRelayUrl = getenv(LBZ_RELAY_URL_ENV) cfg.lbzRelayUrl = getenv(LBZ_RELAY_URL_ENV)
} }
cfg.importThrottleMs, _ = strconv.Atoi(getenv(THROTTLE_IMPORTS_MS))
cfg.disableRateLimit = parseBool(getenv(DISABLE_RATE_LIMIT_ENV)) cfg.disableRateLimit = parseBool(getenv(DISABLE_RATE_LIMIT_ENV))
cfg.structuredLogging = parseBool(getenv(ENABLE_STRUCTURED_LOGGING_ENV)) cfg.structuredLogging = parseBool(getenv(ENABLE_STRUCTURED_LOGGING_ENV))
@ -278,3 +282,9 @@ func RateLimitDisabled() bool {
defer lock.RUnlock() defer lock.RUnlock()
return globalConfig.disableRateLimit return globalConfig.disableRateLimit
} }
func ThrottleImportMs() int {
lock.RLock()
defer lock.RUnlock()
return globalConfig.importThrottleMs
}

@ -39,6 +39,12 @@ func ImportMalojaFile(ctx context.Context, store db.DB, filename string) error {
l.Err(err).Msgf("Failed to read import file: %s", filename) l.Err(err).Msgf("Failed to read import file: %s", filename)
return err return err
} }
var throttleFunc = func() {}
if ms := cfg.ThrottleImportMs(); ms > 0 {
throttleFunc = func() {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
}
export := new(MalojaExport) export := new(MalojaExport)
err = json.NewDecoder(file).Decode(&export) err = json.NewDecoder(file).Decode(&export)
if err != nil { if err != nil {
@ -73,6 +79,7 @@ func ImportMalojaFile(ctx context.Context, store db.DB, filename string) error {
l.Err(err).Msg("Failed to import maloja playback item") l.Err(err).Msg("Failed to import maloja playback item")
return err return err
} }
throttleFunc()
} }
_, err = os.Stat(path.Join(cfg.ConfigDir(), "import_complete")) _, err = os.Stat(path.Join(cfg.ConfigDir(), "import_complete"))
if err != nil { if err != nil {

@ -31,6 +31,12 @@ func ImportSpotifyFile(ctx context.Context, store db.DB, filename string) error
l.Err(err).Msgf("Failed to read import file: %s", filename) l.Err(err).Msgf("Failed to read import file: %s", filename)
return err return err
} }
var throttleFunc = func() {}
if ms := cfg.ThrottleImportMs(); ms > 0 {
throttleFunc = func() {
time.Sleep(time.Duration(ms) * time.Millisecond)
}
}
export := make([]SpotifyExportItem, 0) export := make([]SpotifyExportItem, 0)
err = json.NewDecoder(file).Decode(&export) err = json.NewDecoder(file).Decode(&export)
if err != nil { if err != nil {
@ -59,6 +65,7 @@ func ImportSpotifyFile(ctx context.Context, store db.DB, filename string) error
l.Err(err).Msg("Failed to import spotify playback item") l.Err(err).Msg("Failed to import spotify playback item")
return err return err
} }
throttleFunc()
} }
_, err = os.Stat(path.Join(cfg.ConfigDir(), "import_complete")) _, err = os.Stat(path.Join(cfg.ConfigDir(), "import_complete"))
if err != nil { if err != nil {

Loading…
Cancel
Save