shithub: mycel

Download patch

ref: f26be72083a16563f20142e02e59e0d4c662f6c1
parent: a767c4963bdc0c14120d7ebb084ce1fcae2cbe6c
author: Philip Silva <[email protected]>
date: Tue Jan 25 10:19:58 EST 2022

Parallel download for images

--- a/browser/browser.go
+++ b/browser/browser.go
@@ -220,7 +220,7 @@
 		mw, _ := n.CssPx("max-width")
 		w := n.Width()
 		h := n.Height()
-		i, err = img.Load(dui, browser, src, mw, w, h)
+		i, err = img.Load(dui, browser, src, mw, w, h, false)
 		if err != nil {
 			return nil, fmt.Errorf("load image: %w", err)
 		}
@@ -1514,10 +1514,14 @@
 	if err != nil {
 		log.Fatal(err)
 	}
-
+	tr := http.DefaultTransport.(*http.Transport).Clone()
+	tr.MaxIdleConns = 10
+	tr.MaxConnsPerHost = 6
+	tr.MaxIdleConnsPerHost = 6
 	b = &Browser{
 		client: &http.Client{
 			Jar: jar,
+			Transport: tr,
 		},
 		dui:      _dui,
 		Website:  &Website{},
@@ -1524,13 +1528,11 @@
 		LocCh:    make(chan string, 10),
 		StatusCh: make(chan string, 10),
 	}
-
 	u, err := url.Parse(initUrl)
 	if err != nil {
 		log.Fatalf("parse: %v", err)
 	}
 	b.History.Push(u, 0)
-
 	browser = b
 	b.Website.UI = &duit.Label{}
 	style.SetFetcher(b)
--- a/img/img.go
+++ b/img/img.go
@@ -192,32 +192,61 @@
 }
 
 // Load and resize to w and h if != 0
-//
-// Stolen from duit.ReadImage
-func Load(dui *duit.DUI, f opossum.Fetcher, src string, maxW, w, h int) (ni *draw.Image, err error) {
-	drawImg, err := load(f, src, maxW, w, h)
-	if err != nil {
-		return nil, err
+func Load(dui *duit.DUI, f opossum.Fetcher, src string, maxW, w, h int, forceSync bool) (ni *draw.Image, err error) {
+	log.Printf("Load(..., %v, maxW=%v, w=%v, h=%v, ...)", src, maxW, w, h)
+	ch := make(chan image.Image, 1)
+	var bounds draw.Rectangle
+	if w != 0 && h != 0 && !forceSync {
+		bounds = draw.Rect(0, 0, w, h)
+		go func() {
+			log.Printf("load async %v...", src)
+			drawImg, err := load(f, src, maxW, w, h)
+			if err != nil {
+				log.Errorf("load %v: %v", src, err)
+				close(ch)
+				return
+			}
+			ch <- drawImg
+			log.Printf("loaded async %v", src)
+		}()
+	} else {
+		drawImg, err := load(f, src, maxW, w, h)
+		if err != nil {
+			return nil, err
+		}
+		bounds = drawImg.Bounds()
+		ch <- drawImg
 	}
-	var rgba *image.RGBA
-	switch i := drawImg.(type) {
-	case *image.RGBA:
-		rgba = i
-	default:
-		b := drawImg.Bounds()
-		rgba = image.NewRGBA(image.Rectangle{image.ZP, b.Size()})
-		imagedraw.Draw(rgba, rgba.Bounds(), drawImg, b.Min, imagedraw.Src)
-	}
 
-	// todo: package image claims data is in r,g,b,a.  who is reversing the bytes? devdraw? will this work on big endian?
-	ni, err = dui.Display.AllocImage(rgba.Bounds(), draw.ABGR32, false, draw.White)
+	ni, err = dui.Display.AllocImage(bounds, draw.ABGR32, false, draw.White)
 	if err != nil {
 		return nil, fmt.Errorf("allocimage: %s", err)
 	}
-	_, err = ni.Load(rgba.Bounds(), rgba.Pix)
-	if err != nil {
-		return nil, fmt.Errorf("load image: %s", err)
-	}
+
+	go func() {
+		drawImg, ok := <-ch
+		if !ok {
+			log.Errorf("could not load image %v", src)
+			return
+		}
+		dui.Call <- func() {
+			// Stolen from duit.ReadImage
+			var rgba *image.RGBA
+			switch i := drawImg.(type) {
+			case *image.RGBA:
+				rgba = i
+			default:
+				b := drawImg.Bounds()
+				rgba = image.NewRGBA(image.Rectangle{image.ZP, b.Size()})
+				imagedraw.Draw(rgba, rgba.Bounds(), drawImg, b.Min, imagedraw.Src)
+			}
+			_, err = ni.Load(rgba.Bounds(), rgba.Pix)
+			if err != nil {
+				log.Errorf("load image: %s", err)
+			}
+			log.Printf("copied image %v", src)
+		}
+	}()
 	return
 }
 
--- a/style/experimental.go
+++ b/style/experimental.go
@@ -194,7 +194,7 @@
 		h := cs.Height()
 
 		var err error
-		i, err = img.Load(dui, fetcher, imgUrl, 0, w, h)
+		i, err = img.Load(dui, fetcher, imgUrl, 0, w, h, true)
 		if err != nil {
 			log.Errorf("bg img load %v: %v", imgUrl, err)
 			return