You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by se...@apache.org on 2018/01/08 08:30:35 UTC

[incubator-servicecomb-saga] 03/04: SCB-174 retrieve kryo instance from pool

This is an automated email from the ASF dual-hosted git repository.

seanyinx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-saga.git

commit 390271d76f7f20ccab120851d7522b82d4c277fa
Author: Eric Lee <da...@huawei.com>
AuthorDate: Sun Jan 7 16:11:48 2018 +0800

    SCB-174 retrieve kryo instance from pool
    
    Signed-off-by: Eric Lee <da...@huawei.com>
---
 .../servicecomb/saga/omega/format/KryoMessageFormat.java | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/omega/omega-format/src/main/java/org/apache/servicecomb/saga/omega/format/KryoMessageFormat.java b/omega/omega-format/src/main/java/org/apache/servicecomb/saga/omega/format/KryoMessageFormat.java
index 7ef16a2..f948963 100644
--- a/omega/omega-format/src/main/java/org/apache/servicecomb/saga/omega/format/KryoMessageFormat.java
+++ b/omega/omega-format/src/main/java/org/apache/servicecomb/saga/omega/format/KryoMessageFormat.java
@@ -25,17 +25,24 @@ import com.esotericsoftware.kryo.Kryo;
 import com.esotericsoftware.kryo.KryoException;
 import com.esotericsoftware.kryo.io.Input;
 import com.esotericsoftware.kryo.io.Output;
+import com.esotericsoftware.kryo.pool.KryoFactory;
+import com.esotericsoftware.kryo.pool.KryoPool;
 
 public class KryoMessageFormat implements MessageFormat {
 
   private static final int DEFAULT_BUFFER_SIZE = 4096;
 
-  private static final Kryo kryo = new Kryo();
+  private static final KryoFactory factory = Kryo::new;
+
+  private static final KryoPool pool = new KryoPool.Builder(factory).softReferences().build();
 
   @Override
   public byte[] serialize(Object[] objects) {
     Output output = new Output(DEFAULT_BUFFER_SIZE, -1);
+
+    Kryo kryo = pool.borrow();
     kryo.writeObjectOrNull(output, objects, Object[].class);
+    pool.release(kryo);
 
     return output.toBytes();
   }
@@ -44,7 +51,12 @@ public class KryoMessageFormat implements MessageFormat {
   public Object[] deserialize(byte[] message) {
     try {
       Input input = new Input(new ByteArrayInputStream(message));
-      return kryo.readObjectOrNull(input, Object[].class);
+
+      Kryo kryo = pool.borrow();
+      Object[] objects = kryo.readObjectOrNull(input, Object[].class);
+      pool.release(kryo);
+
+      return objects;
     } catch (KryoException e) {
       throw new OmegaException("Unable to deserialize message", e);
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.