Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete all service in parallel in commands #609

Merged
merged 3 commits into from
Dec 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions commands/provider/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html/template"
"os"
"path/filepath"
"sync"

"github.com/mesg-foundation/core/commands/provider/assets"
"github.com/mesg-foundation/core/protobuf/coreapi"
Expand Down Expand Up @@ -43,13 +44,21 @@ func (p *ServiceProvider) ServiceDeleteAll() error {
return err
}

var errs xerrors.Errors
var (
errs xerrors.SyncErrors
wg sync.WaitGroup
)
wg.Add(len(rep.Services))
for _, s := range rep.Services {
_, err := p.client.DeleteService(context.Background(), &coreapi.DeleteServiceRequest{ServiceID: s.ID})
if err != nil {
errs = append(errs, err)
}
go func(id string) {
_, err := p.client.DeleteService(context.Background(), &coreapi.DeleteServiceRequest{ServiceID: id})
if err != nil {
errs.Append(err)
}
wg.Done()
}(s.ID)
}
wg.Wait()
return errs.ErrorOrNil()
}

Expand Down
18 changes: 17 additions & 1 deletion x/xerrors/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package xerrors

import "strings"
import (
"strings"
"sync"
)

// Errors is an error for tracing multiple errors.
type Errors []error
Expand All @@ -23,3 +26,16 @@ func (e Errors) Error() string {

return strings.Join(s, "\n")
}

// SyncErrors is an error for tracing multiple errors safe to use in multiple goroutines.
type SyncErrors struct {
Copy link
Contributor

@ilgooz ilgooz Dec 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to have Append and AppendSync (or AppendSafe) methods for Error type. I think we don't quite need two types here.

mx sync.Mutex
Errors
}

// Append appends given err.
func (e *SyncErrors) Append(err error) {
e.mx.Lock()
e.Errors = append(e.Errors, err)
e.mx.Unlock()
}