|
| 1 | +/* |
| 2 | + * Copyright 2023 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * 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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.navercorp.pinpoint.web.realtime.atc.websocket; |
| 17 | + |
| 18 | +import com.navercorp.pinpoint.web.task.TimerTaskDecorator; |
| 19 | +import com.navercorp.pinpoint.web.task.TimerTaskDecoratorFactory; |
| 20 | +import org.springframework.web.socket.WebSocketSession; |
| 21 | +import reactor.core.Disposable; |
| 22 | + |
| 23 | +import java.util.concurrent.locks.Lock; |
| 24 | +import java.util.concurrent.locks.ReentrantLock; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author youngjin.kim2 |
| 29 | + */ |
| 30 | +final class ATCSessionContext { |
| 31 | + |
| 32 | + private static final String KEY_ATTR = "atcAttrs"; |
| 33 | + |
| 34 | + private final TimerTaskDecorator taskDecorator; |
| 35 | + |
| 36 | + private final Lock lock = new ReentrantLock(); |
| 37 | + private final long sessionCreatedAt = System.currentTimeMillis(); |
| 38 | + private Disposable subscription = null; |
| 39 | + |
| 40 | + private ATCSessionContext(TimerTaskDecorator taskDecorator) { |
| 41 | + this.taskDecorator = taskDecorator; |
| 42 | + } |
| 43 | + |
| 44 | + static void newContext(WebSocketSession session, TimerTaskDecoratorFactory taskDecoratorFactory) { |
| 45 | + final ATCSessionContext ctx = new ATCSessionContext(getTimerTaskDecorator(taskDecoratorFactory)); |
| 46 | + session.getAttributes().put(KEY_ATTR, ctx); |
| 47 | + } |
| 48 | + |
| 49 | + private static TimerTaskDecorator getTimerTaskDecorator(TimerTaskDecoratorFactory taskDecoratorFactory) { |
| 50 | + if (taskDecoratorFactory == null) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + return taskDecoratorFactory.createTimerTaskDecorator(); |
| 54 | + } |
| 55 | + |
| 56 | + static void runWithLockedContext(WebSocketSession session, Consumer<ATCSessionContext> consumer) { |
| 57 | + final ATCSessionContext ctx = get(session); |
| 58 | + if (ctx != null) { |
| 59 | + final Lock lock = ctx.lock; |
| 60 | + lock.lock(); |
| 61 | + consumer.accept(ctx); |
| 62 | + lock.unlock(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static void dispose(WebSocketSession session) { |
| 67 | + runWithLockedContext(session, ctx -> { |
| 68 | + final Disposable subscription = ctx.getSubscription(); |
| 69 | + if (subscription != null) { |
| 70 | + subscription.dispose(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + long getSessionCreatedAt() { |
| 76 | + return sessionCreatedAt; |
| 77 | + } |
| 78 | + |
| 79 | + TimerTaskDecorator getTaskDecorator() { |
| 80 | + return taskDecorator; |
| 81 | + } |
| 82 | + |
| 83 | + private Disposable getSubscription() { |
| 84 | + return subscription; |
| 85 | + } |
| 86 | + |
| 87 | + void setSubscription(Disposable newSubscription) { |
| 88 | + final Disposable prevSubscription = this.subscription; |
| 89 | + this.subscription = newSubscription; |
| 90 | + if (prevSubscription != null) { |
| 91 | + prevSubscription.dispose(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + static ATCSessionContext get(WebSocketSession session) { |
| 96 | + return (ATCSessionContext) session.getAttributes().get(KEY_ATTR); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments