Changeset 667 in code


Ignore:
Timestamp:
Nov 3, 2021, 5:21:05 PM (4 years ago)
Author:
contact
Message:

msgstore: add context to messageStore methods

This allows setting a hard timeout.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r666 r667  
    13291329        ch := net.channels.Value(target)
    13301330
     1331        ctx, cancel := context.WithTimeout(context.TODO(), messageStoreTimeout)
     1332        defer cancel()
     1333
    13311334        limit := 4000
    13321335        targetCM := net.casemap(target)
    1333         history, err := dc.user.msgStore.LoadLatestID(&net.Network, targetCM, msgID, limit)
     1336        history, err := dc.user.msgStore.LoadLatestID(ctx, &net.Network, targetCM, msgID, limit)
    13341337        if err != nil {
    13351338                dc.logger.Printf("failed to send backlog for %q: %v", target, err)
     
    23352338                eventPlayback := dc.caps["draft/event-playback"]
    23362339
     2340                ctx, cancel := context.WithTimeout(context.TODO(), messageStoreTimeout)
     2341                defer cancel()
     2342
    23372343                var history []*irc.Message
    23382344                switch subcommand {
    23392345                case "BEFORE":
    2340                         history, err = store.LoadBeforeTime(&network.Network, entity, bounds[0], time.Time{}, limit, eventPlayback)
     2346                        history, err = store.LoadBeforeTime(ctx, &network.Network, entity, bounds[0], time.Time{}, limit, eventPlayback)
    23412347                case "AFTER":
    2342                         history, err = store.LoadAfterTime(&network.Network, entity, bounds[0], time.Now(), limit, eventPlayback)
     2348                        history, err = store.LoadAfterTime(ctx, &network.Network, entity, bounds[0], time.Now(), limit, eventPlayback)
    23432349                case "BETWEEN":
    23442350                        if bounds[0].Before(bounds[1]) {
    2345                                 history, err = store.LoadAfterTime(&network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
     2351                                history, err = store.LoadAfterTime(ctx, &network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
    23462352                        } else {
    2347                                 history, err = store.LoadBeforeTime(&network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
     2353                                history, err = store.LoadBeforeTime(ctx, &network.Network, entity, bounds[0], bounds[1], limit, eventPlayback)
    23482354                        }
    23492355                case "TARGETS":
    23502356                        // TODO: support TARGETS in multi-upstream mode
    2351                         targets, err := store.ListTargets(&network.Network, bounds[0], bounds[1], limit, eventPlayback)
     2357                        targets, err := store.ListTargets(ctx, &network.Network, bounds[0], bounds[1], limit, eventPlayback)
    23522358                        if err != nil {
    23532359                                dc.logger.Printf("failed fetching targets for chathistory: %v", err)
  • trunk/msgstore.go

    r666 r667  
    33import (
    44        "bytes"
     5        "context"
    56        "encoding/base64"
    67        "fmt"
     
    2021        // LoadLatestID queries the latest non-event messages for the given network,
    2122        // entity and date, up to a count of limit messages, sorted from oldest to newest.
    22         LoadLatestID(network *Network, entity, id string, limit int) ([]*irc.Message, error)
     23        LoadLatestID(ctx context.Context, network *Network, entity, id string, limit int) ([]*irc.Message, error)
    2324        Append(network *Network, entity string, msg *irc.Message) (id string, err error)
    2425}
     
    3839        // both excluded. end may be before or after start.
    3940        // If events is false, only PRIVMSG/NOTICE messages are considered.
    40         ListTargets(network *Network, start, end time.Time, limit int, events bool) ([]chatHistoryTarget, error)
     41        ListTargets(ctx context.Context, network *Network, start, end time.Time, limit int, events bool) ([]chatHistoryTarget, error)
    4142        // LoadBeforeTime loads up to limit messages before start down to end. The
    4243        // returned messages must be between and excluding the provided bounds.
    4344        // end is before start.
    4445        // If events is false, only PRIVMSG/NOTICE messages are considered.
    45         LoadBeforeTime(network *Network, entity string, start, end time.Time, limit int, events bool) ([]*irc.Message, error)
     46        LoadBeforeTime(ctx context.Context, network *Network, entity string, start, end time.Time, limit int, events bool) ([]*irc.Message, error)
    4647        // LoadBeforeTime loads up to limit messages after start up to end. The
    4748        // returned messages must be between and excluding the provided bounds.
    4849        // end is after start.
    4950        // If events is false, only PRIVMSG/NOTICE messages are considered.
    50         LoadAfterTime(network *Network, entity string, start, end time.Time, limit int, events bool) ([]*irc.Message, error)
     51        LoadAfterTime(ctx context.Context, network *Network, entity string, start, end time.Time, limit int, events bool) ([]*irc.Message, error)
    5152}
    5253
  • trunk/msgstore_fs.go

    r666 r667  
    33import (
    44        "bufio"
     5        "context"
    56        "fmt"
    67        "io"
     
    477478}
    478479
    479 func (ms *fsMessageStore) LoadBeforeTime(network *Network, entity string, start time.Time, end time.Time, limit int, events bool) ([]*irc.Message, error) {
     480func (ms *fsMessageStore) LoadBeforeTime(ctx context.Context, network *Network, entity string, start time.Time, end time.Time, limit int, events bool) ([]*irc.Message, error) {
    480481        start = start.In(time.Local)
    481482        end = end.In(time.Local)
     
    502503}
    503504
    504 func (ms *fsMessageStore) LoadAfterTime(network *Network, entity string, start time.Time, end time.Time, limit int, events bool) ([]*irc.Message, error) {
     505func (ms *fsMessageStore) LoadAfterTime(ctx context.Context, network *Network, entity string, start time.Time, end time.Time, limit int, events bool) ([]*irc.Message, error) {
    505506        start = start.In(time.Local)
    506507        end = end.In(time.Local)
     
    526527}
    527528
    528 func (ms *fsMessageStore) LoadLatestID(network *Network, entity, id string, limit int) ([]*irc.Message, error) {
     529func (ms *fsMessageStore) LoadLatestID(ctx context.Context, network *Network, entity, id string, limit int) ([]*irc.Message, error) {
    529530        var afterTime time.Time
    530531        var afterOffset int64
     
    570571}
    571572
    572 func (ms *fsMessageStore) ListTargets(network *Network, start, end time.Time, limit int, events bool) ([]chatHistoryTarget, error) {
     573func (ms *fsMessageStore) ListTargets(ctx context.Context, network *Network, start, end time.Time, limit int, events bool) ([]chatHistoryTarget, error) {
    573574        start = start.In(time.Local)
    574575        end = end.In(time.Local)
  • trunk/msgstore_memory.go

    r666 r667  
    22
    33import (
     4        "context"
    45        "fmt"
    56        "time"
     
    9293}
    9394
    94 func (ms *memoryMessageStore) LoadLatestID(network *Network, entity, id string, limit int) ([]*irc.Message, error) {
     95func (ms *memoryMessageStore) LoadLatestID(ctx context.Context, network *Network, entity, id string, limit int) ([]*irc.Message, error) {
    9596        _, _, seq, err := parseMemoryMsgID(id)
    9697        if err != nil {
  • trunk/server.go

    r662 r667  
    2626var upstreamMessageDelay = 2 * time.Second
    2727var upstreamMessageBurst = 10
     28var messageStoreTimeout = 10 * time.Second
    2829
    2930type Logger interface {
Note: See TracChangeset for help on using the changeset viewer.