lib/protocol: Further interface refactor (#9396)

This is a symmetric change to #9375 -- where that PR changed the
protocol->model interface, this changes the model->protocol one.
This commit is contained in:
Jakob Borg
2024-08-24 12:45:10 +02:00
committed by GitHub
parent 7df75e681d
commit 5342bec1b7
13 changed files with 170 additions and 221 deletions
+9 -7
View File
@@ -30,8 +30,8 @@ func newFakeConnection(id protocol.DeviceID, model Model) *fakeConnection {
model: model,
closed: make(chan struct{}),
}
f.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
return f.fileData[name], nil
f.RequestCalls(func(ctx context.Context, req *protocol.Request) ([]byte, error) {
return f.fileData[req.Name], nil
})
f.DeviceIDReturns(id)
f.ConnectionIDReturns(rand.String(16))
@@ -60,14 +60,16 @@ type fakeConnection struct {
}
func (f *fakeConnection) setIndexFn(fn func(_ context.Context, folder string, fs []protocol.FileInfo) error) {
f.IndexCalls(fn)
f.IndexUpdateCalls(fn)
f.IndexCalls(func(ctx context.Context, idx *protocol.Index) error { return fn(ctx, idx.Folder, idx.Files) })
f.IndexUpdateCalls(func(ctx context.Context, idxUp *protocol.IndexUpdate) error {
return fn(ctx, idxUp.Folder, idxUp.Files)
})
}
func (f *fakeConnection) DownloadProgress(_ context.Context, folder string, updates []protocol.FileDownloadProgressUpdate) {
func (f *fakeConnection) DownloadProgress(_ context.Context, dp *protocol.DownloadProgress) {
f.downloadProgressMessages = append(f.downloadProgressMessages, downloadProgressMessage{
folder: folder,
updates: updates,
folder: dp.Folder,
updates: dp.Updates,
})
}
+2 -2
View File
@@ -228,9 +228,9 @@ func (s *indexHandler) sendIndexTo(ctx context.Context, fset *db.FileSet) error
l.Debugf("%v: Sending %d files (<%d bytes)", s, len(fs), batch.Size())
if initial {
initial = false
return s.conn.Index(ctx, s.folder, fs)
return s.conn.Index(ctx, &protocol.Index{Folder: s.folder, Files: fs})
}
return s.conn.IndexUpdate(ctx, s.folder, fs)
return s.conn.IndexUpdate(ctx, &protocol.IndexUpdate{Folder: s.folder, Files: fs})
})
var err error
+5 -5
View File
@@ -40,10 +40,10 @@ func TestIndexhandlerConcurrency(t *testing.T) {
c2.Start()
defer c2.Close(io.EOF)
c1.ClusterConfig(protocol.ClusterConfig{})
c2.ClusterConfig(protocol.ClusterConfig{})
c1.Index(ctx, "foo", nil)
c2.Index(ctx, "foo", nil)
c1.ClusterConfig(&protocol.ClusterConfig{})
c2.ClusterConfig(&protocol.ClusterConfig{})
c1.Index(ctx, &protocol.Index{Folder: "foo"})
c2.Index(ctx, &protocol.Index{Folder: "foo"})
const msgs = 5e2
const files = 1e3
@@ -64,7 +64,7 @@ func TestIndexhandlerConcurrency(t *testing.T) {
})
b1 := db.NewFileInfoBatch(func(fs []protocol.FileInfo) error {
return c1.IndexUpdate(ctx, "foo", fs)
return c1.IndexUpdate(ctx, &protocol.IndexUpdate{Folder: "foo", Files: fs})
})
sentEntries := 0
for i := 0; i < msgs; i++ {
+5 -5
View File
@@ -2414,7 +2414,7 @@ func (m *model) promoteConnections() {
if conn.Statistics().StartedAt.IsZero() {
conn.SetFolderPasswords(passwords)
conn.Start()
conn.ClusterConfig(protocol.ClusterConfig{Secondary: true})
conn.ClusterConfig(&protocol.ClusterConfig{Secondary: true})
}
}
}
@@ -2469,7 +2469,7 @@ func (m *model) RequestGlobal(ctx context.Context, deviceID protocol.DeviceID, f
}
l.Debugf("%v REQ(out): %s (%s): %q / %q b=%d o=%d s=%d h=%x wh=%x ft=%t", m, deviceID.Short(), conn, folder, name, blockNo, offset, size, hash, weakHash, fromTemporary)
return conn.Request(ctx, folder, name, blockNo, offset, size, hash, weakHash, fromTemporary)
return conn.Request(ctx, &protocol.Request{Folder: folder, Name: name, BlockNo: blockNo, Offset: offset, Size: size, Hash: hash, WeakHash: weakHash, FromTemporary: fromTemporary})
}
// requestConnectionForDevice returns a connection to the given device, to
@@ -2586,14 +2586,14 @@ func (m *model) numHashers(folder string) int {
// generateClusterConfig returns a ClusterConfigMessage that is correct and the
// set of folder passwords for the given peer device
func (m *model) generateClusterConfig(device protocol.DeviceID) (protocol.ClusterConfig, map[string]string) {
func (m *model) generateClusterConfig(device protocol.DeviceID) (*protocol.ClusterConfig, map[string]string) {
m.mut.RLock()
defer m.mut.RUnlock()
return m.generateClusterConfigRLocked(device)
}
func (m *model) generateClusterConfigRLocked(device protocol.DeviceID) (protocol.ClusterConfig, map[string]string) {
var message protocol.ClusterConfig
func (m *model) generateClusterConfigRLocked(device protocol.DeviceID) (*protocol.ClusterConfig, map[string]string) {
message := &protocol.ClusterConfig{}
folders := m.cfg.FolderList()
passwords := make(map[string]string, len(folders))
for _, folderCfg := range folders {
+2 -2
View File
@@ -3631,11 +3631,11 @@ func testConfigChangeTriggersClusterConfigs(t *testing.T, expectFirst, expectSec
cc1 := make(chan struct{}, 1)
cc2 := make(chan struct{}, 1)
fc1 := newFakeConnection(device1, m)
fc1.ClusterConfigCalls(func(_ protocol.ClusterConfig) {
fc1.ClusterConfigCalls(func(_ *protocol.ClusterConfig) {
cc1 <- struct{}{}
})
fc2 := newFakeConnection(device2, m)
fc2.ClusterConfigCalls(func(_ protocol.ClusterConfig) {
fc2.ClusterConfigCalls(func(_ *protocol.ClusterConfig) {
cc2 <- struct{}{}
})
m.AddConnection(fc1, protocol.Hello{})
+2 -2
View File
@@ -39,7 +39,7 @@ type progressUpdate struct {
}
func (p progressUpdate) send(ctx context.Context) {
p.conn.DownloadProgress(ctx, p.folder, p.updates)
p.conn.DownloadProgress(ctx, &protocol.DownloadProgress{Folder: p.folder, Updates: p.updates})
}
// NewProgressEmitter creates a new progress emitter which emits
@@ -334,7 +334,7 @@ func (t *ProgressEmitter) clearLocked() {
}
for _, folder := range state.folders() {
if updates := state.cleanup(folder); len(updates) > 0 {
conn.DownloadProgress(context.Background(), folder, updates)
conn.DownloadProgress(context.Background(), &protocol.DownloadProgress{Folder: folder, Updates: updates})
}
}
}
+11 -11
View File
@@ -143,11 +143,11 @@ func TestSymlinkTraversalWrite(t *testing.T) {
}
return nil
})
fc.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
if name != "symlink" && strings.HasPrefix(name, "symlink") {
badReq <- name
fc.RequestCalls(func(ctx context.Context, req *protocol.Request) ([]byte, error) {
if req.Name != "symlink" && strings.HasPrefix(req.Name, "symlink") {
badReq <- req.Name
}
return fc.fileData[name], nil
return fc.fileData[req.Name], nil
})
// Send an update for the symlink, wait for it to sync and be reported back.
@@ -338,7 +338,7 @@ func pullInvalidIgnored(t *testing.T, ft config.FolderType) {
})
// Make sure pulling doesn't interfere, as index updates are racy and
// thus we cannot distinguish between scan and pull results.
fc.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
fc.RequestCalls(func(_ context.Context, _ *protocol.Request) ([]byte, error) {
return nil, nil
})
@@ -926,7 +926,7 @@ func TestNeedFolderFiles(t *testing.T) {
defer sub.Unsubscribe()
errPreventSync := errors.New("you aren't getting any of this")
fc.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
fc.RequestCalls(func(_ context.Context, _ *protocol.Request) ([]byte, error) {
return nil, errPreventSync
})
@@ -1065,9 +1065,9 @@ func TestRequestLastFileProgress(t *testing.T) {
done := make(chan struct{})
fc.RequestCalls(func(ctx context.Context, folder, name string, blockNo int, offset int64, size int, hash []byte, weakHash uint32, fromTemporary bool) ([]byte, error) {
fc.RequestCalls(func(_ context.Context, req *protocol.Request) ([]byte, error) {
defer close(done)
progress, queued, rest, err := m.NeedFolderFiles(folder, 1, 10)
progress, queued, rest, err := m.NeedFolderFiles(req.Folder, 1, 10)
must(t, err)
if len(queued)+len(rest) != 0 {
t.Error(`There should not be any queued or "rest" items`)
@@ -1075,7 +1075,7 @@ func TestRequestLastFileProgress(t *testing.T) {
if len(progress) != 1 {
t.Error("Expected exactly one item in progress.")
}
return fc.fileData[name], nil
return fc.fileData[req.Name], nil
})
contents := []byte("test file contents\n")
@@ -1232,7 +1232,7 @@ func TestRequestIndexSenderClusterConfigBeforeStart(t *testing.T) {
done := make(chan struct{})
defer close(done) // Must be the last thing to be deferred, thus first to run.
indexChan := make(chan []protocol.FileInfo, 1)
ccChan := make(chan protocol.ClusterConfig, 1)
ccChan := make(chan *protocol.ClusterConfig, 1)
fc.setIndexFn(func(_ context.Context, folder string, fs []protocol.FileInfo) error {
select {
case indexChan <- fs:
@@ -1240,7 +1240,7 @@ func TestRequestIndexSenderClusterConfigBeforeStart(t *testing.T) {
}
return nil
})
fc.ClusterConfigCalls(func(cc protocol.ClusterConfig) {
fc.ClusterConfigCalls(func(cc *protocol.ClusterConfig) {
select {
case ccChan <- cc:
case <-done: