Use db abstraction in webmapper.

This commit is contained in:
Sascha L. Teichmann 2022-02-27 21:31:55 +01:00
parent 4f7fedf0b9
commit ae230d5abf
2 changed files with 31 additions and 41 deletions

View File

@ -10,7 +10,6 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"strings"
"bitbucket.org/s_l_teichmann/mtsatellite/common" "bitbucket.org/s_l_teichmann/mtsatellite/common"
@ -107,6 +106,7 @@ func main() {
} }
if redisHost != "" { if redisHost != "" {
var colors *common.Colors var colors *common.Colors
var err error var err error
if colors, err = common.ParseColors(colorsFile); err != nil { if colors, err = common.ParseColors(colorsFile); err != nil {
@ -114,12 +114,8 @@ func main() {
} }
colors.TransparentDim = common.Clamp32f( colors.TransparentDim = common.Clamp32f(
float32(transparentDim/100.0), 0.0, 100.0) float32(transparentDim/100.0), 0.0, 100.0)
var redisAddress string
if strings.ContainsRune(redisHost, '/') { dbcc := common.CreateDBClientCreator(redisHost, redisPort)
redisAddress = redisHost
} else {
redisAddress = fmt.Sprintf("%s:%d", redisHost, redisPort)
}
var allowedUpdateIps []net.IP var allowedUpdateIps []net.IP
if allowedUpdateIps, err = ipsFromHosts(updateHosts); err != nil { if allowedUpdateIps, err = ipsFromHosts(updateHosts); err != nil {
@ -128,7 +124,7 @@ func main() {
tu := newTileUpdater( tu := newTileUpdater(
mapDir, mapDir,
redisAddress, dbcc,
allowedUpdateIps, allowedUpdateIps,
colors, bg, colors, bg,
yMin, yMax, yMin, yMax,

View File

@ -32,18 +32,18 @@ type baseTilesUpdates interface {
} }
type tileUpdater struct { type tileUpdater struct {
changes map[xz]struct{} changes map[xz]struct{}
btu baseTilesUpdates btu baseTilesUpdates
mapDir string mapDir string
redisAddress string dbcc common.DBClientCreator
ips []net.IP ips []net.IP
colors *common.Colors colors *common.Colors
bg color.RGBA bg color.RGBA
yMin, yMax int16 yMin, yMax int16
workers int workers int
transparent bool transparent bool
cond *sync.Cond cond *sync.Cond
mu sync.Mutex mu sync.Mutex
} }
type xz struct { type xz struct {
@ -78,7 +78,8 @@ func (c xz) parent() xzm {
} }
func newTileUpdater( func newTileUpdater(
mapDir, redisAddress string, mapDir string,
dbcc common.DBClientCreator,
ips []net.IP, ips []net.IP,
colors *common.Colors, colors *common.Colors,
bg color.RGBA, bg color.RGBA,
@ -88,17 +89,17 @@ func newTileUpdater(
btu baseTilesUpdates) *tileUpdater { btu baseTilesUpdates) *tileUpdater {
tu := tileUpdater{ tu := tileUpdater{
btu: btu, btu: btu,
mapDir: mapDir, mapDir: mapDir,
redisAddress: redisAddress, dbcc: dbcc,
ips: ips, ips: ips,
changes: map[xz]struct{}{}, changes: map[xz]struct{}{},
colors: colors, colors: colors,
bg: bg, bg: bg,
yMin: int16(yMin), yMin: int16(yMin),
yMax: int16(yMax), yMax: int16(yMax),
transparent: transparent, transparent: transparent,
workers: workers} workers: workers}
tu.cond = sync.NewCond(&tu.mu) tu.cond = sync.NewCond(&tu.mu)
return &tu return &tu
} }
@ -196,17 +197,10 @@ func (tu *tileUpdater) doUpdates() {
jobs := make(chan *xzc) jobs := make(chan *xzc)
var done sync.WaitGroup var done sync.WaitGroup
var proto string
if strings.ContainsRune(tu.redisAddress, '/') {
proto = "unix"
} else {
proto = "tcp"
}
for i, n := 0, common.Min(tu.workers, len(changes)); i < n; i++ { for i, n := 0, common.Min(tu.workers, len(changes)); i < n; i++ {
var client *common.RedisClient var client common.DBClient
var err error var err error
if client, err = common.NewRedisClient(proto, tu.redisAddress); err != nil { if client, err = tu.dbcc(); err != nil {
log.Printf("WARN: Cannot connect to redis server: %s\n", err) log.Printf("WARN: Cannot connect to redis server: %s\n", err)
continue continue
} }