GODT-1351: Cache and update of space bytes in user object.

This commit is contained in:
Jakub
2021-09-21 14:54:15 +02:00
parent 41e15db442
commit 2899e7bb15
27 changed files with 243 additions and 49 deletions

View File

@ -49,6 +49,8 @@ type User struct {
userID string
creds *credentials.Credentials
usedBytes, totalBytes int64
lock sync.RWMutex
}
@ -122,6 +124,8 @@ func (u *User) connect(client pmapi.Client, creds *credentials.Credentials) erro
u.store.StartWatcher()
}
u.UpdateSpace(nil)
return nil
}
@ -201,6 +205,40 @@ func (u *User) ID() string {
return u.userID
}
// UsedBytes returns number of bytes used on server.
func (u *User) UsedBytes() int64 {
return u.usedBytes
}
// TotalBytes returns number of bytes available on server.
func (u *User) TotalBytes() int64 {
return u.totalBytes
}
// UpdateSpace will update TotalBytes and UsedBytes values from API user. If
// pointer is nill it will get fresh user from API. API user can come from
// update event which means it doesn't contain all data. Therefore only
// positive values will be updated.
func (u *User) UpdateSpace(apiUser *pmapi.User) {
// If missing get latest pmapi.User from API instead of using cached
// values from client.CurrentUser()
if apiUser == nil {
var err error
apiUser, err = u.client.GetUser(context.Background())
if err != nil {
u.log.WithError(err).Warning("Cannot update user space")
return
}
}
if apiUser.UsedSpace != nil {
u.usedBytes = *apiUser.UsedSpace
}
if apiUser.MaxSpace != nil {
u.totalBytes = *apiUser.MaxSpace
}
}
// Username returns the user's username as found in the user's credentials.
func (u *User) Username() string {
u.lock.RLock()
@ -351,7 +389,7 @@ func (u *User) UpdateUser(ctx context.Context) error {
defer u.lock.Unlock()
defer u.listener.Emit(events.UserRefreshEvent, u.userID)
_, err := u.client.UpdateUser(ctx)
user, err := u.client.UpdateUser(ctx)
if err != nil {
return err
}
@ -367,6 +405,8 @@ func (u *User) UpdateUser(ctx context.Context) error {
u.creds = creds
u.UpdateSpace(user)
return nil
}

View File

@ -36,7 +36,7 @@ func TestUpdateUser(t *testing.T) {
defer cleanUpUserData(user)
gomock.InOrder(
m.pmapiClient.EXPECT().UpdateUser(gomock.Any()).Return(nil, nil),
m.pmapiClient.EXPECT().UpdateUser(gomock.Any()).Return(testPMAPIUser, nil),
m.pmapiClient.EXPECT().ReloadKeys(gomock.Any(), testCredentials.MailboxPassword).Return(nil),
m.pmapiClient.EXPECT().Addresses().Return([]*pmapi.Address{testPMAPIAddress}),

View File

@ -115,6 +115,10 @@ func (u *Users) watchEvents() {
log.WithError(err).Error("Failed to load store after reconnecting")
}
}
if user.totalBytes == 0 {
user.UpdateSpace(nil)
}
}
}
}

View File

@ -116,9 +116,14 @@ var (
IsCombinedAddressMode: false,
}
usedSpace = int64(1048576)
maxSpace = int64(10485760)
testPMAPIUser = &pmapi.User{ //nolint[gochecknoglobals]
ID: "user",
Name: "username",
ID: "user",
Name: "username",
UsedSpace: &usedSpace,
MaxSpace: &maxSpace,
}
testPMAPIUserDisconnected = &pmapi.User{ //nolint[gochecknoglobals]
@ -297,6 +302,7 @@ func mockInitConnectedUser(t *testing.T, m mocks) {
// Mock of user initialisation.
m.pmapiClient.EXPECT().AddAuthRefreshHandler(gomock.Any())
m.pmapiClient.EXPECT().IsUnlocked().Return(true).AnyTimes()
m.pmapiClient.EXPECT().GetUser(gomock.Any()).Return(testPMAPIUser, nil) // load connected user
// Mock of store initialisation.
gomock.InOrder(