Changeset 409 in code for trunk/upstream.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/upstream.go

    r407 r409  
    709709                                ch.Members[newNick] = memberships
    710710                                uc.appendLog(ch.Name, msg)
    711                                 uc.appendHistory(ch.Name, msg)
    712711                        }
    713712                }
     
    819818
    820819                                uc.appendLog(ch.Name, msg)
    821                                 uc.appendHistory(ch.Name, msg)
    822820                        }
    823821                }
     
    16121610}
    16131611
    1614 // TODO: handle moving logs when a network name changes, when support for this is added
    16151612func (uc *upstreamConn) appendLog(entity string, msg *irc.Message) {
    16161613        if uc.srv.LogPath == "" {
     
    16241621        }
    16251622
    1626         if _, err := ml.Append(msg); err != nil {
    1627                 uc.logger.Printf("failed to log message: %v", err)
    1628         }
    1629 }
    1630 
    1631 // appendHistory appends a message to the history. entity can be empty.
    1632 func (uc *upstreamConn) appendHistory(entity string, msg *irc.Message) {
    16331623        detached := false
    16341624        if ch, ok := uc.network.channels[entity]; ok {
     
    16381628        history, ok := uc.network.history[entity]
    16391629        if !ok {
     1630                lastID, err := lastMsgID(uc.network, entity, time.Now())
     1631                if err != nil {
     1632                        uc.logger.Printf("failed to log message: failed to get last message ID: %v", err)
     1633                        return
     1634                }
     1635
    16401636                history = &networkHistory{
    1641                         clients: make(map[string]uint64),
    1642                         ring:    NewRing(uc.srv.RingCap),
     1637                        clients: make(map[string]string),
    16431638                }
    16441639                uc.network.history[entity] = history
    16451640
    16461641                for clientName, _ := range uc.network.offlineClients {
    1647                         history.clients[clientName] = 0
     1642                        history.clients[clientName] = lastID
    16481643                }
    16491644
     
    16521647                        // clients too
    16531648                        uc.forEachDownstream(func(dc *downstreamConn) {
    1654                                 history.clients[dc.clientName] = 0
    1655                         })
    1656                 }
    1657         }
    1658 
    1659         history.ring.Produce(msg)
     1649                                history.clients[dc.clientName] = lastID
     1650                        })
     1651                }
     1652        }
     1653
     1654        msgID, err := ml.Append(msg)
     1655        if err != nil {
     1656                uc.logger.Printf("failed to log message: %v", err)
     1657                return
     1658        }
    16601659
    16611660        if !detached {
    16621661                uc.forEachDownstream(func(dc *downstreamConn) {
    1663                         history.clients[dc.clientName] = history.ring.Cur()
    1664                 })
    1665         }
    1666 }
    1667 
    1668 // produce appends a message to the logs, adds it to the history and forwards
    1669 // it to connected downstream connections.
     1662                        history.clients[dc.clientName] = msgID
     1663                })
     1664        }
     1665}
     1666
     1667// produce appends a message to the logs and forwards it to connected downstream
     1668// connections.
    16701669//
    16711670// If origin is not nil and origin doesn't support echo-message, the message is
     
    16751674                uc.appendLog(target, msg)
    16761675        }
    1677 
    1678         uc.appendHistory(target, msg)
    16791676
    16801677        // Don't forward messages if it's a detached channel
Note: See TracChangeset for help on using the changeset viewer.