aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/jdisc/MaxPendingContentChannelOutputStream.java
blob: aec4eeecd7b9cc50177bb0c1635e3293047e896d (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc;

import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author baldersheim
 */
public class MaxPendingContentChannelOutputStream extends ContentChannelOutputStream {

    private final long maxPending;
    private final AtomicLong sent = new AtomicLong(0);
    private final AtomicLong acked = new AtomicLong(0);

    public MaxPendingContentChannelOutputStream(ContentChannel endpoint, long maxPending) {
        super(endpoint);
        this.maxPending = maxPending;
    }

    private long pendingBytes() {
        return sent.get() - acked.get();
    }

    private class TrackCompletion implements CompletionHandler {

        private final long written;
        private final AtomicBoolean replied = new AtomicBoolean(false);

        TrackCompletion(long written) {
            this.written = written;
            sent.addAndGet(written);
        }

        @Override
        public void completed() {
            if (!replied.getAndSet(true)) {
                acked.addAndGet(written);
            }
        }

        @Override
        public void failed(Throwable t) {
            if (!replied.getAndSet(true)) {
                acked.addAndGet(written);
            }
        }

    }

    @Override
    public void send(ByteBuffer src) throws IOException {
        try {
            stallWhilePendingAbove(maxPending);
        }
        catch (InterruptedException ignored) {
            throw new InterruptedIOException("Interrupted waiting for IO");
        }
        CompletionHandler pendingTracker = new TrackCompletion(src.remaining());
        try {
            send(src, pendingTracker);
        }
        catch (Throwable throwable) {
            pendingTracker.failed(throwable);
            throw throwable;
        }
    }

    private void stallWhilePendingAbove(long pending) throws InterruptedException {
        while (pendingBytes() > pending) {
            Thread.sleep(1);
        }
    }

    @Override
    public void flush() throws IOException {
        super.flush();
        try {
            stallWhilePendingAbove(0);
        }
        catch (InterruptedException e) {
            throw new InterruptedIOException("Interrupted waiting for IO");
        }
    }

}