mirror of
https://github.com/wahyd4/pinpoint.git
synced 2026-08-16 08:16:15 +10:00
Merge pull request #589 from emeroad/udpreceiver_refactoring
refactoring collector
This commit is contained in:
+15
-2
@@ -23,11 +23,24 @@ import java.nio.ByteBuffer;
|
||||
*/
|
||||
public class ByteBufferFactory implements ObjectPoolFactory<ByteBuffer> {
|
||||
|
||||
private static final int AcceptedSize = 65507;
|
||||
public static final int UDP_MAX_PACKET_LENGTH = 65507;
|
||||
|
||||
private final int bufferLength;
|
||||
|
||||
public ByteBufferFactory() {
|
||||
this(UDP_MAX_PACKET_LENGTH);
|
||||
}
|
||||
|
||||
public ByteBufferFactory(int bufferLength) {
|
||||
if (bufferLength < 0 ) {
|
||||
throw new IllegalArgumentException("negative bufferLength:" + bufferLength);
|
||||
}
|
||||
this.bufferLength = bufferLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer create() {
|
||||
return ByteBuffer.allocate(AcceptedSize);
|
||||
return ByteBuffer.allocate(bufferLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+16
-3
@@ -23,16 +23,29 @@ import java.net.DatagramPacket;
|
||||
*/
|
||||
public class DatagramPacketFactory implements ObjectPoolFactory<DatagramPacket> {
|
||||
|
||||
private static final int AcceptedSize = 65507;
|
||||
public static final int UDP_MAX_PACKET_LENGTH = 65507;
|
||||
|
||||
private final int bufferLength;
|
||||
|
||||
public DatagramPacketFactory() {
|
||||
this(UDP_MAX_PACKET_LENGTH);
|
||||
}
|
||||
|
||||
public DatagramPacketFactory(int bufferLength) {
|
||||
if (bufferLength < 0 ) {
|
||||
throw new IllegalArgumentException("negative bufferLength:" + bufferLength);
|
||||
}
|
||||
this.bufferLength = bufferLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DatagramPacket create() {
|
||||
byte[] bytes = new byte[AcceptedSize];
|
||||
byte[] bytes = new byte[bufferLength];
|
||||
return new DatagramPacket(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeReturn(DatagramPacket packet) {
|
||||
packet.setLength(AcceptedSize);
|
||||
packet.setLength(bufferLength);
|
||||
}
|
||||
}
|
||||
|
||||
+12
-8
@@ -39,14 +39,14 @@ public class DefaultObjectPool<T> implements ObjectPool<T> {
|
||||
|
||||
private void fill(int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
PooledObjectWrapper<T> wrapper = createObject();
|
||||
PooledObjectWrapper wrapper = createObject();
|
||||
queue.offer(wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
private PooledObjectWrapper<T> createObject() {
|
||||
private PooledObjectWrapper createObject() {
|
||||
T t = this.factory.create();
|
||||
return new PooledObjectWrapper<T>(t);
|
||||
return new PooledObjectWrapper(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +60,7 @@ public class DefaultObjectPool<T> implements ObjectPool<T> {
|
||||
}
|
||||
|
||||
|
||||
public void returnObject(PooledObject<T> t) {
|
||||
private void returnObject(PooledObject<T> t) {
|
||||
if (t == null) {
|
||||
return;
|
||||
}
|
||||
@@ -68,10 +68,14 @@ public class DefaultObjectPool<T> implements ObjectPool<T> {
|
||||
queue.offer(t);
|
||||
}
|
||||
|
||||
private class PooledObjectWrapper<V extends T > implements PooledObject<T> {
|
||||
private final V value;
|
||||
public int size() {
|
||||
return queue.size();
|
||||
}
|
||||
|
||||
public PooledObjectWrapper(V value) {
|
||||
private class PooledObjectWrapper implements PooledObject<T> {
|
||||
private final T value;
|
||||
|
||||
public PooledObjectWrapper(T value) {
|
||||
if (value == null) {
|
||||
throw new NullPointerException("value must not be null");
|
||||
}
|
||||
@@ -79,7 +83,7 @@ public class DefaultObjectPool<T> implements ObjectPool<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getObject() {
|
||||
public T getObject() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2014 NAVER Corp.
|
||||
*
|
||||
* 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 com.navercorp.pinpoint.collector.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author emeroad
|
||||
*/
|
||||
public class DatagramPacketFactoryTest {
|
||||
|
||||
@Test
|
||||
public void testCreate() throws Exception {
|
||||
int bufferLength = 10;
|
||||
DatagramPacketFactory factory = new DatagramPacketFactory(bufferLength);
|
||||
DatagramPacket packet = factory.create();
|
||||
Assert.assertEquals(bufferLength, packet.getLength());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeforeReturn() throws Exception {
|
||||
int bufferLength = 10;
|
||||
DatagramPacketFactory factory = new DatagramPacketFactory(bufferLength);
|
||||
DatagramPacket packet = factory.create();
|
||||
|
||||
packet.setLength(1);
|
||||
factory.beforeReturn(packet);
|
||||
Assert.assertEquals(bufferLength, packet.getLength());
|
||||
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2014 NAVER Corp.
|
||||
*
|
||||
* 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 com.navercorp.pinpoint.collector.util;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author emeroad
|
||||
*/
|
||||
public class DefaultObjectPoolTest {
|
||||
|
||||
@Test
|
||||
public void testGetObject() throws Exception {
|
||||
DefaultObjectPool<DatagramPacket> pool = new DefaultObjectPool<DatagramPacket>(new DatagramPacketFactory(), 1);
|
||||
|
||||
PooledObject<DatagramPacket> pooledObject = pool.getObject();
|
||||
Assert.assertEquals(0, pool.size());
|
||||
|
||||
pooledObject.returnObject();
|
||||
Assert.assertEquals(1, pool.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReset() throws Exception {
|
||||
DefaultObjectPool<DatagramPacket> pool = new DefaultObjectPool<DatagramPacket>(new DatagramPacketFactory(), 1);
|
||||
|
||||
PooledObject<DatagramPacket> pooledObject = pool.getObject();
|
||||
DatagramPacket packet = pooledObject.getObject();
|
||||
|
||||
packet.setLength(10);
|
||||
|
||||
pooledObject.returnObject();
|
||||
|
||||
DatagramPacket check = pooledObject.getObject();
|
||||
|
||||
Assert.assertSame(check, packet);
|
||||
Assert.assertEquals(packet.getLength(), DatagramPacketFactory.UDP_MAX_PACKET_LENGTH);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user