chore(all): memory and thread safe storage
- settings: get filter choices from storage for settings validation - updater: update servers to the storage - storage: minimal deep copying and data duplication - storage: add merged servers mutex for thread safety - connection: filter servers in storage - formatter: format servers to Markdown in storage - PIA: get server by name from storage directly - Updater: get servers count from storage directly - Updater: equality check done in storage, fix #882
This commit is contained in:
@@ -19,7 +19,7 @@ type Updater struct {
|
||||
options settings.Updater
|
||||
|
||||
// state
|
||||
servers models.AllServers
|
||||
storage Storage
|
||||
|
||||
// Functions for tests
|
||||
logger Logger
|
||||
@@ -29,6 +29,12 @@ type Updater struct {
|
||||
unzipper unzip.Unzipper
|
||||
}
|
||||
|
||||
type Storage interface {
|
||||
SetServers(provider string, servers []models.Server) (err error)
|
||||
GetServersCount(provider string) (count int)
|
||||
ServersAreEqual(provider string, servers []models.Server) (equal bool)
|
||||
}
|
||||
|
||||
type Logger interface {
|
||||
Info(s string)
|
||||
Warn(s string)
|
||||
@@ -36,20 +42,20 @@ type Logger interface {
|
||||
}
|
||||
|
||||
func New(settings settings.Updater, httpClient *http.Client,
|
||||
currentServers models.AllServers, logger Logger) *Updater {
|
||||
storage Storage, logger Logger) *Updater {
|
||||
unzipper := unzip.New(httpClient)
|
||||
return &Updater{
|
||||
options: settings,
|
||||
storage: storage,
|
||||
logger: logger,
|
||||
timeNow: time.Now,
|
||||
presolver: resolver.NewParallelResolver(settings.DNSAddress.String()),
|
||||
client: httpClient,
|
||||
unzipper: unzipper,
|
||||
options: settings,
|
||||
servers: currentServers,
|
||||
}
|
||||
}
|
||||
|
||||
func (u *Updater) UpdateServers(ctx context.Context) (allServers models.AllServers, err error) {
|
||||
func (u *Updater) UpdateServers(ctx context.Context) (err error) {
|
||||
caser := cases.Title(language.English)
|
||||
for _, provider := range u.options.Providers {
|
||||
u.logger.Info("updating " + caser.String(provider) + " servers...")
|
||||
@@ -62,17 +68,17 @@ func (u *Updater) UpdateServers(ctx context.Context) (allServers models.AllServe
|
||||
|
||||
// return the only error for the single provider.
|
||||
if len(u.options.Providers) == 1 {
|
||||
return allServers, err
|
||||
return err
|
||||
}
|
||||
|
||||
// stop updating the next providers if context is canceled.
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return allServers, ctxErr
|
||||
return ctxErr
|
||||
}
|
||||
|
||||
// Log the error and continue updating the next provider.
|
||||
u.logger.Error(err.Error())
|
||||
}
|
||||
|
||||
return u.servers, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user