aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/Connector.java
blob: abdb1a7165f44403ba1bf36e8a2629028c6744fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;

import com.yahoo.concurrent.CachedThreadPoolWithFallback;

import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

class Connector implements AutoCloseable {

    private static final Object globalLock = new Object();
    private static CachedThreadPoolWithFallback globalExecutor = null;
    private static long usages = 0;

    private static CachedThreadPoolWithFallback acquire() {
        synchronized (globalLock) {
            if (globalExecutor == null) {
                globalExecutor = new CachedThreadPoolWithFallback("jrt.connector", 1, 64, 1L, TimeUnit.SECONDS);
            }
            usages++;
            return globalExecutor;
        }
    }

    private static void release(CachedThreadPoolWithFallback executor) {
        synchronized (globalLock) {
            assert executor == globalExecutor;
            usages--;
            if (usages == 0) {
                globalExecutor.close();
                globalExecutor = null;
            }
        }
    }

    private final AtomicReference<CachedThreadPoolWithFallback> executor;

    Connector() {
        executor = new AtomicReference<>(acquire());
    }

    private void connect(Connection conn) {
        conn.transportThread().addConnection(conn.connect());
    }

    public void connectLater(Connection conn) {
        Executor executor = this.executor.get();
        if (executor != null) {
            try {
                executor.execute(() -> connect(conn));
                return;
            } catch (RejectedExecutionException ignored) {
            }
        }
        conn.transportThread().addConnection(conn);
    }

    @Override
    public void close() {
        CachedThreadPoolWithFallback toShutdown = executor.getAndSet(null);
        if (toShutdown != null) {
            release(toShutdown);
        }
    }
}