src/java.net.http/share/classes/jdk/internal/net/http/common/SubscriptionBase.java
author dfuchs
Fri, 06 Apr 2018 22:11:44 +0100
branchhttp-client-branch
changeset 56399 a0929d5dd63f
parent 56264 c012b93297b0
child 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     1
/*
56088
38fac6d0521d http-client-branch: update copyright year ranges in prep for file moves
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     4
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    10
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    15
 * accompanied this code).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    16
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    20
 *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    23
 * questions.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    24
 */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    25
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    26
package jdk.internal.net.http.common;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    27
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    28
import java.util.concurrent.Flow;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicBoolean;
56399
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    30
import java.util.function.Consumer;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    31
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    32
/**
56264
c012b93297b0 http-client-branch: filtering ordering
chegar
parents: 56092
diff changeset
    33
 * Maintains subscription counter and provides primitives for:
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    34
 * - accessing window
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    35
 * - reducing window when delivering items externally
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    36
 * - resume delivery when window was zero previously
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    37
 */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    38
public class SubscriptionBase implements Flow.Subscription {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    39
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    40
    final Demand demand = new Demand();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    41
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    42
    final SequentialScheduler scheduler; // when window was zero and is opened, run this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    43
    final Runnable cancelAction; // when subscription cancelled, run this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    44
    final AtomicBoolean cancelled;
56399
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    45
    final Consumer<Throwable> onError;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    46
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    47
    public SubscriptionBase(SequentialScheduler scheduler, Runnable cancelAction) {
56399
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    48
        this(scheduler, cancelAction, null);
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    49
    }
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    50
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    51
    public SubscriptionBase(SequentialScheduler scheduler,
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    52
                            Runnable cancelAction,
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    53
                            Consumer<Throwable> onError) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    54
        this.scheduler = scheduler;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    55
        this.cancelAction = cancelAction;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    56
        this.cancelled = new AtomicBoolean(false);
56399
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    57
        this.onError = onError;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    58
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    59
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    60
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    61
    public void request(long n) {
56399
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    62
        try {
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    63
            if (demand.increase(n))
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    64
                scheduler.runOrSchedule();
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    65
        } catch(Throwable t) {
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    66
            if (onError != null) {
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    67
                if (cancelled.getAndSet(true))
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    68
                    return;
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    69
                onError.accept(t);
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    70
            } else throw t;
a0929d5dd63f http-client-branch: review comment - invalid Subscription::request arguments should be relayed to Subscriber::onError (part II: HTTP/2)
dfuchs
parents: 56264
diff changeset
    71
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    72
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    73
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    74
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    75
    public synchronized String toString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    76
        return "SubscriptionBase: window = " + demand.get() +
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    77
                " cancelled = " + cancelled.toString();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    78
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    79
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    80
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    81
     * Returns true if the window was reduced by 1. In that case
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    82
     * items must be supplied to subscribers and the scheduler run
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    83
     * externally. If the window could not be reduced by 1, then false
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    84
     * is returned and the scheduler will run later when the window is updated.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    85
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    86
    public boolean tryDecrement() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    87
        return demand.tryDecrement();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    88
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    89
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    90
    public long window() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    91
        return demand.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    92
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    93
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    94
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    95
    public void cancel() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    96
        if (cancelled.getAndSet(true))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    97
            return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    98
        scheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
    99
        cancelAction.run();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   100
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents:
diff changeset
   101
}