Skip to content

Commit

Permalink
feat: 引擎等MQ场景接入SCS框架 #7443 接入websocket和auth服务
Browse files Browse the repository at this point in the history
  • Loading branch information
royalhuang committed Oct 14, 2022
1 parent dee8eeb commit df42c69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,26 @@ package com.tencent.devops.websocket.handler
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_USER_ID
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.websocket.utils.RedisUtlis
import com.tencent.devops.websocket.servcie.WebsocketService
import com.tencent.devops.websocket.keys.WebsocketKeys
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.server.ServerHttpRequest
import org.springframework.http.server.ServerHttpResponse
import org.springframework.http.server.ServletServerHttpRequest
import org.springframework.stereotype.Component
import org.springframework.web.socket.WebSocketHandler
import org.springframework.web.socket.server.HandshakeInterceptor
import java.util.concurrent.TimeUnit

@Component
class BKHandshakeInterceptor @Autowired constructor(
val redisOperation: RedisOperation,
val websocketService: WebsocketService
val redisOperation: RedisOperation
) : HandshakeInterceptor {

@Value("\${session.timeout:5}")
private val sessionTimeOut: Long? = null

companion object {
private val logger = LoggerFactory.getLogger(BKHandshakeInterceptor::class.java)
}
Expand All @@ -66,11 +71,28 @@ class BKHandshakeInterceptor @Autowired constructor(
userId
)}"
)
websocketService.createTimeoutSession(sessionId, userId)
createTimeoutSession(sessionId, userId)
}
}
}

private fun createTimeoutSession(sessionId: String, userId: String) {
val timeout = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(sessionTimeOut!!)
val redisData = "$sessionId#$userId&$timeout"
// hash后对1000取模,将数据打散到1000个桶内
var bucket = redisData.hashCode().rem(WebsocketKeys.REDIS_MO)
if (bucket < 0) bucket *= -1
val redisHashKey = WebsocketKeys.HASH_USER_TIMEOUT_REDIS_KEY + bucket
logger.info("redis hash sessionId[$sessionId] userId[$userId] redisHashKey[$redisHashKey]")
var timeoutData = redisOperation.get(redisHashKey)
if (timeoutData == null) {
redisOperation.set(redisHashKey, redisData, null, true)
} else {
timeoutData = "$timeoutData,$redisData"
redisOperation.set(redisHashKey, timeoutData, null, true)
}
}

override fun beforeHandshake(
request: ServerHttpRequest,
response: ServerHttpResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.common.redis.RedisLock
import com.tencent.devops.common.redis.RedisOperation
import com.tencent.devops.common.websocket.dispatch.TransferDispatch
import com.tencent.devops.websocket.keys.WebsocketKeys
import com.tencent.devops.common.websocket.utils.RedisUtlis
import com.tencent.devops.websocket.event.ChangePageTransferEvent
import com.tencent.devops.websocket.event.ClearSessionEvent
Expand All @@ -42,7 +41,6 @@ import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.util.concurrent.TimeUnit

@Service
@Suppress("ALL")
Expand All @@ -58,9 +56,6 @@ class WebsocketService @Autowired constructor(
@Value("\${transferData:false}")
private val needTransfer: Boolean = false

@Value("\${session.timeout:5}")
private val sessionTimeOut: Long? = null

@Value("\${session.maxCount:50}")
private val cacheMaxSession: Int? = null

Expand Down Expand Up @@ -237,23 +232,6 @@ class WebsocketService @Autowired constructor(
longSessionList.clear()
}

fun createTimeoutSession(sessionId: String, userId: String) {
val timeout = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(sessionTimeOut!!)
val redisData = "$sessionId#$userId&$timeout"
// hash后对1000取模,将数据打散到1000个桶内
var bucket = redisData.hashCode().rem(WebsocketKeys.REDIS_MO)
if (bucket < 0) bucket *= -1
val redisHashKey = WebsocketKeys.HASH_USER_TIMEOUT_REDIS_KEY + bucket
logger.info("redis hash sessionId[$sessionId] userId[$userId] redisHashKey[$redisHashKey]")
var timeoutData = redisOperation.get(redisHashKey)
if (timeoutData == null) {
redisOperation.set(redisHashKey, redisData, null, true)
} else {
timeoutData = "$timeoutData,$redisData"
redisOperation.set(redisHashKey, timeoutData, null, true)
}
}

fun getMaxSession(): Int? {
return cacheMaxSession
}
Expand Down

0 comments on commit df42c69

Please sign in to comment.