perf(dingtalk): rewrite answer images concurrently and make the rewrite budget configurable
- outboundimage: fetch and publish up to 4 images in parallel, dedupe in document order, log per-image outcome plus a rewrite summary - dingtalk handler: OutboundRewriteTimeout applies to the image rewrite stage only, falling back to finalDeliveryTimeout - cmd/dobotai: DOBOTAI_OUTBOUND_IMAGE_REWRITE_TIMEOUT (default 60s, invalid values fall back)
This commit is contained in:
@@ -2,11 +2,15 @@ package outboundimage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
"path"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -44,35 +48,34 @@ func NewRewriter(fetcher Fetcher, publisher Publisher, maxImages int) *Rewriter
|
||||
return &Rewriter{fetcher: fetcher, publisher: publisher, maxImages: maxImages}
|
||||
}
|
||||
|
||||
// rewriteConcurrency bounds how many images are fetched and published in
|
||||
// parallel. Manual screenshots are multi-megabyte objects whose upload time
|
||||
// dominates the delivery budget, so a serial pass cannot publish a long answer
|
||||
// before the delivery deadline and silently loses the remaining images.
|
||||
const rewriteConcurrency = 4
|
||||
|
||||
func (r *Rewriter) Rewrite(ctx context.Context, markdown string) string {
|
||||
if r == nil || r.fetcher == nil || r.publisher == nil || r.maxImages <= 0 {
|
||||
markdown = normalizePublicHTMLImages(stripResourceImages(markdown))
|
||||
markdown = resourceReferenceRE.ReplaceAllString(resourceHTMLUnsafeImageRE.ReplaceAllString(markdown, ""), "")
|
||||
return sanitizePublicMarkdownImages(markdown)
|
||||
}
|
||||
urls := map[string]string{}
|
||||
attempted := 0
|
||||
started := time.Now()
|
||||
urls := r.resolveReferences(ctx, markdown)
|
||||
resolve := func(reference string) string {
|
||||
if publishedURL, ok := urls[reference]; ok {
|
||||
return publishedURL
|
||||
return urls[reference]
|
||||
}
|
||||
if len(urls) > 0 {
|
||||
published, dropped := 0, 0
|
||||
for _, publishedURL := range urls {
|
||||
if publishedURL == "" {
|
||||
dropped++
|
||||
continue
|
||||
}
|
||||
published++
|
||||
}
|
||||
if attempted >= r.maxImages {
|
||||
urls[reference] = ""
|
||||
return ""
|
||||
}
|
||||
attempted++
|
||||
data, contentType, err := r.fetcher.Fetch(ctx, reference)
|
||||
if err != nil {
|
||||
urls[reference] = ""
|
||||
return ""
|
||||
}
|
||||
publishedURL, err := r.publisher.Publish(ctx, data, contentType)
|
||||
if err != nil || !strings.HasPrefix(publishedURL, "https://") {
|
||||
urls[reference] = ""
|
||||
return ""
|
||||
}
|
||||
urls[reference] = publishedURL
|
||||
return publishedURL
|
||||
log.Printf("outbound image: rewrite finished references=%d published=%d dropped=%d elapsed=%s",
|
||||
len(urls), published, dropped, time.Since(started).Round(time.Millisecond))
|
||||
}
|
||||
|
||||
markdown = resourceMarkdownImageRE.ReplaceAllStringFunc(markdown, func(image string) string {
|
||||
@@ -94,6 +97,97 @@ func (r *Rewriter) Rewrite(ctx context.Context, markdown string) string {
|
||||
return sanitizePublicMarkdownImages(markdown)
|
||||
}
|
||||
|
||||
// resolveReferences returns reference -> public URL for every image reference
|
||||
// in content, in document order and deduplicated, capped at maxImages. Each
|
||||
// image is fetched and published with at most rewriteConcurrency workers; a
|
||||
// reference that cannot be published maps to "".
|
||||
func (r *Rewriter) resolveReferences(ctx context.Context, content string) map[string]string {
|
||||
references := r.collectReferences(content)
|
||||
if len(references) == 0 {
|
||||
return map[string]string{}
|
||||
}
|
||||
results := make(map[string]string, len(references))
|
||||
var mutex sync.Mutex
|
||||
var budgetOnce sync.Once
|
||||
slots := make(chan struct{}, rewriteConcurrency)
|
||||
var wait sync.WaitGroup
|
||||
for _, reference := range references {
|
||||
wait.Add(1)
|
||||
go func(reference string) {
|
||||
defer wait.Done()
|
||||
slots <- struct{}{}
|
||||
defer func() { <-slots }()
|
||||
publishedURL := ""
|
||||
if err := ctx.Err(); err != nil {
|
||||
budgetOnce.Do(func() {
|
||||
log.Printf("outbound image: delivery budget exhausted, dropping remaining images: %v", err)
|
||||
})
|
||||
} else {
|
||||
publishedURL = r.publishReference(ctx, reference)
|
||||
}
|
||||
mutex.Lock()
|
||||
results[reference] = publishedURL
|
||||
mutex.Unlock()
|
||||
}(reference)
|
||||
}
|
||||
wait.Wait()
|
||||
return results
|
||||
}
|
||||
|
||||
// collectReferences gathers the resource references that carry a real image,
|
||||
// in document order, dropping duplicates and anything beyond maxImages. Bare or
|
||||
// unquoted references are never fetched.
|
||||
func (r *Rewriter) collectReferences(content string) []string {
|
||||
type hit struct {
|
||||
at int
|
||||
reference string
|
||||
}
|
||||
hits := make([]hit, 0, 16)
|
||||
for _, pattern := range []*regexp.Regexp{resourceMarkdownImageRE, resourceHTMLDoubleImageRE, resourceHTMLSingleImageRE} {
|
||||
for _, match := range pattern.FindAllStringSubmatchIndex(content, -1) {
|
||||
if len(match) < 4 || match[2] < 0 || match[3] > len(content) {
|
||||
continue
|
||||
}
|
||||
hits = append(hits, hit{at: match[0], reference: content[match[2]:match[3]]})
|
||||
}
|
||||
}
|
||||
sort.SliceStable(hits, func(i, j int) bool { return hits[i].at < hits[j].at })
|
||||
references := make([]string, 0, len(hits))
|
||||
seen := make(map[string]bool, len(hits))
|
||||
for _, candidate := range hits {
|
||||
if seen[candidate.reference] {
|
||||
continue
|
||||
}
|
||||
seen[candidate.reference] = true
|
||||
if len(references) >= r.maxImages {
|
||||
break
|
||||
}
|
||||
references = append(references, candidate.reference)
|
||||
}
|
||||
return references
|
||||
}
|
||||
|
||||
// publishReference fetches one image from WeKnora and publishes it to our own
|
||||
// object storage. Failures are logged with their reason instead of being
|
||||
// silently dropped from the answer.
|
||||
func (r *Rewriter) publishReference(ctx context.Context, reference string) string {
|
||||
started := time.Now()
|
||||
data, contentType, err := r.fetcher.Fetch(ctx, reference)
|
||||
if err != nil {
|
||||
log.Printf("outbound image: fetch failed, image dropped reference=%s error=%v", reference, err)
|
||||
return ""
|
||||
}
|
||||
publishedURL, err := r.publisher.Publish(ctx, data, contentType)
|
||||
if err != nil || !strings.HasPrefix(publishedURL, "https://") {
|
||||
log.Printf("outbound image: publish failed, image dropped reference=%s bytes=%d contentType=%s error=%v",
|
||||
reference, len(data), contentType, err)
|
||||
return ""
|
||||
}
|
||||
log.Printf("outbound image: published reference=%s bytes=%d contentType=%s elapsed=%s",
|
||||
reference, len(data), contentType, time.Since(started).Round(time.Millisecond))
|
||||
return publishedURL
|
||||
}
|
||||
|
||||
func sanitizePublicMarkdownImages(markdown string) string {
|
||||
markdown = markdownImageRE.ReplaceAllStringFunc(markdown, func(image string) string {
|
||||
match := markdownImageRE.FindStringSubmatch(image)
|
||||
|
||||
Reference in New Issue
Block a user