Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emitters sending unnecessarily #1

Open
channyeintun opened this issue May 12, 2022 · 0 comments
Open

Emitters sending unnecessarily #1

channyeintun opened this issue May 12, 2022 · 0 comments

Comments

@channyeintun
Copy link

List emitters = new ArrayList<>();
//...
emitters.forEach(emitter -> {
try {
emitter.send(SseEmitter
.event()
.name(username)
.data(payload));

        } catch (IOException e) {
            deadEmitters.add(emitter);
        }
    });

Above code is not efficient. You should use Map instead of List.

// Should use map
private Map<String, SseEmitter> emitters = new ConcurrentHashMap<>();
// Store in map
emitters.put(username,emitter);
// Get from map
SseEmitter emitter=emitters.get(username);
// you can validate if emitter is null
try {

            emitter.send(SseEmitter
                    .event()
                    .name(username)
                    .data(payload));

        } catch (IOException e) {
             // remove from map
            emitters.remove(username);

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant