feat: defer login bootstrap updates
This commit is contained in:
parent
a246fd5417
commit
4d9f1e271d
18 changed files with 958 additions and 188 deletions
144
internal/store/memory/bootstrap_update_job.go
Normal file
144
internal/store/memory/bootstrap_update_job.go
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
)
|
||||
|
||||
type BootstrapUpdateJobStore struct {
|
||||
mu sync.Mutex
|
||||
nextID int64
|
||||
jobs map[int64]domain.BootstrapUpdateJob
|
||||
uniq map[bootstrapUpdateJobKey]int64
|
||||
}
|
||||
|
||||
type bootstrapUpdateJobKey struct {
|
||||
kind domain.BootstrapUpdateJobKind
|
||||
userID int64
|
||||
messageBoxID int
|
||||
}
|
||||
|
||||
func NewBootstrapUpdateJobStore() *BootstrapUpdateJobStore {
|
||||
return &BootstrapUpdateJobStore{
|
||||
nextID: 1,
|
||||
jobs: make(map[int64]domain.BootstrapUpdateJob),
|
||||
uniq: make(map[bootstrapUpdateJobKey]int64),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) EnqueueLoginMessage(_ context.Context, job domain.BootstrapUpdateJob) (domain.BootstrapUpdateJob, error) {
|
||||
if job.Kind == "" {
|
||||
job.Kind = domain.BootstrapUpdateJobLoginMessage
|
||||
}
|
||||
if job.Status == "" {
|
||||
job.Status = domain.BootstrapUpdateJobPending
|
||||
}
|
||||
now := time.Now()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
key := bootstrapUpdateJobKey{kind: job.Kind, userID: job.UserID, messageBoxID: job.MessageBoxID}
|
||||
if id, ok := s.uniq[key]; ok {
|
||||
existing := s.jobs[id]
|
||||
if existing.Status == domain.BootstrapUpdateJobFailed {
|
||||
existing.Status = domain.BootstrapUpdateJobPending
|
||||
existing.LastError = ""
|
||||
}
|
||||
existing.AuthKeyID = job.AuthKeyID
|
||||
existing.SessionID = job.SessionID
|
||||
existing.UpdatedAt = now
|
||||
s.jobs[id] = existing
|
||||
return existing, nil
|
||||
}
|
||||
job.ID = s.nextID
|
||||
s.nextID++
|
||||
job.CreatedAt = now
|
||||
job.UpdatedAt = now
|
||||
s.jobs[job.ID] = job
|
||||
s.uniq[key] = job.ID
|
||||
return job, nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) MarkReadyForSession(_ context.Context, userID int64, authKeyID [8]byte, sessionID int64) (int, error) {
|
||||
now := time.Now()
|
||||
count := 0
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for id, job := range s.jobs {
|
||||
if job.UserID != userID || job.AuthKeyID != authKeyID || job.SessionID != sessionID || job.Status != domain.BootstrapUpdateJobPending {
|
||||
continue
|
||||
}
|
||||
job.Status = domain.BootstrapUpdateJobReady
|
||||
job.ReadyAt = now
|
||||
job.UpdatedAt = now
|
||||
s.jobs[id] = job
|
||||
count++
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) ClaimReady(_ context.Context, limit int, leaseTimeout time.Duration) ([]domain.BootstrapUpdateJob, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
if leaseTimeout <= 0 {
|
||||
leaseTimeout = 30 * time.Second
|
||||
}
|
||||
now := time.Now()
|
||||
out := make([]domain.BootstrapUpdateJob, 0, limit)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for id, job := range s.jobs {
|
||||
if len(out) >= limit {
|
||||
break
|
||||
}
|
||||
ready := job.Status == domain.BootstrapUpdateJobReady && (job.ReadyAt.IsZero() || !job.ReadyAt.After(now))
|
||||
stalePublishing := job.Status == domain.BootstrapUpdateJobPublishing && now.Sub(job.UpdatedAt) > leaseTimeout
|
||||
if !ready && !stalePublishing {
|
||||
continue
|
||||
}
|
||||
job.Status = domain.BootstrapUpdateJobPublishing
|
||||
job.Attempts++
|
||||
job.UpdatedAt = now
|
||||
s.jobs[id] = job
|
||||
out = append(out, job)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) MarkPublished(_ context.Context, id int64) error {
|
||||
now := time.Now()
|
||||
s.mu.Lock()
|
||||
if job, ok := s.jobs[id]; ok {
|
||||
job.Status = domain.BootstrapUpdateJobPublished
|
||||
job.PublishedAt = now
|
||||
job.UpdatedAt = now
|
||||
s.jobs[id] = job
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) MarkFailed(_ context.Context, id int64, lastError string) error {
|
||||
now := time.Now()
|
||||
s.mu.Lock()
|
||||
if job, ok := s.jobs[id]; ok {
|
||||
if job.Attempts >= domain.BootstrapUpdateMaxAttempts {
|
||||
job.Status = domain.BootstrapUpdateJobFailed
|
||||
} else {
|
||||
job.Status = domain.BootstrapUpdateJobReady
|
||||
delay := time.Duration(job.Attempts*job.Attempts) * time.Second
|
||||
if delay > time.Minute {
|
||||
delay = time.Minute
|
||||
}
|
||||
job.ReadyAt = now.Add(delay)
|
||||
}
|
||||
job.LastError = lastError
|
||||
job.UpdatedAt = now
|
||||
s.jobs[id] = job
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
58
internal/store/memory/bootstrap_update_job_test.go
Normal file
58
internal/store/memory/bootstrap_update_job_test.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
)
|
||||
|
||||
func TestBootstrapUpdateJobRetriesBeforeFailed(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := NewBootstrapUpdateJobStore()
|
||||
job, err := store.EnqueueLoginMessage(ctx, domain.BootstrapUpdateJob{
|
||||
Kind: domain.BootstrapUpdateJobLoginMessage,
|
||||
UserID: 1000000001,
|
||||
AuthKeyID: [8]byte{1, 2, 3},
|
||||
SessionID: 77,
|
||||
MessageBoxID: 10,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("enqueue: %v", err)
|
||||
}
|
||||
if ready, err := store.MarkReadyForSession(ctx, job.UserID, job.AuthKeyID, job.SessionID); err != nil || ready != 1 {
|
||||
t.Fatalf("mark ready = %d, %v; want 1 nil", ready, err)
|
||||
}
|
||||
claimed, err := store.ClaimReady(ctx, 10, time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("claim ready: %v", err)
|
||||
}
|
||||
if len(claimed) != 1 || claimed[0].Attempts != 1 {
|
||||
t.Fatalf("claimed = %+v, want one first attempt", claimed)
|
||||
}
|
||||
if err := store.MarkFailed(ctx, claimed[0].ID, "temporary"); err != nil {
|
||||
t.Fatalf("mark failed: %v", err)
|
||||
}
|
||||
retry, err := store.ClaimReady(ctx, 10, time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("claim retry before backoff: %v", err)
|
||||
}
|
||||
if len(retry) != 0 {
|
||||
t.Fatalf("retry before backoff = %+v, want none", retry)
|
||||
}
|
||||
|
||||
store.mu.Lock()
|
||||
job = store.jobs[claimed[0].ID]
|
||||
job.ReadyAt = time.Now().Add(-time.Second)
|
||||
store.jobs[claimed[0].ID] = job
|
||||
store.mu.Unlock()
|
||||
|
||||
retry, err = store.ClaimReady(ctx, 10, time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("claim retry after backoff: %v", err)
|
||||
}
|
||||
if len(retry) != 1 || retry[0].Attempts != 2 {
|
||||
t.Fatalf("retry after backoff = %+v, want second attempt", retry)
|
||||
}
|
||||
}
|
||||
|
|
@ -46,15 +46,7 @@ func (s *UpdateEventStore) append(userID int64, event domain.UpdateEvent, alloca
|
|||
event.PtsCount = 1
|
||||
}
|
||||
event.UserID = userID
|
||||
event.Message = cloneMessage(event.Message)
|
||||
event.Story = cloneUpdateStory(event.Story)
|
||||
event.MessageIDs = append([]int(nil), event.MessageIDs...)
|
||||
event.Peers = append([]domain.Peer(nil), event.Peers...)
|
||||
event.Users = append([]domain.User(nil), event.Users...)
|
||||
event.Channels = append([]domain.Channel(nil), event.Channels...)
|
||||
event.Reaction = cloneUpdateReaction(event.Reaction)
|
||||
event.QuickReplies = cloneUpdateQuickReplies(event.QuickReplies)
|
||||
event.QuickReplyMessage = cloneUpdateQuickReplyMessage(event.QuickReplyMessage)
|
||||
event = cloneUpdateEvent(event)
|
||||
s.mu.Lock()
|
||||
if allocate {
|
||||
current := 0
|
||||
|
|
@ -79,16 +71,7 @@ func (s *UpdateEventStore) ListAfter(_ context.Context, userID int64, pts, limit
|
|||
if event.Pts <= pts {
|
||||
continue
|
||||
}
|
||||
event.Message = cloneMessage(event.Message)
|
||||
event.Story = cloneUpdateStory(event.Story)
|
||||
event.MessageIDs = append([]int(nil), event.MessageIDs...)
|
||||
event.Peers = append([]domain.Peer(nil), event.Peers...)
|
||||
event.Users = append([]domain.User(nil), event.Users...)
|
||||
event.Channels = append([]domain.Channel(nil), event.Channels...)
|
||||
event.Reaction = cloneUpdateReaction(event.Reaction)
|
||||
event.QuickReplies = cloneUpdateQuickReplies(event.QuickReplies)
|
||||
event.QuickReplyMessage = cloneUpdateQuickReplyMessage(event.QuickReplyMessage)
|
||||
out = append(out, event)
|
||||
out = append(out, cloneUpdateEvent(event))
|
||||
if limit > 0 && len(out) >= limit {
|
||||
break
|
||||
}
|
||||
|
|
@ -96,6 +79,33 @@ func (s *UpdateEventStore) ListAfter(_ context.Context, userID int64, pts, limit
|
|||
return out, nil
|
||||
}
|
||||
|
||||
func (s *UpdateEventStore) FindNewMessageEvent(_ context.Context, userID int64, messageBoxID int) (domain.UpdateEvent, bool, error) {
|
||||
if userID == 0 || messageBoxID <= 0 {
|
||||
return domain.UpdateEvent{}, false, nil
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, event := range s.events[userID] {
|
||||
if event.Type == domain.UpdateEventNewMessage && event.Message.ID == messageBoxID {
|
||||
return cloneUpdateEvent(event), true, nil
|
||||
}
|
||||
}
|
||||
return domain.UpdateEvent{}, false, nil
|
||||
}
|
||||
|
||||
func cloneUpdateEvent(event domain.UpdateEvent) domain.UpdateEvent {
|
||||
event.Message = cloneMessage(event.Message)
|
||||
event.Story = cloneUpdateStory(event.Story)
|
||||
event.MessageIDs = append([]int(nil), event.MessageIDs...)
|
||||
event.Peers = append([]domain.Peer(nil), event.Peers...)
|
||||
event.Users = append([]domain.User(nil), event.Users...)
|
||||
event.Channels = append([]domain.Channel(nil), event.Channels...)
|
||||
event.Reaction = cloneUpdateReaction(event.Reaction)
|
||||
event.QuickReplies = cloneUpdateQuickReplies(event.QuickReplies)
|
||||
event.QuickReplyMessage = cloneUpdateQuickReplyMessage(event.QuickReplyMessage)
|
||||
return event
|
||||
}
|
||||
|
||||
func cloneUpdateStory(story domain.Story) domain.Story {
|
||||
story.Entities = append([]domain.MessageEntity(nil), story.Entities...)
|
||||
story.Views.Reactions = append([]domain.ChannelMessageReactionCount(nil), story.Views.Reactions...)
|
||||
|
|
@ -122,7 +132,6 @@ func cloneUpdateQuickReplyMessage(in domain.QuickReplyMessage) domain.QuickReply
|
|||
return out
|
||||
}
|
||||
|
||||
|
||||
// MaxContiguousPts 返回从 1 起无空洞的最大 pts(内存版按 pts_count 连续扫描)。
|
||||
func (s *UpdateEventStore) MaxContiguousPts(_ context.Context, userID int64) (int, error) {
|
||||
s.mu.RLock()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue