-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a HTTP cache for remote resources.
Fixes #12502
- Loading branch information
Showing
19 changed files
with
579 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright 2024 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tasks | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// RunEvery runs a function at a regular interval. | ||
// Functions can be added and removed while running. | ||
type RunEvery struct { | ||
// The shortest interval between each run. | ||
IntervalLow time.Duration | ||
|
||
// The longest interval between each run. | ||
IntervalHigh time.Duration | ||
|
||
// Any error returned from the function will be passed to this function. | ||
HandleError func(string, error) | ||
|
||
// If set, the function will be run immediately. | ||
RunImmediately bool | ||
|
||
// The named functions to run. | ||
funcs map[string]*fn | ||
|
||
mu sync.Mutex | ||
started bool | ||
closed bool | ||
quit chan struct{} | ||
} | ||
|
||
type fn struct { | ||
interval time.Duration | ||
last time.Time | ||
f func(interval time.Duration) (time.Duration, error) | ||
} | ||
|
||
func (r *RunEvery) Start() error { | ||
if r.started { | ||
return nil | ||
} | ||
if r.IntervalLow == 0 { | ||
r.IntervalLow = 1 * time.Second | ||
} | ||
|
||
if r.IntervalHigh < r.IntervalLow { | ||
r.IntervalHigh = r.IntervalLow * 30 | ||
} | ||
|
||
r.started = true | ||
r.quit = make(chan struct{}) | ||
|
||
go func() { | ||
if r.RunImmediately { | ||
r.run() | ||
} | ||
ticker := time.NewTicker(500 * time.Millisecond) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-r.quit: | ||
return | ||
case <-ticker.C: | ||
r.run() | ||
} | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
// Close stops the RunEvery from running. | ||
func (r *RunEvery) Close() error { | ||
if r.closed { | ||
return nil | ||
} | ||
r.closed = true | ||
if r.quit != nil { | ||
close(r.quit) | ||
} | ||
return nil | ||
} | ||
|
||
// Add adds a function to the RunEvery. | ||
func (r *RunEvery) Add(name string, f func(time.Duration) (time.Duration, error)) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
if r.funcs == nil { | ||
r.funcs = make(map[string]*fn) | ||
} | ||
start := r.IntervalHigh / 3 | ||
if start < r.IntervalLow { | ||
start = r.IntervalLow | ||
} | ||
r.funcs[name] = &fn{f: f, interval: start, last: time.Now()} | ||
} | ||
|
||
// Remove removes a function from the RunEvery. | ||
func (r *RunEvery) Remove(name string) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
delete(r.funcs, name) | ||
} | ||
|
||
// Has returns whether the RunEvery has a function with the given name. | ||
func (r *RunEvery) Has(name string) bool { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
_, found := r.funcs[name] | ||
return found | ||
} | ||
|
||
func (r *RunEvery) run() { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
for name, f := range r.funcs { | ||
if time.Now().Before(f.last.Add(f.interval)) { | ||
continue | ||
} | ||
f.last = time.Now() | ||
interval, err := f.f(f.interval) | ||
if err != nil && r.HandleError != nil { | ||
r.HandleError(name, err) | ||
} | ||
|
||
if interval < r.IntervalLow { | ||
interval = r.IntervalLow | ||
} | ||
|
||
if interval > r.IntervalHigh { | ||
interval = r.IntervalHigh | ||
} | ||
|
||
f.interval = interval | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2024 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
import "sync" | ||
|
||
type Closer interface { | ||
Close() error | ||
} | ||
|
||
type CloseAdder interface { | ||
Add(Closer) | ||
} | ||
|
||
type Closers struct { | ||
mu sync.Mutex | ||
cs []Closer | ||
} | ||
|
||
func (cs *Closers) Add(c Closer) { | ||
cs.mu.Lock() | ||
defer cs.mu.Unlock() | ||
cs.cs = append(cs.cs, c) | ||
} | ||
|
||
func (cs *Closers) Close() error { | ||
cs.mu.Lock() | ||
defer cs.mu.Unlock() | ||
for _, c := range cs.cs { | ||
c.Close() | ||
} | ||
|
||
cs.cs = cs.cs[:0] | ||
|
||
return nil | ||
} |
Oops, something went wrong.