mirror of
https://github.com/hrfee/jfa-go.git
synced 2026-03-18 21:50:33 +01:00
config: add user cache async/sync timeout options
previously were constants in usercache.go, now app.userCache is instantiated in main.go with NewUserCache(time.Duration, time.Duration).
This commit is contained in:
@@ -188,6 +188,10 @@ func (app *appContext) loadConfig() error {
|
|||||||
app.config.Section("jellyfin").Key("device").SetValue("jfa-go")
|
app.config.Section("jellyfin").Key("device").SetValue("jfa-go")
|
||||||
app.config.Section("jellyfin").Key("device_id").SetValue(fmt.Sprintf("jfa-go-%s-%s", version, commit))
|
app.config.Section("jellyfin").Key("device_id").SetValue(fmt.Sprintf("jfa-go-%s-%s", version, commit))
|
||||||
|
|
||||||
|
app.MustSetValue("jellyfin", "cache_timeout", "30")
|
||||||
|
app.MustSetValue("jellyfin", "web_cache_async_timeout", "1")
|
||||||
|
app.MustSetValue("jellyfin", "web_cache_sync_timeout", "10")
|
||||||
|
|
||||||
LOGIP = app.config.Section("advanced").Key("log_ips").MustBool(false)
|
LOGIP = app.config.Section("advanced").Key("log_ips").MustBool(false)
|
||||||
LOGIPU = app.config.Section("advanced").Key("log_ips_users").MustBool(false)
|
LOGIPU = app.config.Section("advanced").Key("log_ips_users").MustBool(false)
|
||||||
|
|
||||||
|
|||||||
9
main.go
9
main.go
@@ -134,7 +134,7 @@ type appContext struct {
|
|||||||
pwrCaptchas map[string]Captcha
|
pwrCaptchas map[string]Captcha
|
||||||
ConfirmationKeys map[string]map[string]ConfirmationKey // Map of invite code to jwt to request
|
ConfirmationKeys map[string]map[string]ConfirmationKey // Map of invite code to jwt to request
|
||||||
confirmationKeysLock sync.Mutex
|
confirmationKeysLock sync.Mutex
|
||||||
userCache UserCache
|
userCache *UserCache
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateSecret(length int) (string, error) {
|
func generateSecret(length int) (string, error) {
|
||||||
@@ -406,7 +406,7 @@ func start(asDaemon, firstCall bool) {
|
|||||||
|
|
||||||
// Initialize jellyfin/emby connection
|
// Initialize jellyfin/emby connection
|
||||||
server := app.config.Section("jellyfin").Key("server").String()
|
server := app.config.Section("jellyfin").Key("server").String()
|
||||||
cacheTimeout := int(app.config.Section("jellyfin").Key("cache_timeout").MustUint(30))
|
cacheTimeout := app.config.Section("jellyfin").Key("cache_timeout").MustInt()
|
||||||
stringServerType := app.config.Section("jellyfin").Key("type").String()
|
stringServerType := app.config.Section("jellyfin").Key("type").String()
|
||||||
timeoutHandler := mediabrowser.NewNamedTimeoutHandler("Jellyfin", "\""+server+"\"", true)
|
timeoutHandler := mediabrowser.NewNamedTimeoutHandler("Jellyfin", "\""+server+"\"", true)
|
||||||
if stringServerType == "emby" {
|
if stringServerType == "emby" {
|
||||||
@@ -469,6 +469,11 @@ func start(asDaemon, firstCall bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
app.userCache = NewUserCache(
|
||||||
|
time.Minute*time.Duration(app.config.Section("jellyfin").Key("web_cache_async_timeout").MustInt()),
|
||||||
|
time.Minute*time.Duration(app.config.Section("jellyfin").Key("web_cache_sync_timeout").MustInt()),
|
||||||
|
)
|
||||||
|
|
||||||
// Since email depends on language, the email reload in loadConfig won't work first time.
|
// Since email depends on language, the email reload in loadConfig won't work first time.
|
||||||
// Email also handles its own proxying, as (SMTP atleast) doesn't use a HTTP transport.
|
// Email also handles its own proxying, as (SMTP atleast) doesn't use a HTTP transport.
|
||||||
app.email = NewEmailer(app)
|
app.email = NewEmailer(app)
|
||||||
|
|||||||
35
usercache.go
35
usercache.go
@@ -10,12 +10,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// After cache is this old, re-sync, but do it in the background and return the old cache.
|
USER_DEFAULT_SORT_FIELD = "name"
|
||||||
WEB_USER_CACHE_SYNC = 30 * time.Second
|
USER_DEFAULT_SORT_ASCENDING = true
|
||||||
// After cache is this old, re-sync and wait for it and return the new cache.
|
|
||||||
WEB_USER_CACHE_WAIT_FOR_SYNC = 5 * time.Minute
|
|
||||||
USER_DEFAULT_SORT_FIELD = "name"
|
|
||||||
USER_DEFAULT_SORT_ASCENDING = true
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UserCache caches the transport representation of users,
|
// UserCache caches the transport representation of users,
|
||||||
@@ -28,19 +24,30 @@ type UserCache struct {
|
|||||||
Ref []*respUser
|
Ref []*respUser
|
||||||
Sorted bool
|
Sorted bool
|
||||||
LastSync time.Time
|
LastSync time.Time
|
||||||
SyncLock sync.Mutex
|
// After cache is this old, re-sync, but do it in the background and return the old cache.
|
||||||
Syncing bool
|
SyncTimeout time.Duration
|
||||||
SortLock sync.Mutex
|
// After cache is this old, re-sync and wait for it and return the new cache.
|
||||||
Sorting bool
|
WaitForSyncTimeout time.Duration
|
||||||
|
SyncLock sync.Mutex
|
||||||
|
Syncing bool
|
||||||
|
SortLock sync.Mutex
|
||||||
|
Sorting bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewUserCache(syncTimeout, waitForSyncTimeout time.Duration) *UserCache {
|
||||||
|
return &UserCache{
|
||||||
|
SyncTimeout: syncTimeout,
|
||||||
|
WaitForSyncTimeout: waitForSyncTimeout,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaybeSync (maybe) syncs the cache, resulting in updated UserCache.Cache/.Ref/.Sorted.
|
// MaybeSync (maybe) syncs the cache, resulting in updated UserCache.Cache/.Ref/.Sorted.
|
||||||
// Only syncs if WEB_USER_CACHE_SYNC duration has passed since last one.
|
// Only syncs if c.SyncTimeout duration has passed since last one.
|
||||||
// If WEB_USER_CACHE_WAIT_FOR_SYNC duration has passed, this will block until a sync is complete, otherwise it will sync in the background
|
// If c.WaitForSyncTimeout duration has passed, this will block until a sync is complete, otherwise it will sync in the background
|
||||||
// (expecting you to use the old cache data). Only one sync will run at a time.
|
// (expecting you to use the old cache data). Only one sync will run at a time.
|
||||||
func (c *UserCache) MaybeSync(app *appContext) error {
|
func (c *UserCache) MaybeSync(app *appContext) error {
|
||||||
shouldWaitForSync := time.Now().After(c.LastSync.Add(WEB_USER_CACHE_WAIT_FOR_SYNC)) || c.Ref == nil || len(c.Ref) == 0
|
shouldWaitForSync := time.Now().After(c.LastSync.Add(c.WaitForSyncTimeout)) || c.Ref == nil || len(c.Ref) == 0
|
||||||
shouldSync := time.Now().After(c.LastSync.Add(WEB_USER_CACHE_SYNC))
|
shouldSync := time.Now().After(c.LastSync.Add(c.SyncTimeout))
|
||||||
|
|
||||||
if !shouldSync {
|
if !shouldSync {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user