You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by cs...@apache.org on 2018/02/28 19:45:19 UTC

[incubator-openwhisk] branch master updated: Remove usages of replaceAll from the hot-path. (#3357)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 91778eb  Remove usages of replaceAll from the hot-path. (#3357)
91778eb is described below

commit 91778eb36e2cea98b8cf47c5246e0eceaeef046c
Author: Markus Thömmes <ma...@me.com>
AuthorDate: Wed Feb 28 20:45:16 2018 +0100

    Remove usages of replaceAll from the hot-path. (#3357)
---
 .../scala/whisk/core/containerpool/ContainerFactory.scala     |  7 ++++---
 .../main/scala/whisk/core/containerpool/ContainerProxy.scala  | 11 ++++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/common/scala/src/main/scala/whisk/core/containerpool/ContainerFactory.scala b/common/scala/src/main/scala/whisk/core/containerpool/ContainerFactory.scala
index 25be520..fd19495 100644
--- a/common/scala/src/main/scala/whisk/core/containerpool/ContainerFactory.scala
+++ b/common/scala/src/main/scala/whisk/core/containerpool/ContainerFactory.scala
@@ -52,11 +52,12 @@ trait ContainerFactory {
 
 object ContainerFactory {
 
+  /** based on https://github.com/moby/moby/issues/3138 and https://github.com/moby/moby/blob/master/daemon/names/names.go */
+  private def isAllowed(c: Char) = c.isLetterOrDigit || c == '_' || c == '.' || c == '-'
+
   /** include the instance name, if specified and strip invalid chars before attempting to use them in the container name */
   def containerNamePrefix(instanceId: InstanceId): String =
-    s"wsk${instanceId.name.getOrElse("")}${instanceId.toInt}"
-      .replaceAll("[^a-zA-Z0-9_\\.\\-]", "") // based on https://github.com/moby/moby/issues/3138 and https://github.com/moby/moby/blob/master/daemon/names/names.go
-
+    s"wsk${instanceId.name.getOrElse("")}${instanceId.toInt}".filter(isAllowed)
 }
 
 /**
diff --git a/core/invoker/src/main/scala/whisk/core/containerpool/ContainerProxy.scala b/core/invoker/src/main/scala/whisk/core/containerpool/ContainerProxy.scala
index 7f37aaf..d50e07c 100644
--- a/core/invoker/src/main/scala/whisk/core/containerpool/ContainerProxy.scala
+++ b/core/invoker/src/main/scala/whisk/core/containerpool/ContainerProxy.scala
@@ -427,9 +427,14 @@ object ContainerProxy {
    * @param suffix the container name's suffix
    * @return a unique container name
    */
-  def containerName(instance: InstanceId, prefix: String, suffix: String) =
-    s"${ContainerFactory.containerNamePrefix(instance)}_${containerCount.next()}_${prefix}_${suffix}"
-      .replaceAll("[^a-zA-Z0-9_]", "")
+  def containerName(instance: InstanceId, prefix: String, suffix: String): String = {
+    def isAllowed(c: Char): Boolean = c.isLetterOrDigit || c == '_'
+
+    val sanitizedPrefix = prefix.filter(isAllowed)
+    val sanitizedSuffix = suffix.filter(isAllowed)
+
+    s"${ContainerFactory.containerNamePrefix(instance)}_${containerCount.next()}_${sanitizedPrefix}_${sanitizedSuffix}"
+  }
 
   /**
    * Creates a WhiskActivation ready to be sent via active ack.

-- 
To stop receiving notification emails like this one, please contact
csantanapr@apache.org.