Skip to content

Commit fab4199

Browse files
committed
添加限速参数timedelay
1 parent 0295045 commit fab4199

File tree

3 files changed

+19
-121
lines changed

3 files changed

+19
-121
lines changed

conf.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
directory ="output"
99
[task]
1010
#number of fetchers
11-
workers = 64
11+
workers = 4
1212
#number of savers
13-
savepipe = 4
13+
savepipe = 1
14+
#min request interval, a speed limit, unit millisecond
15+
timedelay = 100
1416

1517
[tm]
1618
#name for mbtiles
@@ -25,9 +27,10 @@
2527
#the vector tiles metadata tilejson
2628
# json = ""
2729
#url is the schema url of tiles
28-
# url= "https://api.mapbox.com/v4/mapbox.mapbox-streets-v8/{z}/{x}/{y}.vector.pbf?sku=1016gFniej06a&access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA"
29-
# url= "https://api.mapbox.com/v4/mapbox.mapbox-terrain-v2/{z}/{x}/{y}.vector.pbf?sku=1016gFniej06a&access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA"
30-
url = "http://mt0.google.com/vt/lyrs=s&x={x}&y={y}&z={z}"
30+
# url = "https://api.mapbox.com/v4/mapbox.mapbox-streets-v8/{z}/{x}/{y}.vector.pbf?sku=1016gFniej06a&access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA"
31+
# url = "https://api.mapbox.com/v4/mapbox.mapbox-terrain-v2/{z}/{x}/{y}.vector.pbf?sku=1016gFniej06a&access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4M29iazA2Z2gycXA4N2pmbDZmangifQ.-g_vE53SD2WrJ6tFX7QHmA"
32+
url = "https://api.maptiler.com/tiles/v3/{z}/{x}/{y}.pbf?key=KDhMfHvorAFkFe64wlZb"
33+
# url = "http://mt0.google.com/vt/lyrs=s&x={x}&y={y}&z={z}"
3134
#lrs can set diff boundaries for diff levels
3235
[[lrs]]
3336
min = 0

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ func initConf(cfgFile string) {
6161
viper.SetDefault("output.format", "mbtiles")
6262
viper.SetDefault("output.directory", "output")
6363
viper.SetDefault("task.workers", 4)
64-
viper.SetDefault("task.savepipe", 8)
64+
viper.SetDefault("task.savepipe", 1)
65+
viper.SetDefault("task.timedelay", 0)
6566
}
6667

