From 6c340440a24b6b0b309c0ef3554297e758823f7d Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 11 Apr 2017 02:02:12 -0700 Subject: adding throttled content sorting --- system/db/content.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'system') diff --git a/system/db/content.go b/system/db/content.go index 49cba87..994860f 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -9,6 +9,7 @@ import ( "sort" "strconv" "strings" + "time" "github.com/ponzu-cms/ponzu/system/item" @@ -563,10 +564,54 @@ func Query(namespace string, opts QueryOptions) (int, [][]byte) { return total, posts } +var sortContentCalls = make(map[string]time.Time) +var waitDuration = time.Millisecond * 4000 + +func enoughTime(key string, withDelay bool) bool { + last, ok := sortContentCalls[key] + if !ok { + // no envocation yet + // track next evocation + sortContentCalls[key] = time.Now() + return true + } + + // if our required wait time has not been met, return false + if !time.Now().After(last.Add(waitDuration)) { + return false + } + + // dispatch a delayed envocation in case no additional one follows + if withDelay { + go func() { + select { + case <-time.After(waitDuration): + if enoughTime(key, false) { + // track next evocation + sortContentCalls[key] = time.Now() + SortContent(key) + } else { + // retrigger + SortContent(key) + } + } + }() + } + + // track next evocation + sortContentCalls[key] = time.Now() + return true +} + // SortContent sorts all content of the type supplied as the namespace by time, // in descending order, from most recent to least recent // Should be called from a goroutine after SetContent is successful func SortContent(namespace string) { + // wait if running too frequently per namespace + if !enoughTime(namespace, true) { + return + } + // only sort main content types i.e. Post if strings.Contains(namespace, "__") { return -- cgit v1.2.3 From af644c49edf6272880b998496f0f4080dbddbc00 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Tue, 11 Apr 2017 09:18:01 -0700 Subject: updating waitDuration, should determine if this is configurable or not --- system/db/content.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system') diff --git a/system/db/content.go b/system/db/content.go index 994860f..23e488f 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -565,13 +565,13 @@ func Query(namespace string, opts QueryOptions) (int, [][]byte) { } var sortContentCalls = make(map[string]time.Time) -var waitDuration = time.Millisecond * 4000 +var waitDuration = time.Millisecond * 2000 func enoughTime(key string, withDelay bool) bool { last, ok := sortContentCalls[key] if !ok { - // no envocation yet - // track next evocation + // no invocation yet + // track next invocation sortContentCalls[key] = time.Now() return true } @@ -587,7 +587,7 @@ func enoughTime(key string, withDelay bool) bool { select { case <-time.After(waitDuration): if enoughTime(key, false) { - // track next evocation + // track next invocation sortContentCalls[key] = time.Now() SortContent(key) } else { @@ -598,7 +598,7 @@ func enoughTime(key string, withDelay bool) bool { }() } - // track next evocation + // track next invocation sortContentCalls[key] = time.Now() return true } -- cgit v1.2.3