2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
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;
19 * A best-effort output limiter. It does not provide any fairness, and acts as a blocking gate-keeper
20 * for a sessions' channel.
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;
28 ChannelOutputLimiter(final BGPSessionImpl session) {
29 this.session = Preconditions.checkNotNull(session);
32 private void ensureWritable() {
34 LOG.trace("Blocked slow path tripped on session {}", session);
38 LOG.debug("Waiting for session {} to become writable", session);
40 } catch (InterruptedException e) {
41 throw new IllegalStateException("Interrupted while waiting for channel to come back", e);
45 LOG.debug("Resuming write on session {}", session);
50 void write(final Notification msg) {
55 void writeAndFlush(final Notification msg) {
57 session.writeAndFlush(msg);
65 public void channelWritabilityChanged(final ChannelHandlerContext ctx) throws Exception {
66 final boolean w = ctx.channel().isWritable();
70 LOG.debug("Writes on session {} {}", session, w ? "unblocked" : "blocked");
77 super.channelWritabilityChanged(ctx);
81 public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
87 super.channelInactive(ctx);