Skip to content

Commit

Permalink
[#noissue] Memory optimization for PingSession
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Aug 14, 2024
1 parent 04ec094 commit 945189f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@
import com.navercorp.pinpoint.grpc.Header;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

/**
* @author Woonduk Kang(emeroad)
* @author jaehong.kim
*/
public class PingSession {
private static final AtomicLongFieldUpdater<PingSession> UPDATER = AtomicLongFieldUpdater.newUpdater(PingSession.class, "eventIdAllocator");

private final Long id;
private final Header header;
private final AtomicLong eventIdAllocator;

private volatile long eventIdAllocator = 0;

private short serviceType = ServiceType.UNDEFINED.getCode();
private boolean updated = false;
private long lastPingTimeMillis;

public PingSession(Long id, Header header) {
this.id = Objects.requireNonNull(id, "transportMetadata");
this.header = Objects.requireNonNull(header, "header");
this.eventIdAllocator = new AtomicLong();
}

public Header getHeader() {
Expand All @@ -49,7 +52,7 @@ public Long getId() {
}

public long nextEventIdAllocator() {
return eventIdAllocator.incrementAndGet();
return UPDATER.incrementAndGet(this);
}

public short getServiceType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ void getServiceType_undefined() {
session.setServiceType(ServiceType.SPRING.getCode());
Assertions.assertEquals(ServiceType.SPRING.getCode(), session.getServiceType());
}

@Test
void nextEventIdAllocator() {
Header header = new Header("name", "agentId", "agentName", "appName",
ServiceType.SPRING.getCode(), 11, 22, Collections.emptyList());

PingSession session = new PingSession(1L, header);

Assertions.assertEquals(1, session.nextEventIdAllocator());
Assertions.assertEquals(2, session.nextEventIdAllocator());

}

}

0 comments on commit 945189f

Please sign in to comment.