Simplify request factory

This commit is contained in:
mcrakhman 2023-04-19 22:52:06 +02:00 committed by Mikhail Iudin
parent fed0eee243
commit 0d514560a4
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
4 changed files with 8 additions and 11 deletions

View File

@ -6,7 +6,6 @@ import (
"github.com/anytypeio/any-sync/commonspace/object/tree/objecttree" "github.com/anytypeio/any-sync/commonspace/object/tree/objecttree"
"github.com/anytypeio/any-sync/commonspace/object/tree/treechangeproto" "github.com/anytypeio/any-sync/commonspace/object/tree/treechangeproto"
"github.com/anytypeio/any-sync/commonspace/object/tree/treestorage" "github.com/anytypeio/any-sync/commonspace/object/tree/treestorage"
"github.com/anytypeio/any-sync/commonspace/objectsync"
"github.com/anytypeio/any-sync/commonspace/spacestorage" "github.com/anytypeio/any-sync/commonspace/spacestorage"
"github.com/anytypeio/any-sync/net/peer" "github.com/anytypeio/any-sync/net/peer"
"github.com/anytypeio/any-sync/net/rpc/rpcerr" "github.com/anytypeio/any-sync/net/rpc/rpcerr"
@ -47,7 +46,7 @@ func (t treeRemoteGetter) getPeers(ctx context.Context) (peerIds []string, err e
} }
func (t treeRemoteGetter) treeRequest(ctx context.Context, peerId string) (msg *treechangeproto.TreeSyncMessage, err error) { func (t treeRemoteGetter) treeRequest(ctx context.Context, peerId string) (msg *treechangeproto.TreeSyncMessage, err error) {
newTreeRequest := objectsync.GetRequestFactory().CreateNewTreeRequest() newTreeRequest := t.deps.SyncClient.CreateNewTreeRequest()
resp, err := t.deps.SyncClient.SendSync(ctx, peerId, t.treeId, newTreeRequest) resp, err := t.deps.SyncClient.SendSync(ctx, peerId, t.treeId, newTreeRequest)
if err != nil { if err != nil {
return return

View File

@ -94,7 +94,7 @@ type testSyncHandler struct {
// createSyncHandler creates a sync handler when a tree is already created // createSyncHandler creates a sync handler when a tree is already created
func createSyncHandler(peerId, spaceId string, objTree objecttree.ObjectTree, log *messageLog) *testSyncHandler { func createSyncHandler(peerId, spaceId string, objTree objecttree.ObjectTree, log *messageLog) *testSyncHandler {
factory := objectsync.GetRequestFactory() factory := objectsync.NewRequestFactory()
syncClient := objectsync.NewSyncClient(spaceId, newTestMessagePool(peerId, log), factory) syncClient := objectsync.NewSyncClient(spaceId, newTestMessagePool(peerId, log), factory)
netTree := &broadcastTree{ netTree := &broadcastTree{
ObjectTree: objTree, ObjectTree: objTree,
@ -106,7 +106,7 @@ func createSyncHandler(peerId, spaceId string, objTree objecttree.ObjectTree, lo
// createEmptySyncHandler creates a sync handler when the tree will be provided later (this emulates the situation when we have no tree) // createEmptySyncHandler creates a sync handler when the tree will be provided later (this emulates the situation when we have no tree)
func createEmptySyncHandler(peerId, spaceId string, aclList list.AclList, log *messageLog) *testSyncHandler { func createEmptySyncHandler(peerId, spaceId string, aclList list.AclList, log *messageLog) *testSyncHandler {
factory := objectsync.GetRequestFactory() factory := objectsync.NewRequestFactory()
syncClient := objectsync.NewSyncClient(spaceId, newTestMessagePool(peerId, log), factory) syncClient := objectsync.NewSyncClient(spaceId, newTestMessagePool(peerId, log), factory)
batcher := mb.New[protocolMsg](0) batcher := mb.New[protocolMsg](0)
@ -138,7 +138,7 @@ func (h *testSyncHandler) HandleMessage(ctx context.Context, senderId string, re
return return
} }
if unmarshalled.Content.GetFullSyncResponse() == nil { if unmarshalled.Content.GetFullSyncResponse() == nil {
newTreeRequest := objectsync.GetRequestFactory().CreateNewTreeRequest() newTreeRequest := objectsync.NewRequestFactory().CreateNewTreeRequest()
var objMsg *spacesyncproto.ObjectSyncMessage var objMsg *spacesyncproto.ObjectSyncMessage
objMsg, err = objectsync.MarshallTreeMessage(newTreeRequest, request.SpaceId, request.ObjectId, "") objMsg, err = objectsync.MarshallTreeMessage(newTreeRequest, request.SpaceId, request.ObjectId, "")
if err != nil { if err != nil {
@ -165,7 +165,7 @@ func (h *testSyncHandler) HandleMessage(ctx context.Context, senderId string, re
} }
h.SyncHandler = newSyncTreeHandler(request.SpaceId, netTree, h.syncClient, syncstatus.NewNoOpSyncStatus()) h.SyncHandler = newSyncTreeHandler(request.SpaceId, netTree, h.syncClient, syncstatus.NewNoOpSyncStatus())
var objMsg *spacesyncproto.ObjectSyncMessage var objMsg *spacesyncproto.ObjectSyncMessage
newTreeRequest := objectsync.GetRequestFactory().CreateHeadUpdate(netTree, res.Added) newTreeRequest := objectsync.NewRequestFactory().CreateHeadUpdate(netTree, res.Added)
objMsg, err = objectsync.MarshallTreeMessage(newTreeRequest, request.SpaceId, request.ObjectId, "") objMsg, err = objectsync.MarshallTreeMessage(newTreeRequest, request.SpaceId, request.ObjectId, "")
if err != nil { if err != nil {
return return

View File

@ -61,7 +61,7 @@ func NewObjectSync(
configuration: configuration, configuration: configuration,
} }
os.messagePool = newMessagePool(peerManager, os.handleMessage) os.messagePool = newMessagePool(peerManager, os.handleMessage)
os.syncClient = NewSyncClient(spaceId, os.messagePool, GetRequestFactory()) os.syncClient = NewSyncClient(spaceId, os.messagePool, NewRequestFactory())
return os return os
} }

View File

@ -14,10 +14,8 @@ type RequestFactory interface {
CreateFullSyncResponse(t objecttree.ObjectTree, theirHeads, theirSnapshotPath []string) (*treechangeproto.TreeSyncMessage, error) CreateFullSyncResponse(t objecttree.ObjectTree, theirHeads, theirSnapshotPath []string) (*treechangeproto.TreeSyncMessage, error)
} }
var sharedFactory = &requestFactory{} func NewRequestFactory() RequestFactory {
return &requestFactory{}
func GetRequestFactory() RequestFactory {
return sharedFactory
} }
type requestFactory struct{} type requestFactory struct{}