Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mutex for directory update. #228

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import (
"runtime"
"time"

"github.com/m-lab/go/prometheusx"

"github.com/m-lab/annotation-service/handler"
"github.com/m-lab/annotation-service/manager"
"github.com/m-lab/go/memoryless"
"github.com/m-lab/go/prometheusx"
)

var (
updateInterval = flag.Duration("update_interval", time.Duration(24)*time.Hour, "Run the update dataset job with this frequency.")
minInterval = flag.Duration("min_interval", time.Duration(23)*time.Hour, "minimum gap between 2 runs.")
maxInterval = flag.Duration("max_interval", time.Duration(25)*time.Hour, "maximum gap between 2 runs.")
minInterval = flag.Duration("min_interval", time.Duration(18)*time.Hour, "minimum gap between 2 runs.")
maxInterval = flag.Duration("max_interval", time.Duration(26)*time.Hour, "maximum gap between 2 runs.")
// Create a single unified context and a cancellationMethod for said context.
ctx, cancelCtx = context.WithCancel(context.Background())
)
Expand Down Expand Up @@ -66,6 +65,7 @@ func main() {
runtime.SetMutexProfileFraction(1000)

log.Print("Beginning Setup\n")
prometheusx.MustStartPrometheus(":9090")

go memoryless.Run(ctx, manager.MustUpdateDirectory,
memoryless.Config{Expected: *updateInterval, Min: *minInterval, Max: *maxInterval})
Expand All @@ -74,7 +74,6 @@ func main() {
http.HandleFunc("/updateDatasets", updateMaxmindDatasets)

handler.InitHandler()
prometheusx.MustStartPrometheus(":9090")
log.Print("Listening on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
7 changes: 7 additions & 0 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func MustUpdateDirectory() {
// listBuilder wraps a set of CachingLoaders, and creates a set of merged Annotators on request.
// TODO - unit tests?
type listBuilder struct {
mutex sync.Mutex // Prevents concurrent update and/or build
legacyV4 api.CachingLoader // loader for legacy v4 annotators
legacyV6 api.CachingLoader // loader for legacy v6 annotators
geolite2 api.CachingLoader // loader for geolite2 annotators
Expand All @@ -119,6 +120,9 @@ func newListBuilder(v4, v6, g2 api.CachingLoader) *listBuilder {

// Update updates the (dynamic) CachingLoaders
func (bldr *listBuilder) update() error {
bldr.mutex.Lock()
defer bldr.mutex.Unlock()

var errV4, errV6, errG2 error

wg := sync.WaitGroup{}
Expand Down Expand Up @@ -152,6 +156,9 @@ func (bldr *listBuilder) update() error {
// build creates a complete list of CompositeAnnotators from the cached annotators
// from the CachingLoaders.
func (bldr *listBuilder) build() []api.Annotator {
bldr.mutex.Lock()
defer bldr.mutex.Unlock()

v4 := directory.SortSlice(bldr.legacyV4.Fetch())
v6 := directory.SortSlice(bldr.legacyV6.Fetch())

Expand Down