Skip to content

Commit

Permalink
add option to not close store when cached store expires
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenMassaro committed Dec 19, 2023
1 parent dea04eb commit e380002
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Note that if the account you have specified in Bitwarden does not end in "gmail.

### Optional configuration settings:

- `messageProcessingTimeoutSeconds`: number of seconds that should be spent processing each individual message before cancelling attempt to obtain message
- `messageProcessingTimeoutSeconds` (default `60`): number of seconds that should be spent processing each individual message before cancelling attempt to obtain message
- `closeStoreWhenCacheExpires` (default `true`): whether the `close` method should be called on an IMAP store when it expires from the cache. This was added because it seems that particularly slow IMAP providers (like gmail, sometimes), get `FolderClosedExceptions` when a `close` call occurs concurrently with opening a new store.

## Steps to get Bitwarden IDs

Expand Down
38 changes: 21 additions & 17 deletions src/main/java/email/service/ImapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,33 @@ public class ImapService {
private final MessageService messageService;
private final BitwardenService bitwardenService;
private final int messageProcessingTimeoutSeconds;
Cache<String, Store> storeCache = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.removalListener(new RemovalListener<String, Store>() {
@Override
public void onRemoval(RemovalNotification<String, Store> notification) {
try {
if (notification.getValue() != null) {
log.debug("{} - Closing store", notification.getKey());
notification.getValue().close();
}
} catch (MessagingException e) {
log.warn("{} - Failed to close expired store", notification.getKey(), e);
}
}
})
.build();
private final Cache<String, Store> storeCache;

public ImapService(MessageService messageService,
BitwardenService bitwardenService,
@Value("${messageProcessingTimeoutSeconds:60}") int messageProcessingTimeoutSeconds) {
@Value("${messageProcessingTimeoutSeconds:60}") int messageProcessingTimeoutSeconds,
@Value("${closeStoreWhenCacheExpires:true}") boolean shouldCloseStoreWhenCacheExpires) {
this.messageService = messageService;
this.bitwardenService = bitwardenService;
this.messageProcessingTimeoutSeconds = messageProcessingTimeoutSeconds;
storeCache = CacheBuilder.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES)
.removalListener((RemovalListener<String, Store>) notification -> {
if (shouldCloseStoreWhenCacheExpires) {
try {
if (notification.getValue() != null) {
log.debug("{} - Closing expired store", notification.getKey());
notification.getValue().close();
}
} catch (MessagingException e) {
log.warn("{} - Failed to close expired store", notification.getKey(), e);
}
} else {
log.trace("{} - Not closing expired store because closeStoreWhenCacheExpires is false", notification.getKey());
}

})
.build();
}

public List<Message> getInboxMessages(String hostname, int port, String username, String decryptedPassword, List<Message> existingMessages, UUID accountBitwardenId) throws Exception {
Expand Down

0 comments on commit e380002

Please sign in to comment.