mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
provide context to delete function
This commit is contained in:
parent
b82af41535
commit
5e1cae516e
4 changed files with 25 additions and 20 deletions
|
@ -11,11 +11,11 @@ type deleteLoop struct {
|
|||
deleteCtx context.Context
|
||||
deleteCancel context.CancelFunc
|
||||
deleteChan chan struct{}
|
||||
deleteFunc func()
|
||||
deleteFunc func(ctx context.Context)
|
||||
loopDone chan struct{}
|
||||
}
|
||||
|
||||
func newDeleteLoop(deleteFunc func()) *deleteLoop {
|
||||
func newDeleteLoop(deleteFunc func(ctx context.Context)) *deleteLoop {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &deleteLoop{
|
||||
deleteCtx: ctx,
|
||||
|
@ -32,7 +32,7 @@ func (dl *deleteLoop) Run() {
|
|||
|
||||
func (dl *deleteLoop) loop() {
|
||||
defer close(dl.loopDone)
|
||||
dl.deleteFunc()
|
||||
dl.deleteFunc(dl.deleteCtx)
|
||||
ticker := time.NewTicker(deleteLoopInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
|
@ -40,10 +40,10 @@ func (dl *deleteLoop) loop() {
|
|||
case <-dl.deleteCtx.Done():
|
||||
return
|
||||
case <-dl.deleteChan:
|
||||
dl.deleteFunc()
|
||||
dl.deleteFunc(dl.deleteCtx)
|
||||
ticker.Reset(deleteLoopInterval)
|
||||
case <-ticker.C:
|
||||
dl.deleteFunc()
|
||||
dl.deleteFunc(dl.deleteCtx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,16 +2,18 @@ package deletionmanager
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/anyproto/any-sync/app/logger"
|
||||
"github.com/anyproto/any-sync/commonspace/deletionstate"
|
||||
"github.com/anyproto/any-sync/commonspace/object/tree/treestorage"
|
||||
"github.com/anyproto/any-sync/commonspace/object/treemanager"
|
||||
"github.com/anyproto/any-sync/commonspace/spacestorage"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type Deleter interface {
|
||||
Delete()
|
||||
Delete(ctx context.Context)
|
||||
}
|
||||
|
||||
type deleter struct {
|
||||
|
@ -25,7 +27,7 @@ func newDeleter(st spacestorage.SpaceStorage, state deletionstate.ObjectDeletion
|
|||
return &deleter{st, state, getter, log}
|
||||
}
|
||||
|
||||
func (d *deleter) Delete() {
|
||||
func (d *deleter) Delete(ctx context.Context) {
|
||||
var (
|
||||
allQueued = d.state.GetQueued()
|
||||
spaceId = d.st.Id()
|
||||
|
@ -39,7 +41,7 @@ func (d *deleter) Delete() {
|
|||
continue
|
||||
}
|
||||
} else {
|
||||
err = d.getter.DeleteTree(context.Background(), spaceId, id)
|
||||
err = d.getter.DeleteTree(ctx, spaceId, id)
|
||||
if err != nil && err != spacestorage.ErrTreeStorageAlreadyDeleted {
|
||||
log.Error("failed to delete object", zap.Error(err))
|
||||
continue
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
package deletionmanager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/mock/gomock"
|
||||
|
||||
"github.com/anyproto/any-sync/commonspace/deletionstate/mock_deletionstate"
|
||||
"github.com/anyproto/any-sync/commonspace/object/tree/treestorage"
|
||||
"github.com/anyproto/any-sync/commonspace/object/treemanager/mock_treemanager"
|
||||
"github.com/anyproto/any-sync/commonspace/spacestorage/mock_spacestorage"
|
||||
"go.uber.org/mock/gomock"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeleter_Delete(t *testing.T) {
|
||||
|
@ -27,7 +30,7 @@ func TestDeleter_Delete(t *testing.T) {
|
|||
treeManager.EXPECT().MarkTreeDeleted(gomock.Any(), spaceId, id).Return(nil)
|
||||
delState.EXPECT().Delete(id).Return(nil)
|
||||
|
||||
deleter.Delete()
|
||||
deleter.Delete(context.TODO())
|
||||
})
|
||||
|
||||
t.Run("deleter delete mark deleted other error", func(t *testing.T) {
|
||||
|
@ -37,7 +40,7 @@ func TestDeleter_Delete(t *testing.T) {
|
|||
st.EXPECT().Id().Return(spaceId)
|
||||
st.EXPECT().TreeStorage(id).Return(nil, fmt.Errorf("unknown error"))
|
||||
|
||||
deleter.Delete()
|
||||
deleter.Delete(context.TODO())
|
||||
})
|
||||
|
||||
t.Run("deleter delete mark deleted fail", func(t *testing.T) {
|
||||
|
@ -48,7 +51,7 @@ func TestDeleter_Delete(t *testing.T) {
|
|||
st.EXPECT().TreeStorage(id).Return(nil, treestorage.ErrUnknownTreeId)
|
||||
treeManager.EXPECT().MarkTreeDeleted(gomock.Any(), spaceId, id).Return(fmt.Errorf("mark error"))
|
||||
|
||||
deleter.Delete()
|
||||
deleter.Delete(context.TODO())
|
||||
})
|
||||
|
||||
t.Run("deleter delete success", func(t *testing.T) {
|
||||
|
@ -60,7 +63,7 @@ func TestDeleter_Delete(t *testing.T) {
|
|||
treeManager.EXPECT().DeleteTree(gomock.Any(), spaceId, id).Return(nil)
|
||||
delState.EXPECT().Delete(id).Return(nil)
|
||||
|
||||
deleter.Delete()
|
||||
deleter.Delete(context.TODO())
|
||||
})
|
||||
|
||||
t.Run("deleter delete error", func(t *testing.T) {
|
||||
|
@ -71,6 +74,6 @@ func TestDeleter_Delete(t *testing.T) {
|
|||
st.EXPECT().TreeStorage(id).Return(nil, nil)
|
||||
treeManager.EXPECT().DeleteTree(gomock.Any(), spaceId, id).Return(fmt.Errorf("some error"))
|
||||
|
||||
deleter.Delete()
|
||||
deleter.Delete(context.TODO())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -135,13 +135,13 @@ func (m *MockDeleter) EXPECT() *MockDeleterMockRecorder {
|
|||
}
|
||||
|
||||
// Delete mocks base method.
|
||||
func (m *MockDeleter) Delete() {
|
||||
func (m *MockDeleter) Delete(arg0 context.Context) {
|
||||
m.ctrl.T.Helper()
|
||||
m.ctrl.Call(m, "Delete")
|
||||
m.ctrl.Call(m, "Delete", arg0)
|
||||
}
|
||||
|
||||
// Delete indicates an expected call of Delete.
|
||||
func (mr *MockDeleterMockRecorder) Delete() *gomock.Call {
|
||||
func (mr *MockDeleterMockRecorder) Delete(arg0 any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDeleter)(nil).Delete))
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDeleter)(nil).Delete), arg0)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue