mirror of
https://github.com/samuelncui/acp.git
synced 2025-12-23 05:05:15 +00:00
feat: add wrap for panic
This commit is contained in:
2
acp.go
2
acp.go
@@ -85,7 +85,7 @@ func New(ctx context.Context, opts ...Option) (*Copyer, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.running.Add(1)
|
c.running.Add(1)
|
||||||
go c.run(ctx)
|
go wrap(ctx, func() { c.run(ctx) })
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
copy.go
24
copy.go
@@ -30,7 +30,7 @@ func (c *Copyer) copy(ctx context.Context) {
|
|||||||
wg := new(sync.WaitGroup)
|
wg := new(sync.WaitGroup)
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer close(c.writePipe)
|
defer close(c.writePipe)
|
||||||
|
|
||||||
@@ -43,11 +43,11 @@ func (c *Copyer) copy(ctx context.Context) {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
|
|
||||||
for i := 0; i < c.threads; i++ {
|
for i := 0; i < c.threads; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -63,20 +63,20 @@ func (c *Copyer) copy(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
for job := range c.postPipe {
|
for job := range c.postPipe {
|
||||||
c.post(wg, job)
|
c.post(wg, job)
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
|
|
||||||
finished := make(chan struct{}, 1)
|
finished := make(chan struct{}, 1)
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
finished <- struct{}{}
|
finished <- struct{}{}
|
||||||
}()
|
})
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-finished:
|
case <-finished:
|
||||||
@@ -154,7 +154,7 @@ func (c *Copyer) write(ctx context.Context, job *writeJob) {
|
|||||||
chans = append(chans, ch)
|
chans = append(chans, ch)
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
defer sha256Pool.Put(sha)
|
defer sha256Pool.Put(sha)
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ func (c *Copyer) write(ctx context.Context, job *writeJob) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
job.setHash(sha.Sum(nil))
|
job.setHash(sha.Sum(nil))
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var readErr error
|
var readErr error
|
||||||
@@ -187,7 +187,7 @@ func (c *Copyer) write(ctx context.Context, job *writeJob) {
|
|||||||
chans = append(chans, ch)
|
chans = append(chans, ch)
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
var rerr error
|
var rerr error
|
||||||
@@ -230,7 +230,7 @@ func (c *Copyer) write(ctx context.Context, job *writeJob) {
|
|||||||
if readErr != nil {
|
if readErr != nil {
|
||||||
rerr = readErr
|
rerr = readErr
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(chans) == 0 {
|
if len(chans) == 0 {
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func (c *Copyer) startProgressBar(ctx context.Context) {
|
|||||||
ch <- func() { logrus.StandardLogger().Logf(l, format, args...) }
|
ch <- func() { logrus.StandardLogger().Logf(l, format, args...) }
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
copying := make(map[int64]struct{}, c.threads)
|
copying := make(map[int64]struct{}, c.threads)
|
||||||
|
|
||||||
for f := range ch {
|
for f := range ch {
|
||||||
@@ -77,9 +77,9 @@ func (c *Copyer) startProgressBar(ctx context.Context) {
|
|||||||
v()
|
v()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
|
|
||||||
go func() {
|
go wrap(ctx, func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
|
|
||||||
ticker := time.NewTicker(barUpdateInterval) // around 255ms, avoid conflict with progress bar fresh by second
|
ticker := time.NewTicker(barUpdateInterval) // around 255ms, avoid conflict with progress bar fresh by second
|
||||||
@@ -112,5 +112,5 @@ func (c *Copyer) startProgressBar(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
30
recover.go
Normal file
30
recover.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package acp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func wrap(ctx context.Context, f func()) {
|
||||||
|
defer func() {
|
||||||
|
e := recover()
|
||||||
|
if e == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
switch v := e.(type) {
|
||||||
|
case error:
|
||||||
|
err = v
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.WithContext(ctx).WithError(err).Errorf("panic: %s", debug.Stack())
|
||||||
|
}()
|
||||||
|
|
||||||
|
f()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user