6768
func main() {

task.go

Lines changed: 9 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"compress/gzip"
66
"database/sql"
7-
"encoding/json"
87
"fmt"
98
"io/ioutil"
109
"net/http"
@@ -16,8 +15,6 @@ import (
1615
"time"
1716

1817
"github.com/paulmach/orb"
19-
"github.com/paulmach/orb/clip"
20-
"github.com/paulmach/orb/geojson"
2118
"github.com/paulmach/orb/maptile"
2219
"github.com/paulmach/orb/maptile/tilecover"
2320
log "github.com/sirupsen/logrus"
@@ -45,8 +42,8 @@ type Task struct {
4542
db *sql.DB
4643
workerCount int
4744
savePipeSize int
45+
timeDelay int
4846
bufSize int
49-
layerWG sync.WaitGroup
5047
tileWG sync.WaitGroup
5148
abort, pause, play chan struct{}
5249
workers chan maptile.Tile
@@ -85,6 +82,7 @@ func NewTask(layers []Layer, m TileMap) *Task {
8582

8683
task.workerCount = viper.GetInt("task.workers")
8784
task.savePipeSize = viper.GetInt("task.savepipe")
85+
task.timeDelay = viper.GetInt("task.timedelay")
8886
task.workers = make(chan maptile.Tile, task.workerCount)
8987
task.savingpipe = make(chan Tile, task.savePipeSize)
9088
task.bufSize = viper.GetInt("task.mergebuf")
@@ -268,8 +266,8 @@ func (task *Task) tileFetcher(t maptile.Tile, url string) {
268266
T: t,
269267
C: body,
270268
}
271-
if task.TileMap.Format == PBF {
272269

270+
if task.TileMap.Format == PBF {
273271
var buf bytes.Buffer
274272
zw := gzip.NewWriter(&buf)
275273
_, err = zw.Write(body)
@@ -289,8 +287,12 @@ func (task *Task) tileFetcher(t maptile.Tile, url string) {
289287
// task.wg.Add(1)
290288
task.saveTile(tile)
291289
}
292-
secs := time.Since(start).Seconds()
293-
fmt.Printf("tile(z:%d, x:%d, y:%d), %.3fs, %.2f kb, %s ...\n", t.Z, t.X, t.Y, secs, float32(len(body))/1024.0, pbf)
290+
cost := time.Since(start).Milliseconds()
291+
if cost < int64(task.timeDelay) {
292+
time.Sleep(time.Duration(int64(task.timeDelay)-cost) * time.Millisecond)
293+
}
294+
cost = time.Since(start).Milliseconds()
295+
fmt.Printf("tile(z:%d, x:%d, y:%d), %dms , %.2f kb, %s ...\n", t.Z, t.X, t.Y, cost, float32(len(body))/1024.0, pbf)
294296
}
295297

296298
//DownloadZoom 下载指定层级
@@ -331,45 +333,6 @@ func (task *Task) downloadLayer(layer Layer) {
331333
bar.FinishPrint(fmt.Sprintf("Task %s Zoom %d finished ~", task.ID, layer.Zoom))
332334
}
333335

334-
//DownloadZoom 下载指定层级
335-
func (task *Task) downloadGeom(geom orb.Geometry, zoom int) {
336-
count := tilecover.GeometryCount(geom, maptile.Zoom(zoom))
337-
bar := pb.New64(count).Prefix(fmt.Sprintf("Zoom %d : ", zoom))
338-
bar.Start()
339-
340-
var tilelist = make(chan maptile.Tile, task.bufSize)
341-
342-
go func(g orb.Geometry, z maptile.Zoom, ch chan<- maptile.Tile) {
343-
defer close(ch)
344-
tilecover.GeometryChannel(g, z, ch)
345-
}(geom, maptile.Zoom(zoom), tilelist)
346-
347-
for tile := range tilelist {
348-
// log.Infof(`fetching tile %v ~`, tile)
349-
select {
350-
case task.workers <- tile:
351-
bar.Increment()
352-
task.Bar.Increment()
353-
task.tileWG.Add(1)
354-
go task.tileFetcher(tile, task.TileMap.URL)
355-
case <-task.abort:
356-
log.Infof("Task %s got canceled.", task.ID)
357-
close(tilelist)
358-
case <-task.pause:
359-
log.Infof("Task %s suspended.", task.ID)
360-
select {
361-
case <-task.play:
362-
log.Infof("task %s go on.", task.ID)
363-
case <-task.abort:
364-
log.Infof("task %s got canceled.", task.ID)
365-
close(tilelist)
366-
}
367-
}
368-
}
369-
task.tileWG.Wait()
370-
bar.FinishPrint(fmt.Sprintf("Task %s Zoom %d finished ~", task.ID, zoom))
371-
}
372-
373336
//Download 开启下载任务
374337
func (task *Task) Download() {
375338
//g orb.Geometry, minz int, maxz int
@@ -386,72 +349,3 @@ func (task *Task) Download() {
386349
}
387350
task.Bar.FinishPrint(fmt.Sprintf("Task %s finished ~", task.ID))
388351
}
389-
390-
//DownloadDepth 深度优先下载
391-
func (task *Task) DownloadDepth() {
392-
task.Bar = pb.New64(task.Total).Prefix("Fetching -> ").Postfix("\n")
393-
task.Bar.Start()
394-
// for _, layer := range task.Layers {
395-
// task.downloadLayer(layer)
396-
// break
397-
// }
398-
for i, layer := range task.Layers {
399-
if i < 1 {
400-
continue
401-
}
402-
task.tileSet.Lock()
403-
zoomSet := task.tileSet.M
404-
mfc := task.tileSet.M.ToFeatureCollection()
405-
ifile := len(task.tileSet.M)
406-
fmt.Printf("merging up append set, %d tiles ~\n", ifile)
407-
fmt.Println("downloading started, Zoom:", layer.Zoom, "Tiles:", ifile)
408-
bar := pb.StartNew(ifile).Prefix(fmt.Sprintf("Zoom %d : ", layer.Zoom)).Postfix("\n")
409-
var wg sync.WaitGroup
410-
wg.Add(1)
411-
go func(name string, mfc *geojson.FeatureCollection) {
412-
defer wg.Done()
413-
data, err := json.MarshalIndent(mfc, "", " ")
414-
if err != nil {
415-
log.Printf("error marshalling json: %v", err)
416-
return
417-
}
418-
419-
err = ioutil.WriteFile(name+".geojson", data, 0644)
420-
if err != nil {
421-
log.Printf("write file failure: %v", err)
422-
}
423-
log.Printf("output finished : %s.geojson", name)
424-
425-
}(strconv.FormatInt(int64(ifile), 10), mfc)
426-
427-
task.tileSet.M = make(maptile.Set)
428-
task.tileSet.Unlock()
429-
cliperBuffer := make(chan orb.Geometry, 16)
430-
go func(set maptile.Set, buffer chan<- orb.Geometry, bar *pb.ProgressBar) {
431-
defer close(buffer)
432-
for t := range set {
433-
bar.Increment()
434-
// buffer <- t.Bound()
435-
log.Println("starting cliper...")
436-
start := time.Now()
437-
for _, g := range layer.Collection {
438-
clipped := clip.Geometry(t.Bound(), g)
439-
secs := time.Since(start).Seconds()
440-
if clipped != nil {
441-
buffer <- clipped
442-
log.Printf("cliper add to buffer,time:%.4fs...", secs)
443-
}
444-
}
445-
}
446-
log.Printf("cliper buffer closing...")
447-
close(buffer)
448-
}(zoomSet, cliperBuffer, bar)
449-
450-
for geom := range cliperBuffer {
451-
task.downloadGeom(geom, layer.Zoom)
452-
}
453-
wg.Wait() //wait for saving
454-
bar.FinishPrint(fmt.Sprintf("zoom %d finished ~", layer.Zoom))
455-
}
456-
task.Bar.FinishPrint(fmt.Sprintf("Task %s finished ~", task.ID))
457-
}

0 commit comments

Comments
 (0)