view pkg/geoserver/reconf.go @ 5520:05db984d3db1

Improve performance of bottleneck area calculation Avoid buffer calculations by replacing them with simple distance comparisons and calculate the boundary of the result geometry only once per iteration. In some edge cases with very large numbers of iterations, this reduced the runtime of a bottleneck import by a factor of more than twenty.
author Tom Gottfried <tom@intevation.de>
date Thu, 21 Oct 2021 19:50:39 +0200
parents 5f47eeea988d
children
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2018 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>

package geoserver

import (
	"container/list"
	"net"
	"net/url"
	"sync"
	"time"

	"gemma.intevation.de/gemma/pkg/log"
	"gemma.intevation.de/gemma/pkg/models"
)

var (
	confQueue     = list.New()
	confQueueCond = sync.NewCond(new(sync.Mutex))
)

func init() {
	go asyncConfigure()
}

func asyncConfigure() {
	for {
		var fn func() error
		confQueueCond.L.Lock()
		for confQueue.Len() == 0 {
			confQueueCond.Wait()
		}
		fn = confQueue.Remove(confQueue.Front()).(func() error)
		confQueueCond.L.Unlock()
		if err := reconfigure(fn); err != nil {
			log.Warnf("configure GeoServer failed: %v\n", err)
		}
	}
}

func reconfigure(fn func() error) error {
	log.Infof("configure GeoServer...")
	const (
		maxTries = 10
		sleep    = time.Second * 5
	)
	var err error
	for try := 1; try <= maxTries; try++ {
		if err = fn(); err == nil {
			break
		}
		if try < maxTries {
			if uerr, ok := err.(*url.Error); ok {
				if oerr, ok := uerr.Err.(*net.OpError); ok && oerr.Op == "dial" {
					log.Warnf("failed attempt %d of %d to configure GeoServer. "+
						"Will try again in %s...\n", try, maxTries, sleep)
					time.Sleep(sleep)
					continue
				}
			}
		}
		break
	}
	return err
}

// Reconfigure adds a function to a queue of functions to update
// the GeoServer async. The functions are run again a number
// of times if network errors apppear indicating that the
// GeoServer is down.
func Reconfigure(fn func() error) {
	confQueueCond.L.Lock()
	defer confQueueCond.L.Unlock()
	confQueue.PushBack(fn)
	confQueueCond.Signal()
}

// ReconfigureStyle returns a function to update a style
// in the GeoServer to be in sync with the database.
func ReconfigureStyle(name string) {
	Reconfigure(func() error {
		var stls styles
		if err := stls.load(); err != nil {
			return err
		}

		entries := models.InternalServices.Filter(
			models.IntAnd(
				models.IntWMS,
				models.IntWithStyle,
				models.IntByName(name)))

		for i := range entries {
			entry := &entries[i]
			create := !stls.hasStyle(entry.Name)
			if err := updateStyle(entry, create); err != nil {
				return err
			}
		}

		return nil
	})
}