diff --git a/api.go b/api.go index 44ce65e..3112ba3 100644 --- a/api.go +++ b/api.go @@ -1715,11 +1715,22 @@ func (app *appContext) ApplySettings(gc *gin.Context) { "policy": map[string]string{}, "homescreen": map[string]string{}, } + /* Jellyfin doesn't seem to like too many of these requests sent in succession + and can crash and mess up its database. Issue #160 says this occurs when more + than 100 users are modified. A delay totalling 500ms between requests is used + if so. */ + var shouldDelay bool = len(req.ApplyTo) >= 100 + if shouldDelay { + app.debug.Println("Adding delay between requests for large batch") + } for _, id := range req.ApplyTo { status, err := app.jf.SetPolicy(id, policy) if !(status == 200 || status == 204) || err != nil { errors["policy"][id] = fmt.Sprintf("%d: %s", status, err) } + if shouldDelay { + time.Sleep(250 * time.Millisecond) + } if req.Homescreen { status, err = app.jf.SetConfiguration(id, configuration) errorString := "" @@ -1735,6 +1746,9 @@ func (app *appContext) ApplySettings(gc *gin.Context) { errors["homescreen"][id] = errorString } } + if shouldDelay { + time.Sleep(250 * time.Millisecond) + } } code := 200 if len(errors["policy"]) == len(req.ApplyTo) || len(errors["homescreen"]) == len(req.ApplyTo) {