Prechádzať zdrojové kódy

Workers voor region ophalen

Harry de Boer 10 rokov pred
rodič
commit
39187f6c4e
1 zmenil súbory, kde vykonal 48 pridanie a 28 odobranie
  1. 48 28
      board/board.go

+ 48 - 28
board/board.go

@@ -28,8 +28,8 @@ type response struct {
 
 const (
 	maxIceWidth  = 26
-	regionWidth  = 32
-	regionHeight = 32
+	regionWidth  = 1024
+	regionHeight = 1024
 )
 
 // Create a new randomly generated board.
@@ -50,6 +50,7 @@ func NewRemote(width, height int) *Board {
 	yRegions := (height + (regionHeight - 1)) / regionHeight
 
 	returnChan := make(chan response, 32)
+	defer close(returnChan)
 	go b.genRegions(xRegions, yRegions, returnChan)
 
 	for answers := 0; xRegions*yRegions > answers; answers++ {
@@ -80,46 +81,65 @@ func (b *Board) placeIglo() {
 	b.Set(b.Finish.X+1, b.Finish.Y, Water)
 }
 
+type work struct {
+	x, y   int
+	width  int
+	height int
+}
+
 func (b *Board) genRegions(xRegions, yRegions int, responseChan chan response) {
+	const numWorkers = 50
+
+	workChan := make(chan work, numWorkers)
+
+	for i := 0; i < numWorkers; i++ {
+		go worker(workChan, responseChan)
+	}
 
 	for i := 0; i < xRegions; i++ {
 		for j := 0; j < yRegions; j++ {
-			var partialWidth, partialHeight int
 			x := i * regionWidth
 			y := j * regionHeight
+			workChan <- work{x, y, b.Width, b.Height}
+		}
+	}
 
-			if x+regionWidth > b.Width {
-				partialWidth = b.Width - x
-			} else {
-				partialWidth = regionWidth
-			}
+}
 
-			if y+regionHeight > b.Height {
-				partialHeight = b.Height - y
-			} else {
-				partialHeight = regionHeight
-			}
+func worker(workChan chan work, responseChan chan response) {
 
-			requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", b.Width, b.Height, partialWidth, partialHeight, x, y)
-			log.Printf("Requested generation of tile with url: %v\n", requestUrl)
+	for w, open := <-workChan; open; w, open = <-workChan {
+		var partialWidth, partialHeight int
+		if w.x+regionWidth > w.width {
+			partialWidth = w.width - w.x
+		} else {
+			partialWidth = regionWidth
+		}
 
-			resp, err := http.Get(requestUrl)
+		if w.y+regionHeight > w.height {
+			partialHeight = w.height - w.y
+		} else {
+			partialHeight = regionHeight
+		}
 
-			if err != nil {
-				log.Printf("ERROR requesting region: %v", err)
-				continue
-			}
+		requestUrl := fmt.Sprintf("http://localhost:8081/?totalWidth=%v&totalHeight=%v&width=%v&height=%v&x=%v&y=%v", w.width, w.height, partialWidth, partialHeight, w.x, w.y)
+		log.Printf("Requested generation of tile with url: %v\n", requestUrl)
 
-			result := response{
-				x:         x,
-				y:         y,
-				width:     partialHeight,
-				height:    partialHeight,
-				boardTile: resp.Body,
-			}
+		resp, err := http.Get(requestUrl)
+		for err != nil {
+			log.Printf("ERROR requesting region: %v", err)
+			resp, err = http.Get(requestUrl)
+		}
 
-			responseChan <- result
+		result := response{
+			x:         w.x,
+			y:         w.y,
+			width:     partialHeight,
+			height:    partialHeight,
+			boardTile: resp.Body,
 		}
+
+		responseChan <- result
 	}
 }