jdk/src/java.httpclient/share/classes/java/net/http/WSSharedPool.java
author prappo
Mon, 09 May 2016 23:33:09 +0100
changeset 37874 02589df0999a
child 39730 196f4e25d9f5
permissions -rw-r--r--
8087113: Websocket API and implementation Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37874
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     1
/*
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     4
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General  License version 2 only, as
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    10
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General  License
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    15
 * accompanied this code).
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    16
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General  License version
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    20
 *
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    23
 * questions.
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    24
 */
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    25
package java.net.http;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    26
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    27
import java.nio.Buffer;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    28
import java.util.concurrent.BlockingQueue;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    29
import java.util.concurrent.LinkedBlockingQueue;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    30
import java.util.concurrent.atomic.AtomicInteger;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    31
import java.util.function.Supplier;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    32
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    33
import static java.lang.System.Logger.Level.TRACE;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    34
import static java.net.http.WSShared.duplicate;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    35
import static java.net.http.WSUtils.logger;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    36
import static java.util.Objects.requireNonNull;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    37
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    38
final class WSSharedPool<T extends Buffer> implements Supplier<WSShared<T>> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    39
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    40
    private final Supplier<T> factory;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    41
    private final BlockingQueue<T> queue;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    42
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    43
    WSSharedPool(Supplier<T> factory, int maxPoolSize) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    44
        this.factory = requireNonNull(factory);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    45
        this.queue = new LinkedBlockingQueue<>(maxPoolSize);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    46
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    47
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    48
    @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    49
    public Pooled get() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    50
        T b = queue.poll();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    51
        if (b == null) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    52
            logger.log(TRACE, "Pool {0} contains no free buffers", this);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    53
            b = requireNonNull(factory.get());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    54
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    55
        Pooled buf = new Pooled(new AtomicInteger(1), b, duplicate(b));
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    56
        logger.log(TRACE, "Pool {0} created new buffer {1}", this, buf);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    57
        return buf;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    58
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    59
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    60
    private void put(Pooled b) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    61
        assert b.disposed.get() && b.refCount.get() == 0
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    62
                : WSUtils.dump(b.disposed, b.refCount, b);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    63
        b.shared.clear();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    64
        boolean accepted = queue.offer(b.getShared());
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    65
        if (logger.isLoggable(TRACE)) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    66
            if (accepted) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    67
                logger.log(TRACE, "Pool {0} accepted {1}", this, b);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    68
            } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    69
                logger.log(TRACE, "Pool {0} discarded {1}", this, b);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    70
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    71
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    72
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    73
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    74
    @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    75
    public String toString() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    76
        return super.toString() + "[queue.size=" + queue.size() + "]";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    77
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    78
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    79
    private final class Pooled extends WSShared<T> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    80
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    81
        private final AtomicInteger refCount;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    82
        private final T shared;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    83
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    84
        private Pooled(AtomicInteger refCount, T shared, T region) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    85
            super(region);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    86
            this.refCount = refCount;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    87
            this.shared = shared;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    88
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    89
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    90
        private T getShared() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    91
            return shared;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    92
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    93
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    94
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    95
        @SuppressWarnings("unchecked")
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    96
        public Pooled share(final int pos, final int limit) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    97
            synchronized (this) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    98
                T buffer = buffer();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
    99
                checkRegion(pos, limit, buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   100
                final int oldPos = buffer.position();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   101
                final int oldLimit = buffer.limit();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   102
                select(pos, limit, buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   103
                T slice = WSShared.slice(buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   104
                select(oldPos, oldLimit, buffer);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   105
                referenceAndGetCount();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   106
                Pooled buf = new Pooled(refCount, shared, slice);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   107
                logger.log(TRACE, "Shared {0} from {1}", buf, this);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   108
                return buf;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   109
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   110
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   111
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   112
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   113
        public void dispose() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   114
            logger.log(TRACE, "Disposed {0}", this);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   115
            super.dispose();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   116
            if (dereferenceAndGetCount() == 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   117
                WSSharedPool.this.put(this);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   118
            }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   119
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   120
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   121
        private int referenceAndGetCount() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   122
            return refCount.updateAndGet(n -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   123
                if (n != Integer.MAX_VALUE) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   124
                    return n + 1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   125
                } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   126
                    throw new IllegalArgumentException
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   127
                            ("Too many references: " + this);
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   128
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   129
            });
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   130
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   131
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   132
        private int dereferenceAndGetCount() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   133
            return refCount.updateAndGet(n -> {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   134
                if (n > 0) {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   135
                    return n - 1;
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   136
                } else {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   137
                    throw new InternalError();
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   138
                }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   139
            });
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   140
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   141
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   142
        @Override
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   143
        public String toString() {
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   144
            return WSUtils.toStringSimple(this) + "[" + WSUtils.toString(buffer)
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   145
                    + "[refCount=" + refCount + ", disposed=" + disposed + "]]";
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   146
        }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   147
    }
02589df0999a 8087113: Websocket API and implementation
prappo
parents:
diff changeset
   148
}