public synchronized void addSession(Session session) {
    MemcachedSession tcpSession = (MemcachedSession) session;
    InetSocketAddressWrapper addrWrapper = tcpSession.getInetSocketAddressWrapper();

    InetSocketAddress mainNodeAddress = addrWrapper.getMainNodeAddress();
    if (mainNodeAddress != null) {
      // It is a standby session
      this.addStandbySession(session, mainNodeAddress);
    } else {
      // It is a main session
      this.addMainSession(session);
      // Update main sessions
      this.updateSessions();
    }
  }
  private void addMainSession(Session session) {
    InetSocketAddress remoteSocketAddress = session.getRemoteSocketAddress();
    log.warn(
        "Add a session: "
            + SystemUtils.getRawAddress(remoteSocketAddress)
            + ":"
            + remoteSocketAddress.getPort());
    Queue<Session> sessions = this.sessionMap.get(remoteSocketAddress);
    if (sessions == null) {
      sessions = new ConcurrentLinkedQueue<Session>();
      Queue<Session> oldSessions = this.sessionMap.putIfAbsent(remoteSocketAddress, sessions);
      if (null != oldSessions) {
        sessions = oldSessions;
      }
    }
    // If it is in failure mode,remove closed session from list
    if (this.failureMode) {
      Iterator<Session> it = sessions.iterator();
      while (it.hasNext()) {
        Session tmp = it.next();
        if (tmp.isClosed()) {
          it.remove();
          break;
        }
      }
    }

    sessions.offer(session);
    // Remove old session and close it
    while (sessions.size() > this.connectionPoolSize) {
      Session oldSession = sessions.poll();
      ((MemcachedSession) oldSession).setAllowReconnect(false);
      oldSession.close();
    }
  }
 private MemcachedSession findStandbySession(MemcachedSession session) {
   if (this.failureMode) {
     List<Session> sessionList =
         this.getStandbySessionListByMainNodeAddr(session.getRemoteSocketAddress());
     if (sessionList != null && !sessionList.isEmpty()) {
       return (MemcachedTCPSession) sessionList.get(this.random.nextInt(sessionList.size()));
     }
   }
   return session;
 }
 public synchronized void quitAllSessions() {
   for (Session session : this.sessionSet) {
     ((MemcachedSession) session).quit();
   }
   int sleepCount = 0;
   while (sleepCount++ < 5 && this.sessionSet.size() > 0) {
     try {
       this.wait(1000);
     } catch (InterruptedException e) {
       Thread.currentThread().interrupt();
     }
   }
 }
 public void send(final Command msg) throws MemcachedException {
   MemcachedSession session = (MemcachedSession) this.findSessionByKey(msg.getKey());
   if (session == null) {
     throw new MemcachedException("There is no available connection at this moment");
   }
   // If session was closed,try to use standby memcached node
   if (session.isClosed()) {
     session = this.findStandbySession(session);
   }
   if (session.isClosed()) {
     throw new MemcachedException(
         "Session("
             + SystemUtils.getRawAddress(session.getRemoteSocketAddress())
             + ":"
             + session.getRemoteSocketAddress().getPort()
             + ") has been closed");
   }
   if (session.isAuthFailed()) {
     throw new MemcachedException("Auth failed to connection " + session.getRemoteSocketAddress());
   }
   session.write(msg);
 }
 public void setBufferAllocator(BufferAllocator allocator) {
   this.bufferAllocator = allocator;
   for (Session session : this.getSessionSet()) {
     ((MemcachedSession) session).setBufferAllocator(allocator);
   }
 }