From e0c91df4efef11b1115eb2fa3400d8bb86526ce5 Mon Sep 17 00:00:00 2001 From: derfenix Date: Fri, 14 Apr 2023 09:32:13 +0300 Subject: [PATCH] Refactoring --- entity/page.go | 12 ++++++++---- entity/result.go | 9 +++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/entity/page.go b/entity/page.go index 359868b..8d98dc5 100644 --- a/entity/page.go +++ b/entity/page.go @@ -62,7 +62,7 @@ type Page struct { Description string Created time.Time Formats []Format - Results Results + Results ResultsRO Version uint16 Status Status Meta Meta @@ -83,25 +83,27 @@ func (p *Page) Process(ctx context.Context, processor Processor) { p.Meta = meta } + results := Results{} + for _, format := range p.Formats { go func(format Format) { defer innerWG.Done() defer func() { if err := recover(); err != nil { - p.Results.Add(Result{Format: format, Err: fmt.Errorf("recovered from panic: %v", err)}) + results.Add(Result{Format: format, Err: fmt.Errorf("recovered from panic: %v", err)}) } }() result := processor.Process(ctx, format, p.URL) - p.Results.Add(result) + results.Add(result) }(format) } innerWG.Wait() var hasResultWithOutErrors bool - for _, result := range p.Results.Results() { + for _, result := range results.Results() { if result.Err != nil { p.Status = StatusWithErrors } else { @@ -116,4 +118,6 @@ func (p *Page) Process(ctx context.Context, processor Processor) { if p.Status == StatusProcessing { p.Status = StatusDone } + + p.Results = results.RO() } diff --git a/entity/result.go b/entity/result.go index ae3d017..fd31f39 100644 --- a/entity/result.go +++ b/entity/result.go @@ -6,6 +6,8 @@ import ( "github.com/vmihailenco/msgpack/v5" ) +type ResultsRO []Result + type Result struct { Format Format Err error @@ -17,6 +19,13 @@ type Results struct { results []Result } +func (r *Results) RO() ResultsRO { + r.mu.Lock() + defer r.mu.Unlock() + + return r.results +} + func (r *Results) MarshalMsgpack() ([]byte, error) { return msgpack.Marshal(r.results) }