wForget commented on code in PR #2364:
URL: https://github.com/apache/incubator-kyuubi/pull/2364#discussion_r851104715


##########
kyuubi-server/src/main/scala/org/apache/kyuubi/session/SessionLimiter.scala:
##########
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.kyuubi.session
+
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.atomic.AtomicInteger
+
+import org.apache.commons.lang3.StringUtils
+
+import org.apache.kyuubi.KyuubiSQLException
+
+trait SessionLimiter {
+  def increment(userIpAddress: UserIpAddress): Unit
+  def decrement(userIpAddress: UserIpAddress): Unit
+}
+
+case class UserIpAddress(user: String, ipAddress: String)
+
+class SessionLimiterImpl(userLimit: Int, ipAddressLimit: Int, 
userIpAddressLimit: Int)
+  extends SessionLimiter {
+
+  private val _counters: java.util.Map[String, AtomicInteger] =
+    new ConcurrentHashMap[String, AtomicInteger]()
+
+  private[session] def counters(): java.util.Map[String, AtomicInteger] = 
_counters
+
+  override def increment(userIpAddress: UserIpAddress): Unit = {
+    val user = userIpAddress.user
+    val ipAddress = userIpAddress.ipAddress
+    // increment userIpAddress count
+    if (ipAddressLimit > 0 && StringUtils.isNotBlank(user) && 
StringUtils.isNotBlank(ipAddress)) {
+      incrLimitCount(
+        s"$user:$ipAddress",
+        userIpAddressLimit,
+        s"Connection limit per user:ipaddress reached" +
+          s" (user:ipaddress: $user:$ipAddress limit: $userIpAddressLimit)")
+    }
+    // increment user count
+    if (userLimit > 0 && StringUtils.isNotBlank(user)) {
+      incrLimitCount(
+        user,
+        userLimit,
+        s"Connection limit per user reached (user: $user limit: $userLimit)")
+    }
+    // increment ipAddress count
+    if (ipAddressLimit > 0 && StringUtils.isNotBlank(ipAddress)) {
+      incrLimitCount(
+        ipAddress,
+        ipAddressLimit,
+        s"Connection limit per ipaddress reached (ipaddress: $ipAddress limit: 
$ipAddressLimit)")
+    }
+  }
+
+  override def decrement(userIpAddress: UserIpAddress): Unit = {
+    val user = userIpAddress.user
+    val ipAddress = userIpAddress.ipAddress
+    // decrement user count
+    if (userLimit > 0 && StringUtils.isNotBlank(user)) {
+      decrLimitCount(user)
+    }
+    // decrement ipAddress count
+    if (ipAddressLimit > 0 && StringUtils.isNotBlank(ipAddress)) {
+      decrLimitCount(ipAddress)
+    }
+    // decrement userIpAddress count
+    if (ipAddressLimit > 0 && StringUtils.isNotBlank(user) && 
StringUtils.isNotBlank(ipAddress)) {
+      decrLimitCount(s"$user:$ipAddress")
+    }
+  }
+
+  private def incrLimitCount(key: String, limit: Int, errorMsg: String): Unit 
= {
+    val count = _counters.computeIfAbsent(key, _ => new AtomicInteger())
+    if (count.incrementAndGet() > limit) {
+      count.decrementAndGet()

Review Comment:
   > shall we syncronized the whole method ? we should make sure the `increment 
and decrement` atomic
   
   I don't think it's necessary, we just need to make sure that increment and 
comparison operations are atomic.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to