-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tracing] reduce locking section for accumulating span collector
- Loading branch information
1 parent
7012fa5
commit 107dcd4
Showing
2 changed files
with
74 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tracing/src/main/java/io/yupiik/uship/tracing/collector/Buffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2021-2023 - Yupiik SAS - https://www.yupiik.com | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package io.yupiik.uship.tracing.collector; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class Buffer<T> { | ||
private final Map<T, T> buffer = new ConcurrentHashMap<>(); | ||
private final AtomicInteger size = new AtomicInteger(); | ||
|
||
public void add(final T item) { | ||
buffer.put(item, item); | ||
size.incrementAndGet(); | ||
} | ||
|
||
public int size() { | ||
return size.get(); // faster than ConcurrentHashMap#size | ||
} | ||
|
||
// always called from a single thread | ||
public Collection<T> drain() { | ||
final var all = new ArrayList<>(buffer.keySet()); | ||
size.addAndGet(-all.size()); | ||
buffer.clear(); | ||
return all; | ||
} | ||
} |