lib/model: Sort and group model initialization better (ref #6303)

This commit is contained in:
Jakob Borg 2020-02-01 08:12:25 +01:00
parent 55937b61ca
commit 5823e7a5ce

View File

@ -111,26 +111,29 @@ type Model interface {
type model struct { type model struct {
*suture.Supervisor *suture.Supervisor
// constructor parameters
cfg config.Wrapper cfg config.Wrapper
db *db.Lowlevel
finder *db.BlockFinder
progressEmitter *ProgressEmitter
id protocol.DeviceID id protocol.DeviceID
shortID protocol.ShortID clientName string
cacheIgnoredFiles bool clientVersion string
db *db.Lowlevel
protectedFiles []string protectedFiles []string
evLogger events.Logger evLogger events.Logger
// globalRequestLimiter limits the amount of data in concurrent incoming requests // constant or concurrency safe fields
finder *db.BlockFinder
progressEmitter *ProgressEmitter
shortID protocol.ShortID
cacheIgnoredFiles bool
// globalRequestLimiter limits the amount of data in concurrent incoming
// requests
globalRequestLimiter *byteSemaphore globalRequestLimiter *byteSemaphore
// folderIOLimiter limits the number of concurrent I/O heavy operations, // folderIOLimiter limits the number of concurrent I/O heavy operations,
// such as scans and pulls. A limit of zero means no limit. // such as scans and pulls.
folderIOLimiter *byteSemaphore folderIOLimiter *byteSemaphore
clientName string // fields protected by fmut
clientVersion string fmut sync.RWMutex
fmut sync.RWMutex // protects the below
folderCfgs map[string]config.FolderConfiguration // folder -> cfg folderCfgs map[string]config.FolderConfiguration // folder -> cfg
folderFiles map[string]*db.FileSet // folder -> files folderFiles map[string]*db.FileSet // folder -> files
deviceStatRefs map[protocol.DeviceID]*stats.DeviceStatisticsReference // deviceID -> statsRef deviceStatRefs map[protocol.DeviceID]*stats.DeviceStatisticsReference // deviceID -> statsRef
@ -140,7 +143,8 @@ type model struct {
folderRestartMuts syncMutexMap // folder -> restart mutex folderRestartMuts syncMutexMap // folder -> restart mutex
folderVersioners map[string]versioner.Versioner // folder -> versioner (may be nil) folderVersioners map[string]versioner.Versioner // folder -> versioner (may be nil)
pmut sync.RWMutex // protects the below // fields protected by pmut
pmut sync.RWMutex
conn map[protocol.DeviceID]connections.Connection conn map[protocol.DeviceID]connections.Connection
connRequestLimiters map[protocol.DeviceID]*byteSemaphore connRequestLimiters map[protocol.DeviceID]*byteSemaphore
closed map[protocol.DeviceID]chan struct{} closed map[protocol.DeviceID]chan struct{}
@ -183,19 +187,26 @@ func NewModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersio
}, },
PassThroughPanics: true, PassThroughPanics: true,
}), }),
// constructor parameters
cfg: cfg, cfg: cfg,
db: ldb,
finder: db.NewBlockFinder(ldb),
progressEmitter: NewProgressEmitter(cfg, evLogger),
id: id, id: id,
shortID: id.Short(),
cacheIgnoredFiles: cfg.Options().CacheIgnoredFiles,
protectedFiles: protectedFiles,
evLogger: evLogger,
globalRequestLimiter: newByteSemaphore(1024 * cfg.Options().MaxConcurrentIncomingRequestKiB()),
folderIOLimiter: newByteSemaphore(cfg.Options().MaxFolderConcurrency()),
clientName: clientName, clientName: clientName,
clientVersion: clientVersion, clientVersion: clientVersion,
db: ldb,
protectedFiles: protectedFiles,
evLogger: evLogger,
// constant or concurrency safe fields
finder: db.NewBlockFinder(ldb),
progressEmitter: NewProgressEmitter(cfg, evLogger),
shortID: id.Short(),
cacheIgnoredFiles: cfg.Options().CacheIgnoredFiles,
globalRequestLimiter: newByteSemaphore(1024 * cfg.Options().MaxConcurrentIncomingRequestKiB()),
folderIOLimiter: newByteSemaphore(cfg.Options().MaxFolderConcurrency()),
// fields protected by fmut
fmut: sync.NewRWMutex(),
folderCfgs: make(map[string]config.FolderConfiguration), folderCfgs: make(map[string]config.FolderConfiguration),
folderFiles: make(map[string]*db.FileSet), folderFiles: make(map[string]*db.FileSet),
deviceStatRefs: make(map[protocol.DeviceID]*stats.DeviceStatisticsReference), deviceStatRefs: make(map[protocol.DeviceID]*stats.DeviceStatisticsReference),
@ -203,14 +214,15 @@ func NewModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersio
folderRunners: make(map[string]service), folderRunners: make(map[string]service),
folderRunnerTokens: make(map[string][]suture.ServiceToken), folderRunnerTokens: make(map[string][]suture.ServiceToken),
folderVersioners: make(map[string]versioner.Versioner), folderVersioners: make(map[string]versioner.Versioner),
// fields protected by pmut
pmut: sync.NewRWMutex(),
conn: make(map[protocol.DeviceID]connections.Connection), conn: make(map[protocol.DeviceID]connections.Connection),
connRequestLimiters: make(map[protocol.DeviceID]*byteSemaphore), connRequestLimiters: make(map[protocol.DeviceID]*byteSemaphore),
closed: make(map[protocol.DeviceID]chan struct{}), closed: make(map[protocol.DeviceID]chan struct{}),
helloMessages: make(map[protocol.DeviceID]protocol.HelloResult), helloMessages: make(map[protocol.DeviceID]protocol.HelloResult),
deviceDownloads: make(map[protocol.DeviceID]*deviceDownloadState), deviceDownloads: make(map[protocol.DeviceID]*deviceDownloadState),
remotePausedFolders: make(map[protocol.DeviceID][]string), remotePausedFolders: make(map[protocol.DeviceID][]string),
fmut: sync.NewRWMutex(),
pmut: sync.NewRWMutex(),
} }
for devID := range cfg.Devices() { for devID := range cfg.Devices() {
m.deviceStatRefs[devID] = stats.NewDeviceStatisticsReference(m.db, devID.String()) m.deviceStatRefs[devID] = stats.NewDeviceStatisticsReference(m.db, devID.String())