Skip to content

Commit

Permalink
Put global map access within mutexes to try to address #89
Browse files Browse the repository at this point in the history
  • Loading branch information
pramsey committed Apr 21, 2021
1 parent 2c26ff8 commit 9b94c47
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 186 deletions.
4 changes: 2 additions & 2 deletions assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h2>Table Layers</h2>
{{ range $key, $value := . }}
{{ if eq $value.Type "table" }}
<li>
<strong class="layer-name">{{ $value.Id }}</strong> (<a href='{{ $value.DetailUrl | replace ".json" ".html"}}'>preview</a> | <a href="{{ $value.DetailUrl }}">json</a>)
<strong class="layer-name">{{ $value.ID }}</strong> (<a href='{{ $value.DetailURL | replace ".json" ".html"}}'>preview</a> | <a href="{{ $value.DetailURL }}">json</a>)
{{ if $value.Description }}<br/><span class="layer-description">{{ $value.Description }}</span> {{ end }}
</li>
{{ end }}
Expand All @@ -35,7 +35,7 @@ <h2>Function Layers</h2>
{{ range $key, $value := . }}
{{ if eq $value.Type "function" }}
<li>
<strong class="layer-name">{{ $value.Id }}</strong> (<a href='{{$value.DetailUrl | replace ".json" ".html"}}'>preview</a> | <a href="{{ $value.DetailUrl }}">json</a>)
<strong class="layer-name">{{ $value.ID }}</strong> (<a href='{{$value.DetailURL | replace ".json" ".html"}}'>preview</a> | <a href="{{ $value.DetailURL }}">json</a>)
{{ if $value.Description }}<br/><span class="layer-description">{{ $value.Description }}</span> {{ end }}
</li>
{{ end }}
Expand Down
8 changes: 4 additions & 4 deletions assets/preview-function.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<meta charset='utf-8' />
<title>pg_tileserv: {{.Id}}</title>
<title>pg_tileserv: {{.ID}}</title>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mapbox-gl/1.13.0/mapbox-gl.js'></script>
<link href='https://cdnjs.cloudflare.com/ajax/libs/mapbox-gl/1.13.0/mapbox-gl.css' rel='stylesheet' />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Expand All @@ -15,7 +15,7 @@
<body>

<div id="meta">
<h1>{{ .Id }}</h1>
<h1>{{ .ID }}</h1>
{{ if .Description }}
<p>{{ .Description }}</p>
{{ end }}
Expand Down Expand Up @@ -44,9 +44,9 @@ <h1>{{ .Id }}</h1>
var map;
var mapcolor = "blue";

$.getJSON("{{ .Id }}.json", function(layer) {
$.getJSON("{{ .ID }}.json", function(layer) {
// A little info for explorers
console.log("{{ .Id }}.json");
console.log("{{ .ID }}.json");
console.log(layer);

layer["arguments"].forEach(function (item, i) {
Expand Down
8 changes: 4 additions & 4 deletions assets/preview-table.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<meta charset='utf-8' />
<title>pg_tileserv: {{.Id}}</title>
<title>pg_tileserv: {{.ID}}</title>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mapbox-gl/1.13.0/mapbox-gl.js'></script>
<link href='https://cdnjs.cloudflare.com/ajax/libs/mapbox-gl/1.13.0/mapbox-gl.css' rel='stylesheet' />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Expand All @@ -16,7 +16,7 @@
<body>

<div id="meta">
<h1>{{ .Id }}</h1>
<h1>{{ .ID }}</h1>
{{ if .Description }}
<p>{{ .Description }}</p>
{{ end }}
Expand All @@ -32,9 +32,9 @@ <h1>{{ .Id }}</h1>
var map;
var mapcolor = "blue";

$.getJSON("{{ .Id }}.json", function(layer) {
$.getJSON("{{ .ID }}.json", function(layer) {
// A little info for explorers
console.log("{{ .Id }}.json");
console.log("{{ .ID }}.json");
console.log(layer);

var mapConfig = {
Expand Down
16 changes: 8 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
log "github.com/sirupsen/logrus"
)

func DbConnect() (*pgxpool.Pool, error) {
func dbConnect() (*pgxpool.Pool, error) {
if globalDb == nil {
var err error
var config *pgxpool.Config
Expand Down Expand Up @@ -67,8 +67,8 @@ func DbConnect() (*pgxpool.Pool, error) {
return globalDb, nil
}

func LoadVersions() error {
db, err := DbConnect()
func loadVersions() error {
db, err := dbConnect()
if err != nil {
return err
}
Expand Down Expand Up @@ -105,13 +105,13 @@ func LoadVersions() error {
return nil
}

func DBTileRequest(ctx context.Context, tr *TileRequest) ([]byte, error) {
db, err := DbConnect()
func dBTileRequest(ctx context.Context, tr *TileRequest) ([]byte, error) {
db, err := dbConnect()
if err != nil {
log.Error(err)
return nil, err
}
row := db.QueryRow(ctx, tr.Sql, tr.Args...)
row := db.QueryRow(ctx, tr.SQL, tr.Args...)
var mvtTile []byte
err = row.Scan(&mvtTile)
if err != nil {
Expand All @@ -123,13 +123,13 @@ func DBTileRequest(ctx context.Context, tr *TileRequest) ([]byte, error) {
if pgconn.Timeout(err) {
return nil, tileAppError{
SrcErr: err,
Message: fmt.Sprintf("Timeout: deadline exceeded on %s/%s", tr.LayerId, tr.Tile.String()),
Message: fmt.Sprintf("Timeout: deadline exceeded on %s/%s", tr.LayerID, tr.Tile.String()),
}
}

return nil, tileAppError{
SrcErr: err,
Message: fmt.Sprintf("SQL error on %s/%s", tr.LayerId, tr.Tile.String()),
Message: fmt.Sprintf("SQL error on %s/%s", tr.LayerID, tr.Tile.String()),
}

}
Expand Down
66 changes: 33 additions & 33 deletions layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import (
"net/http"
)

type layerType int
// LayerType is the table/function type of a layer
type LayerType int

const (
layerTypeTable = 1
layerTypeFunction = 2
// LayerTypeTable is a table layer
LayerTypeTable = 1
// LayerTypeFunction is a function layer
LayerTypeFunction = 2
)

func (lt layerType) String() string {
func (lt LayerType) String() string {
switch lt {
case layerTypeTable:
case LayerTypeTable:
return "table"
case layerTypeFunction:
case LayerTypeFunction:
return "function"
default:
return "unknown"
Expand All @@ -30,80 +33,77 @@ func (lt layerType) String() string {
// a TileRequest containing SQL to produce tiles
// given an input tile
type Layer interface {
GetType() layerType
GetId() string
GetType() LayerType
GetID() string
GetDescription() string
GetName() string
GetSchema() string
GetTileRequest(tile Tile, r *http.Request) TileRequest
WriteLayerJson(w http.ResponseWriter, req *http.Request) error
WriteLayerJSON(w http.ResponseWriter, req *http.Request) error
}

type TileRequest struct {
LayerId string
LayerID string
Tile Tile
Sql string
SQL string
Args []interface{}
}

// A global array of Layer where the state is held for performance
// Refreshed when LoadLayerTableList is called
// Key is of the form: schemaname.tablename
var globalLayers map[string]Layer

func GetLayer(lyrId string) (Layer, error) {
if lyr, ok := globalLayers[lyrId]; ok {
func getLayer(lyrID string) (Layer, error) {
lyr, ok := globalLayers[lyrID]
if ok {
return lyr, nil
} else {
return lyr, errors.New(fmt.Sprintf("Unable to get layer '%s'", lyrId))
}
return lyr, errors.New(fmt.Sprintf("Unable to get layer '%s'", lyrID))
}

func LoadLayers() error {
func loadLayers() error {
_, errBnd := getServerBounds()
if errBnd != nil {
return errBnd
}
tableLayers, errTl := GetTableLayers()
tableLayers, errTl := getTableLayers()
if errTl != nil {
return errTl
}
functionLayers, errFl := GetFunctionLayers()
functionLayers, errFl := getFunctionLayers()
if errFl != nil {
return errFl
}
// Empty the global layer map
globalLayersMutex.Lock()
globalLayers = make(map[string]Layer)
for _, lyr := range tableLayers {
globalLayers[lyr.GetId()] = lyr
globalLayers[lyr.GetID()] = lyr
}
for _, lyr := range functionLayers {
globalLayers[lyr.GetId()] = lyr
globalLayers[lyr.GetID()] = lyr
}
globalLayersMutex.Unlock()

return nil
}

type LayerJson struct {
type layerJSON struct {
Type string `json:"type"`
Id string `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Schema string `json:"schema"`
Description string `json:"description"`
DetailUrl string `json:"detailurl"`
DetailURL string `json:"detailurl"`
}

func GetJsonLayers(r *http.Request) map[string]LayerJson {
json := make(map[string]LayerJson)
func getJSONLayers(r *http.Request) map[string]layerJSON {
json := make(map[string]layerJSON)
urlBase := serverURLBase(r)
for k, v := range globalLayers {
json[k] = LayerJson{
json[k] = layerJSON{
Type: v.GetType().String(),
Id: v.GetId(),
ID: v.GetID(),
Name: v.GetName(),
Schema: v.GetSchema(),
Description: v.GetDescription(),
DetailUrl: fmt.Sprintf("%s/%s.json", urlBase, v.GetId()),
DetailURL: fmt.Sprintf("%s/%s.json", urlBase, v.GetID()),
}
}
return json
Expand Down
Loading

0 comments on commit 9b94c47

Please sign in to comment.