comparison pkg/geoserver/reconf.go @ 947:1417ae641f7f

Merged geo-style branch back into default.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 12 Oct 2018 09:36:46 +0200
parents 0a563fef64a9
children a244b18cb916
comparison
equal deleted inserted replaced
945:ab39679c508f 947:1417ae641f7f
1 package geoserver
2
3 import (
4 "container/list"
5 "log"
6 "net"
7 "net/url"
8 "sync"
9 "time"
10 )
11
12 var (
13 confQueue = list.New()
14 confQueueCond = sync.NewCond(new(sync.Mutex))
15 )
16
17 func init() {
18 go asyncConfigure()
19 }
20
21 func asyncConfigure() {
22 for {
23 var fn func() error
24 confQueueCond.L.Lock()
25 for confQueue.Len() == 0 {
26 confQueueCond.Wait()
27 }
28 fn = confQueue.Remove(confQueue.Front()).(func() error)
29 confQueueCond.L.Unlock()
30 if err := reconfigure(fn); err != nil {
31 log.Printf("warn: configure GeoServer failed: %v\n", err)
32 }
33 }
34 }
35
36 func reconfigure(fn func() error) error {
37 log.Println("Configure GeoServer...")
38 const (
39 maxTries = 10
40 sleep = time.Second * 5
41 )
42 var err error
43 for try := 1; try <= maxTries; try++ {
44 if err = fn(); err == nil {
45 break
46 }
47 if try < maxTries {
48 if uerr, ok := err.(*url.Error); ok {
49 if oerr, ok := uerr.Err.(*net.OpError); ok && oerr.Op == "dial" {
50 log.Printf("Failed attempt %d of %d to configure GeoServer. "+
51 "Will try again in %s...\n", try, maxTries, sleep)
52 time.Sleep(sleep)
53 continue
54 }
55 }
56 }
57 break
58 }
59 return err
60 }
61
62 func Reconfigure(fn func() error) {
63 confQueueCond.L.Lock()
64 defer confQueueCond.L.Unlock()
65 confQueue.PushBack(fn)
66 confQueueCond.Signal()
67 }