BUG-5790: BGP Test tool
[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.ChannelFuture;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelInboundHandlerAdapter;
14 import javax.annotation.concurrent.ThreadSafe;
15 import org.opendaylight.yangtools.yang.binding.Notification;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * A best-effort output limiter. It does not provide any fairness, and acts as a blocking gate-keeper
21  * for a sessions' channel.
22  */
23 @ThreadSafe
24 public final class ChannelOutputLimiter extends ChannelInboundHandlerAdapter {
25     private static final Logger LOG = LoggerFactory.getLogger(ChannelOutputLimiter.class);
26     private final BGPSessionImpl session;
27     private volatile boolean blocked;
28
29     ChannelOutputLimiter(final BGPSessionImpl session) {
30         this.session = Preconditions.checkNotNull(session);
31     }
32
33     private void ensureWritable() {
34         if (this.blocked) {
35             LOG.trace("Blocked slow path tripped on session {}", this.session);
36             synchronized (this) {
37                 while (this.blocked) {
38                     try {
39                         LOG.debug("Waiting for session {} to become writable", this.session);
40                         this.wait();
41                     } catch (final InterruptedException e) {
42                         throw new IllegalStateException("Interrupted while waiting for channel to come back", e);
43                     }
44                 }
45
46                 LOG.debug("Resuming write on session {}", this.session);
47             }
48         }
49     }
50
51     public void write(final Notification msg) {
52         ensureWritable();
53         this.session.write(msg);
54     }
55
56     ChannelFuture writeAndFlush(final Notification msg) {
57         ensureWritable();
58         return this.session.writeAndFlush(msg);
59     }
60
61     public void flush() {
62         this.session.flush();
63     }
64
65     @Override
66     public void channelWritabilityChanged(final ChannelHandlerContext ctx) throws Exception {
67         final boolean w = ctx.channel().isWritable();
68
69         synchronized (this) {
70             this.blocked = !w;
71             LOG.debug("Writes on session {} {}", this.session, w ? "unblocked" : "blocked");
72
73             if (w) {
74                 this.notifyAll();
75             } else {
76                 flush();
77             }
78         }
79
80         super.channelWritabilityChanged(ctx);
81     }
82
83     @Override
84     public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
85         synchronized (this) {
86             this.blocked = false;
87             this.notifyAll();
88         }
89
90         super.channelInactive(ctx);
91     }
92 }