mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 23:56:56 +00:00
GODT-1932: frontend is instantiated before bridge.
WIP: introduced frontend.Type. WIP: frontend is create before bridge is instantiated. WIP: filtering of internet stastus event in gRPC event queue.
This commit is contained in:
@ -44,9 +44,9 @@ const (
|
||||
flagLogSMTP = "log-smtp"
|
||||
flagNonInteractive = "noninteractive"
|
||||
|
||||
// Memory cache was estimated by empirical usage in past and it was set to 100MB.
|
||||
// Memory cache was estimated by empirical usage in the past, and it was set to 100MB.
|
||||
// NOTE: This value must not be less than maximal size of one email (~30MB).
|
||||
inMemoryCacheLimnit = 100 * (1 << 20)
|
||||
inMemoryCacheLimit = 100 * (1 << 20)
|
||||
)
|
||||
|
||||
func New(base *base.Base) *cli.App {
|
||||
@ -63,7 +63,7 @@ func New(base *base.Base) *cli.App {
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: flagNonInteractive,
|
||||
Usage: "Start Bridge entirely noninteractively",
|
||||
Usage: "Start Bridge entirely non-interactively",
|
||||
},
|
||||
}...)
|
||||
|
||||
@ -71,6 +71,17 @@ func New(base *base.Base) *cli.App {
|
||||
}
|
||||
|
||||
func main(b *base.Base, c *cli.Context) error { //nolint:funlen
|
||||
frontendType := getFrontendTypeFromCLIParams(c)
|
||||
f := frontend.New(
|
||||
frontendType,
|
||||
!c.Bool(base.FlagNoWindow),
|
||||
b.CrashHandler,
|
||||
b.Listener,
|
||||
b.Updater,
|
||||
b,
|
||||
b.Locations,
|
||||
)
|
||||
|
||||
cache, cacheErr := loadMessageCache(b)
|
||||
if cacheErr != nil {
|
||||
logrus.WithError(cacheErr).Error("Could not load local cache.")
|
||||
@ -141,28 +152,10 @@ func main(b *base.Base, c *cli.Context) error { //nolint:funlen
|
||||
// We want cookies to be saved to disk so they are loaded the next time.
|
||||
b.AddTeardownAction(b.CookieJar.PersistCookies)
|
||||
|
||||
var frontendMode string
|
||||
|
||||
switch {
|
||||
case c.Bool(base.FlagCLI):
|
||||
frontendMode = "cli"
|
||||
case c.Bool(flagNonInteractive):
|
||||
return <-(make(chan error)) // Block forever.
|
||||
default:
|
||||
frontendMode = "grpc"
|
||||
if frontendType == frontend.NonInteractive {
|
||||
return <-(make(chan error))
|
||||
}
|
||||
|
||||
f := frontend.New(
|
||||
frontendMode,
|
||||
!c.Bool(base.FlagNoWindow),
|
||||
b.CrashHandler,
|
||||
b.Listener,
|
||||
b.Updater,
|
||||
bridge,
|
||||
b,
|
||||
b.Locations,
|
||||
)
|
||||
|
||||
// Watch for updates routine
|
||||
go func() {
|
||||
ticker := time.NewTicker(constants.UpdateCheckInterval)
|
||||
@ -173,7 +166,18 @@ func main(b *base.Base, c *cli.Context) error { //nolint:funlen
|
||||
}
|
||||
}()
|
||||
|
||||
return f.Loop()
|
||||
return f.Loop(bridge)
|
||||
}
|
||||
|
||||
func getFrontendTypeFromCLIParams(c *cli.Context) frontend.Type {
|
||||
switch {
|
||||
case c.Bool(base.FlagCLI):
|
||||
return frontend.CLI
|
||||
case c.Bool(flagNonInteractive):
|
||||
return frontend.NonInteractive
|
||||
default:
|
||||
return frontend.GRPC
|
||||
}
|
||||
}
|
||||
|
||||
func checkAndHandleUpdate(u types.Updater, f frontend.Frontend, autoUpdate bool) {
|
||||
@ -226,7 +230,7 @@ func checkAndHandleUpdate(u types.Updater, f frontend.Frontend, autoUpdate bool)
|
||||
// local cache is enabled but unavailable (in-memory cache will be returned nevertheless).
|
||||
func loadMessageCache(b *base.Base) (cache.Cache, error) {
|
||||
if !b.Settings.GetBool(settings.CacheEnabledKey) {
|
||||
return cache.NewInMemoryCache(inMemoryCacheLimnit), nil
|
||||
return cache.NewInMemoryCache(inMemoryCacheLimit), nil
|
||||
}
|
||||
|
||||
var compressor cache.Compressor
|
||||
@ -246,12 +250,12 @@ func loadMessageCache(b *base.Base) (cache.Cache, error) {
|
||||
path = customPath
|
||||
} else {
|
||||
path = b.Cache.GetDefaultMessageCacheDir()
|
||||
// Store path so it will allways persist if default location
|
||||
// Store path so it will always persist if default location
|
||||
// will be changed in new version.
|
||||
b.Settings.Set(settings.CacheLocationKey, path)
|
||||
}
|
||||
|
||||
// To prevent memory peaks we set maximal write concurency for store
|
||||
// To prevent memory peaks we set maximal write concurrency for store
|
||||
// build jobs.
|
||||
store.SetBuildAndCacheJobLimit(b.Settings.GetInt(settings.CacheConcurrencyWrite))
|
||||
|
||||
@ -262,7 +266,7 @@ func loadMessageCache(b *base.Base) (cache.Cache, error) {
|
||||
ConcurrentWrite: b.Settings.GetInt(settings.CacheConcurrencyWrite),
|
||||
})
|
||||
if err != nil {
|
||||
return cache.NewInMemoryCache(inMemoryCacheLimnit), err
|
||||
return cache.NewInMemoryCache(inMemoryCacheLimit), err
|
||||
}
|
||||
|
||||
return messageCache, nil
|
||||
|
||||
Reference in New Issue
Block a user