View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.ssl;
17  
18  import java.nio.ByteBuffer;
19  
20  import javax.net.ssl.SSLEngine;
21  
22  /**
23   * A {@link ByteBuffer} pool dedicated for {@link SslHandler} performance
24   * improvement.
25   * <p>
26   * In most cases, you won't need to create a new pool instance because
27   * {@link SslHandler} has a default pool instance internally.
28   * <p>
29   * The reason why {@link SslHandler} requires a buffer pool is because the
30   * current {@link SSLEngine} implementation always requires a 17KiB buffer for
31   * every 'wrap' and 'unwrap' operation.  In most cases, the actual size of the
32   * required buffer is much smaller than that, and therefore allocating a 17KiB
33   * buffer for every 'wrap' and 'unwrap' operation wastes a lot of memory
34   * bandwidth, resulting in the application performance degradation.
35   *
36   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
37   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
38   *
39   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
40   */
41  public class SslBufferPool {
42  
43      // Add 1024 as a room for compressed data and another 1024 for Apache Harmony compatibility.
44      private static final int MAX_PACKET_SIZE = 16665 + 2048;
45      private static final int DEFAULT_POOL_SIZE = MAX_PACKET_SIZE * 1024;
46  
47      private final ByteBuffer[] pool;
48      private final int maxBufferCount;
49      private int index;
50  
51      /**
52       * Creates a new buffer pool whose size is {@code 18113536}, which can
53       * hold {@code 1024} buffers.
54       */
55      public SslBufferPool() {
56          this(DEFAULT_POOL_SIZE);
57      }
58  
59      /**
60       * Creates a new buffer pool.
61       *
62       * @param maxPoolSize the maximum number of bytes that this pool can hold
63       */
64      public SslBufferPool(int maxPoolSize) {
65          if (maxPoolSize <= 0) {
66              throw new IllegalArgumentException("maxPoolSize: " + maxPoolSize);
67          }
68  
69          int maxBufferCount = maxPoolSize / MAX_PACKET_SIZE;
70          if (maxPoolSize % MAX_PACKET_SIZE != 0) {
71              maxBufferCount ++;
72          }
73  
74          pool = new ByteBuffer[maxBufferCount];
75          this.maxBufferCount = maxBufferCount;
76      }
77  
78      /**
79       * Returns the maximum size of this pool in byte unit.  The returned value
80       * can be somewhat different from what was specified in the constructor.
81       */
82      public int getMaxPoolSize() {
83          return maxBufferCount * MAX_PACKET_SIZE;
84      }
85  
86      /**
87       * Returns the number of bytes which were allocated but have not been
88       * acquired yet.  You can estimate how optimal the specified maximum pool
89       * size is from this value.  If it keeps returning {@code 0}, it means the
90       * pool is getting exhausted.  If it keeps returns a unnecessarily big
91       * value, it means the pool is wasting the heap space.
92       */
93      public synchronized int getUnacquiredPoolSize() {
94          return index * MAX_PACKET_SIZE;
95      }
96  
97      synchronized ByteBuffer acquire() {
98          if (index == 0) {
99              return ByteBuffer.allocate(MAX_PACKET_SIZE);
100         } else {
101             return (ByteBuffer) pool[-- index].clear();
102         }
103     }
104 
105     synchronized void release(ByteBuffer buffer) {
106         if (index < maxBufferCount) {
107             pool[index ++] = buffer;
108         }
109     }
110 }