Changeset 409 in code for trunk/downstream.go


Ignore:
Timestamp:
Aug 20, 2020, 6:05:01 PM (5 years ago)
Author:
contact
Message:

Nuke in-memory ring buffer

Instead, always read chat history from logs. Unify the implicit chat
history (pushing history to clients) and explicit chat history
(via the CHATHISTORY command).

Instead of keeping track of ring buffer cursors for each client, use
message IDs.

If necessary, the ring buffer could be re-introduced behind a
common MessageStore interface (could be useful when on-disk logs are
disabled).

References: https://todo.sr.ht/~emersion/soju/80

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r406 r409  
    851851                        delete(net.offlineClients, dc.clientName)
    852852                }
     853
     854                // Fast-forward history to last message
     855                for target, history := range net.history {
     856                        if ch, ok := net.channels[target]; ok && ch.Detached {
     857                                continue
     858                        }
     859
     860                        lastID, err := lastMsgID(net, target, time.Now())
     861                        if err != nil {
     862                                dc.logger.Printf("failed to get last message ID: %v", err)
     863                                continue
     864                        }
     865                        history.clients[dc.clientName] = lastID
     866                }
    853867        })
    854868
     
    857871
    858872func (dc *downstreamConn) sendNetworkHistory(net *network) {
    859         if dc.caps["draft/chathistory"] {
     873        if dc.caps["draft/chathistory"] || dc.srv.LogPath == "" {
    860874                return
    861875        }
     
    865879                }
    866880
    867                 seq, ok := history.clients[dc.clientName]
     881                lastDelivered, ok := history.clients[dc.clientName]
    868882                if !ok {
    869883                        continue
    870884                }
    871                 history.clients[dc.clientName] = history.ring.Cur()
    872 
    873                 consumer := history.ring.NewConsumer(seq)
     885
     886                limit := 4000
     887                history, err := loadHistoryLatestID(dc.network, target, lastDelivered, limit)
     888                if err != nil {
     889                        dc.logger.Printf("failed to send implicit history for %q: %v", target, err)
     890                        continue
     891                }
    874892
    875893                batchRef := "history"
     
    882900                }
    883901
    884                 for {
    885                         msg := consumer.Consume()
    886                         if msg == nil {
    887                                 break
    888                         }
    889 
     902                for _, msg := range history {
    890903                        // Don't replay all messages, because that would mess up client
    891904                        // state. For instance we just sent the list of users, sending
     
    901914
    902915                        if dc.caps["batch"] {
    903                                 msg = msg.Copy()
    904916                                msg.Tags["batch"] = irc.TagValue(batchRef)
    905917                        }
    906 
    907918                        dc.SendMessage(dc.marshalMessage(msg, net))
    908919                }
Note: See TracChangeset for help on using the changeset viewer.