Skip to content

Commit

Permalink
Implement better user hashmap handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabricio20 committed Sep 26, 2024
1 parent e4ae31a commit 714ab2c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,45 @@ jobs:
with:
distribution: 'zulu'
java-version: 17
- name: Fetch version
run: |
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
echo VERSION=$VERSION
echo "RELEASE=$VERSION" >> $GITHUB_ENV
echo "release=$VERSION" > ./bot/src/main/resources/sentry.properties
- name: Setup gradle
uses: gradle/gradle-build-action@v2
- name: Build with gradle
run: ./gradlew build
env:
USERNAME: ${{ github.actor }}
TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Docker Build
run: docker build ./bot/ --file ./bot/Dockerfile --tag $IMAGE_NAME
- name: Log into registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Push image
run: |
IMAGE_ID=ghcr.io/$IMAGE_NAME
# Change all uppercase to lowercase
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
echo IMAGE_ID=$IMAGE_ID
docker tag $IMAGE_NAME $IMAGE_ID:latest
echo "Pushing images"
docker push $IMAGE_ID:latest
- name: Create Sentry release
uses: getsentry/action-release@v1
env:
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
with:
version: ${{ env.RELEASE }}
23 changes: 12 additions & 11 deletions bot/src/main/java/net/lindseybot/bot/listener/UserNameListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class UserNameListener extends ListenerAdapter implements ExpirationListener<Long, String> {

private final ProfileServiceImpl profiles;
private final ExpiringMap<Long, String> users = ExpiringMap.builder()
.expirationPolicy(ExpirationPolicy.CREATED)
.expiration(1, TimeUnit.MINUTES)
.expirationListener(this)
.maxSize(15_000)
.expiration(5, TimeUnit.MINUTES)
.asyncExpirationListener(this)
.maxSize(50_000)
.build();
private final Queue<UserUpdate> queue = new ArrayDeque<>();
private final AtomicInteger tracker = new AtomicInteger(0);
private final Queue<UserUpdate> queue = new ConcurrentLinkedQueue<>();

public UserNameListener(IEventManager api, ProfileServiceImpl profiles) {
this.profiles = profiles;
Expand All @@ -35,20 +37,19 @@ public UserNameListener(IEventManager api, ProfileServiceImpl profiles) {

@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
long userId = event.getAuthor().getIdLong();
if (users.containsKey(userId)) {
return;
}
users.put(userId, event.getAuthor().getAsTag());
var author = event.getAuthor();
users.put(author.getIdLong(), author.getAsTag());
}

@Override
public void expired(Long userId, String name) {
this.queue.add(new UserUpdate(userId, name));
if (queue.size() > 250) {
int size = tracker.incrementAndGet();
if (size > 250) {
List<UserUpdate> updates = queue.stream()
.limit(250).toList();
this.profiles.updateNames(updates);
tracker.set(0);
}
}

Expand Down

0 comments on commit 714ab2c

Please sign in to comment.