wsky / top-push

Message-push abstraction component, provide useful messaging components.
8 stars 4 forks source link

message loose while statebuildIdle shorter than sender #38

Closed wsky closed 11 years ago

wsky commented 11 years ago

in LRU flush

protected void SendMessage(CancellationToken token, Object message) {
        // FIFO queue for LRU load-balance
        while (true) {
            if (token.isCancelling()) {
                onDrop(message, "canceled");
                return;
            }

            ClientConnection connection = connectionQueue.poll();
            if (connection == null) {
                onDrop(message, "no valid connection");
                return;
            }

            if (!connection.isOpen()) {
                this.RemoveConnection(connection);
                this.onDisconnect(connection);
                this.logger.info("connection#%s[%s] is closed, remove it",
                        connection.getId(),
                        connection.getOrigin());
                continue;
            }

            try {
                // here maybe have many exception, use statusCode instead
                if (!connection.sendMessage(message))
                    return;
            } catch (Exception e) {
                onDrop(message, "send message error");
                this.logger.error("send message error", e);
                return; // only send once
            } finally {
                connectionQueue.add(connection);
            }

            this.onSent(message);
            return;
        }
    }

sad that if client was in concurrent,

ClientConnection connection = connectionQueue.poll(); 

maybe got null, then message drop

wsky commented 11 years ago

if client not changed to be thread safe, LRU queue must be a copy

Queue<ClientConnection> connectionQueue=new ConcurrentLinkedQueue<ClientConnection>(this.connections);

or just sync but bad perf

protected synchronized void SendMessage(CancellationToken token, Object message) {}