Merge "BUG-2283: introduce OutputLimiter"
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ChannelOutputLimiter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.ChannelHandlerContext;
12 import io.netty.channel.ChannelInboundHandlerAdapter;
13 import javax.annotation.concurrent.ThreadSafe;
14 import org.opendaylight.yangtools.yang.binding.Notification;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * A best-effort output limiter. It does not provide any fairness, and acts as a blocking gate-keeper
20  * for a sessions' channel.
21  */
22 @ThreadSafe
23 final class ChannelOutputLimiter extends ChannelInboundHandlerAdapter {
24     private static final Logger LOG = LoggerFactory.getLogger(ChannelOutputLimiter.class);
25     private final BGPSessionImpl session;
26     private volatile boolean blocked;
27
28     ChannelOutputLimiter(final BGPSessionImpl session) {
29         this.session = Preconditions.checkNotNull(session);
30     }
31
32     void write(final Notification msg) {
33         if (blocked) {
34             LOG.trace("Blocked slow path tripped on session {}", session);
35             synchronized (this) {
36                 while (blocked) {
37                     try {
38                         LOG.debug("Waiting for session {} to become writable", session);
39                         this.wait();
40                     } catch (InterruptedException e) {
41                         throw new IllegalStateException("Interrupted while waiting for channel to come back", e);
42                     }
43                 }
44
45                 LOG.debug("Resuming write on session {}", session);
46             }
47         }
48
49         session.sendMessage(msg);
50     }
51
52     void flush() {
53         // FIXME: no-op, as we do not have hatching APIs in session yet
54     }
55
56     @Override
57     public void channelWritabilityChanged(final ChannelHandlerContext ctx) throws Exception {
58         final boolean w = ctx.channel().isWritable();
59
60         synchronized (this) {
61             blocked = !w;
62             LOG.debug("Writes on session {} {}", session, w ? "unblocked" : "blocked");
63
64             if (w) {
65                 this.notifyAll();
66             }
67         }
68
69         super.channelWritabilityChanged(ctx);
70     }
71
72     @Override
73     public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
74         synchronized (this) {
75             blocked = false;
76             this.notifyAll();
77         }
78
79         super.channelInactive(ctx);
80     }
81 }