Skip to content

Commit

Permalink
Smarter support for request scope.
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagolvlsantos committed May 6, 2024
1 parent 1fb72c4 commit b64558b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@
@Slf4j
public abstract class AbstractGitTransaction implements IGitTransaction {

private static final String TRANSACTION_LEVEL = "gtlevel";
private AtomicInteger counter = new AtomicInteger(0);
private Map<Integer, String> levels = new ConcurrentHashMap<>();
protected static final String TRANSACTION_LEVEL = "gtlevel";
protected AtomicInteger counter = new AtomicInteger(0);
protected Map<Integer, String> levels = new ConcurrentHashMap<>();

@Override
public void beginTransaction(ApplicationContext context, IGitAnnotation annotation)
throws GitTransactionsException {
try {
synchronized (counter) {
setGitTransactionLevel();
log.debug("");
log.debug("** @@@@@@@@@@@@@@ START: {} >>>>>>>>>>>>>>>>>>> ", annotation.value());
log.debug("push({}): {}", counter.intValue(), annotation.value());
log.debug("** @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
log.debug("** @@@@@@@@@@@@@@ START: {}", annotation.value());
counter.incrementAndGet();
context.getBean(IGitProvider.class).init(annotation);
}
Expand All @@ -57,12 +57,10 @@ public void endTransaction(ApplicationContext context, IGitAnnotation annotation
try {
synchronized (counter) {
int current = counter.decrementAndGet();
if (current == 0) {
context.getBean(IGitProvider.class).clean(annotation);
}
endOnCounter(context, annotation, current);
log.debug("pop({}): {}", current, annotation);
log.debug("** @@@@@@@@@@@@@@ END: {}", annotation.value());
log.debug("** @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
log.debug("** <<<<<<<<<<<<<<<<<< END: {} @@@@@@@@@@@@@@", annotation.value());
log.debug("");
setGitTransactionLevel();
}
} catch (BeansException | GitAPIException e) {
Expand All @@ -74,4 +72,11 @@ public void endTransaction(ApplicationContext context, IGitAnnotation annotation
MDC.remove(TRANSACTION_LEVEL);
}
}

protected void endOnCounter(ApplicationContext context, IGitAnnotation annotation, int current)
throws GitAPIException {
if (current == 0) {
context.getBean(IGitProvider.class).clean(annotation);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package io.github.thiagolvlsantos.git.transactions.impl.web;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import io.github.thiagolvlsantos.git.transactions.IGitAnnotation;
import io.github.thiagolvlsantos.git.transactions.config.GitConstants;
import io.github.thiagolvlsantos.git.transactions.impl.AbstractGitTransaction;

@Component
@Profile(GitConstants.PROFILE_WEB)
@Scope(GitConstants.SCOPE_REQUEST)
public class GitTransactionWeb extends AbstractGitTransaction {

@Override
protected void endOnCounter(ApplicationContext context, IGitAnnotation annotation, int current)
throws GitAPIException {
// do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;

import io.github.thiagolvlsantos.git.transactions.IGitAnnotation;
import io.github.thiagolvlsantos.git.transactions.config.GitConfiguration;
Expand Down Expand Up @@ -376,12 +375,13 @@ public void cleanWrite(String group) throws GitAPIException {
public void clean(IGitAnnotation annotation) throws GitAPIException {
long time = System.currentTimeMillis();

String group = annotation != null ? annotation.value() : "na";
if (!this.gitInstances.isEmpty()) {
for (Map.Entry<File, Git> entry : this.gitInstances.entrySet()) {
Git git = entry.getValue();
git.close();
}
log.debug("close gits({}) = {}", annotation.value(), this.gitInstances.keySet());
log.debug("close gits({}) = {}", group, this.gitInstances.keySet());
}
this.gitInstances.clear();

Expand All @@ -392,7 +392,7 @@ public void clean(IGitAnnotation annotation) throws GitAPIException {
for (Map.Entry<Git, File> entry : this.pulledWrites.entrySet()) {
cleanDirectory(entry.getValue());
}
log.debug("clean writes({}) = {}", annotation.value(), this.pulledWrites.values());
log.debug("clean writes({}) = {}", group, this.pulledWrites.values());
}
this.pulledWrites.clear();
this.gitWrites.clear();
Expand All @@ -401,11 +401,11 @@ public void clean(IGitAnnotation annotation) throws GitAPIException {
for (Map.Entry<String, File> entry : this.gitCommits.entrySet()) {
cleanDirectory(entry.getValue());
}
log.debug("clean commits({}) = {}", annotation.value(), this.gitCommits.keySet());
log.debug("clean commits({}) = {}", group, this.gitCommits.keySet());
}
this.gitCommits.clear();

log.info("clean({}) time={}", annotation.value(), System.currentTimeMillis() - time);
log.info("clean({}) time={}", group, System.currentTimeMillis() - time);
}

protected void cleanDirectory(File dir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.thiagolvlsantos.git.transactions.provider.impl.web;

import javax.annotation.PreDestroy;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
Expand All @@ -11,4 +14,9 @@
@Profile(GitConstants.PROFILE_WEB)
@Scope(GitConstants.SCOPE_REQUEST)
public class GitProviderWeb extends AbstractGitProvider {

@PreDestroy
public void doClean() throws GitAPIException {
super.clean(null); // remove transaction objects on destroy
}
}

0 comments on commit b64558b

Please sign in to comment.