Skip to content

Commit

Permalink
Make only default IdleStateEvents cached string representation (netty…
Browse files Browse the repository at this point in the history
…#9705)

Motivation:

In PR netty#9695   IdleStateEvents
were made to cache their string representation.   The reason for this
was to avoid creating garbage as these values would be used frequently.
However, these objects may be used on multiple event loops and this
may cause an unexpected race to occur.

Modification:
Only make the events that Netty creates cache their toString representation.

Result:
No races.
  • Loading branch information
carl-mastrangelo authored and normanmaurer committed Oct 24, 2019
1 parent 39cc7a6 commit ff9df03
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions handler/src/main/java/io/netty/handler/timeout/IdleStateEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
* A user event triggered by {@link IdleStateHandler} when a {@link Channel} is idle.
*/
public class IdleStateEvent {
public static final IdleStateEvent FIRST_READER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.READER_IDLE, true);
public static final IdleStateEvent READER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.READER_IDLE, false);
public static final IdleStateEvent FIRST_WRITER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.WRITER_IDLE, true);
public static final IdleStateEvent WRITER_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.WRITER_IDLE, false);
public static final IdleStateEvent FIRST_ALL_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.ALL_IDLE, true);
public static final IdleStateEvent ALL_IDLE_STATE_EVENT = new IdleStateEvent(IdleState.ALL_IDLE, false);
public static final IdleStateEvent FIRST_READER_IDLE_STATE_EVENT =
new DefaultIdleStateEvent(IdleState.READER_IDLE, true);
public static final IdleStateEvent READER_IDLE_STATE_EVENT =
new DefaultIdleStateEvent(IdleState.READER_IDLE, false);
public static final IdleStateEvent FIRST_WRITER_IDLE_STATE_EVENT =
new DefaultIdleStateEvent(IdleState.WRITER_IDLE, true);
public static final IdleStateEvent WRITER_IDLE_STATE_EVENT =
new DefaultIdleStateEvent(IdleState.WRITER_IDLE, false);
public static final IdleStateEvent FIRST_ALL_IDLE_STATE_EVENT =
new DefaultIdleStateEvent(IdleState.ALL_IDLE, true);
public static final IdleStateEvent ALL_IDLE_STATE_EVENT =
new DefaultIdleStateEvent(IdleState.ALL_IDLE, false);

private final IdleState state;
private final boolean first;
private String strVal;

/**
* Constructor for sub-classes.
Expand Down Expand Up @@ -61,9 +66,20 @@ public boolean isFirst() {

@Override
public String toString() {
if (strVal == null) {
strVal = StringUtil.simpleClassName(this) + "(" + state + (first ? ", first" : "") + ')';
return StringUtil.simpleClassName(this) + '(' + state + (first ? ", first" : "") + ')';
}

private static final class DefaultIdleStateEvent extends IdleStateEvent {
private final String representation;

DefaultIdleStateEvent(IdleState state, boolean first) {
super(state, first);
this.representation = "IdleStateEvent(" + state + (first ? ", first" : "") + ')';
}

@Override
public String toString() {
return representation;
}
return strVal;
}
}

0 comments on commit ff9df03

Please sign in to comment.