feat: defer login bootstrap updates
This commit is contained in:
parent
a246fd5417
commit
4d9f1e271d
18 changed files with 958 additions and 188 deletions
16
internal/store/bootstrap_update_job.go
Normal file
16
internal/store/bootstrap_update_job.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
)
|
||||
|
||||
type BootstrapUpdateJobStore interface {
|
||||
EnqueueLoginMessage(ctx context.Context, job domain.BootstrapUpdateJob) (domain.BootstrapUpdateJob, error)
|
||||
MarkReadyForSession(ctx context.Context, userID int64, authKeyID [8]byte, sessionID int64) (int, error)
|
||||
ClaimReady(ctx context.Context, limit int, leaseTimeout time.Duration) ([]domain.BootstrapUpdateJob, error)
|
||||
MarkPublished(ctx context.Context, id int64) error
|
||||
MarkFailed(ctx context.Context, id int64, lastError string) error
|
||||
}
|
||||
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()
|
||||
|
|
|
|||
189
internal/store/postgres/bootstrap_update_job.go
Normal file
189
internal/store/postgres/bootstrap_update_job.go
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"telesrv/internal/domain"
|
||||
"telesrv/internal/store/postgres/sqlcgen"
|
||||
)
|
||||
|
||||
type BootstrapUpdateJobStore struct {
|
||||
db sqlcgen.DBTX
|
||||
}
|
||||
|
||||
func NewBootstrapUpdateJobStore(db sqlcgen.DBTX) *BootstrapUpdateJobStore {
|
||||
return &BootstrapUpdateJobStore{db: db}
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) EnqueueLoginMessage(ctx context.Context, job domain.BootstrapUpdateJob) (domain.BootstrapUpdateJob, error) {
|
||||
if job.Kind == "" {
|
||||
job.Kind = domain.BootstrapUpdateJobLoginMessage
|
||||
}
|
||||
if job.Status == "" {
|
||||
job.Status = domain.BootstrapUpdateJobPending
|
||||
}
|
||||
row := s.db.QueryRow(ctx, `
|
||||
INSERT INTO bootstrap_update_jobs (
|
||||
kind, user_id, auth_key_id, session_id, message_box_id, status
|
||||
) VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (kind, user_id, message_box_id) DO UPDATE SET
|
||||
auth_key_id = EXCLUDED.auth_key_id,
|
||||
session_id = EXCLUDED.session_id,
|
||||
status = CASE
|
||||
WHEN bootstrap_update_jobs.status = 'failed' THEN 'pending'
|
||||
ELSE bootstrap_update_jobs.status
|
||||
END,
|
||||
last_error = CASE
|
||||
WHEN bootstrap_update_jobs.status = 'failed' THEN ''
|
||||
ELSE bootstrap_update_jobs.last_error
|
||||
END,
|
||||
updated_at = now()
|
||||
RETURNING id, kind, user_id, auth_key_id, session_id, message_box_id, status, attempts, last_error, created_at, updated_at, ready_at, published_at`,
|
||||
string(job.Kind),
|
||||
job.UserID,
|
||||
authKeyIDToInt64(job.AuthKeyID),
|
||||
job.SessionID,
|
||||
job.MessageBoxID,
|
||||
string(job.Status),
|
||||
)
|
||||
out, err := scanBootstrapUpdateJob(row)
|
||||
if err != nil {
|
||||
return domain.BootstrapUpdateJob{}, fmt.Errorf("enqueue bootstrap login message: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) MarkReadyForSession(ctx context.Context, userID int64, authKeyID [8]byte, sessionID int64) (int, error) {
|
||||
tag, err := s.db.Exec(ctx, `
|
||||
UPDATE bootstrap_update_jobs
|
||||
SET status = 'ready',
|
||||
ready_at = now(),
|
||||
updated_at = now()
|
||||
WHERE user_id = $1
|
||||
AND auth_key_id = $2
|
||||
AND session_id = $3
|
||||
AND status = 'pending'`,
|
||||
userID, authKeyIDToInt64(authKeyID), sessionID)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("mark bootstrap jobs ready: %w", err)
|
||||
}
|
||||
return int(tag.RowsAffected()), nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) ClaimReady(ctx context.Context, limit int, leaseTimeout time.Duration) ([]domain.BootstrapUpdateJob, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
if limit > 1000 {
|
||||
limit = 1000
|
||||
}
|
||||
leaseSeconds := int(leaseTimeout / time.Second)
|
||||
if leaseSeconds <= 0 {
|
||||
leaseSeconds = int(defaultDispatchLease / time.Second)
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `
|
||||
WITH picked AS (
|
||||
SELECT id
|
||||
FROM bootstrap_update_jobs
|
||||
WHERE (status = 'ready' AND COALESCE(ready_at, created_at) <= now())
|
||||
OR (status = 'publishing' AND updated_at < now() - make_interval(secs => $1::int))
|
||||
ORDER BY COALESCE(ready_at, created_at) ASC, user_id ASC, id ASC
|
||||
LIMIT $2
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
UPDATE bootstrap_update_jobs j
|
||||
SET status = 'publishing',
|
||||
attempts = j.attempts + 1,
|
||||
updated_at = now()
|
||||
FROM picked p
|
||||
WHERE j.id = p.id
|
||||
RETURNING j.id, j.kind, j.user_id, j.auth_key_id, j.session_id, j.message_box_id, j.status, j.attempts, j.last_error, j.created_at, j.updated_at, j.ready_at, j.published_at`,
|
||||
leaseSeconds, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("claim bootstrap jobs: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]domain.BootstrapUpdateJob, 0)
|
||||
for rows.Next() {
|
||||
job, err := scanBootstrapUpdateJob(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, job)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("claim bootstrap jobs rows: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) MarkPublished(ctx context.Context, id int64) error {
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
UPDATE bootstrap_update_jobs
|
||||
SET status = 'published',
|
||||
published_at = now(),
|
||||
updated_at = now()
|
||||
WHERE id = $1`, id); err != nil {
|
||||
return fmt.Errorf("mark bootstrap job published: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *BootstrapUpdateJobStore) MarkFailed(ctx context.Context, id int64, lastError string) error {
|
||||
if _, err := s.db.Exec(ctx, `
|
||||
UPDATE bootstrap_update_jobs
|
||||
SET status = CASE WHEN attempts >= $3 THEN 'failed' ELSE 'ready' END,
|
||||
ready_at = CASE
|
||||
WHEN attempts >= $3 THEN ready_at
|
||||
ELSE now() + make_interval(secs => LEAST(60, attempts * attempts))
|
||||
END,
|
||||
last_error = $2,
|
||||
updated_at = now()
|
||||
WHERE id = $1`, id, lastError, domain.BootstrapUpdateMaxAttempts); err != nil {
|
||||
return fmt.Errorf("mark bootstrap job failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type bootstrapJobScanner interface {
|
||||
Scan(dest ...any) error
|
||||
}
|
||||
|
||||
func scanBootstrapUpdateJob(row bootstrapJobScanner) (domain.BootstrapUpdateJob, error) {
|
||||
var (
|
||||
job domain.BootstrapUpdateJob
|
||||
kind, status string
|
||||
authKeyID int64
|
||||
readyAt, published sql.NullTime
|
||||
)
|
||||
if err := row.Scan(
|
||||
&job.ID,
|
||||
&kind,
|
||||
&job.UserID,
|
||||
&authKeyID,
|
||||
&job.SessionID,
|
||||
&job.MessageBoxID,
|
||||
&status,
|
||||
&job.Attempts,
|
||||
&job.LastError,
|
||||
&job.CreatedAt,
|
||||
&job.UpdatedAt,
|
||||
&readyAt,
|
||||
&published,
|
||||
); err != nil {
|
||||
return domain.BootstrapUpdateJob{}, fmt.Errorf("scan bootstrap job: %w", err)
|
||||
}
|
||||
job.Kind = domain.BootstrapUpdateJobKind(kind)
|
||||
job.Status = domain.BootstrapUpdateJobStatus(status)
|
||||
job.AuthKeyID = authKeyIDFromInt64(authKeyID)
|
||||
if readyAt.Valid {
|
||||
job.ReadyAt = readyAt.Time
|
||||
}
|
||||
if published.Valid {
|
||||
job.PublishedAt = published.Time
|
||||
}
|
||||
return job, nil
|
||||
}
|
||||
|
|
@ -457,6 +457,34 @@ func (s *UpdateEventStore) ListAfter(ctx context.Context, userID int64, pts, lim
|
|||
return out, nil
|
||||
}
|
||||
|
||||
func (s *UpdateEventStore) FindNewMessageEvent(ctx context.Context, userID int64, messageBoxID int) (domain.UpdateEvent, bool, error) {
|
||||
if userID == 0 || messageBoxID <= 0 {
|
||||
return domain.UpdateEvent{}, false, nil
|
||||
}
|
||||
var pts int
|
||||
if err := s.db.QueryRow(ctx, `
|
||||
SELECT pts
|
||||
FROM user_update_events
|
||||
WHERE user_id = $1
|
||||
AND event_type = $2
|
||||
AND message_box_id = $3
|
||||
ORDER BY pts ASC
|
||||
LIMIT 1`, userID, string(domain.UpdateEventNewMessage), messageBoxID).Scan(&pts); err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return domain.UpdateEvent{}, false, nil
|
||||
}
|
||||
return domain.UpdateEvent{}, false, fmt.Errorf("find new message update event: %w", err)
|
||||
}
|
||||
events, err := s.ListAfter(ctx, userID, pts-1, 1)
|
||||
if err != nil {
|
||||
return domain.UpdateEvent{}, false, err
|
||||
}
|
||||
if len(events) == 0 || events[0].Pts != pts {
|
||||
return domain.UpdateEvent{}, false, fmt.Errorf("hydrate new message update event: expected pts %d", pts)
|
||||
}
|
||||
return events[0], true, nil
|
||||
}
|
||||
|
||||
// MaxContiguousPts 见 store.UpdateEventStore 接口说明。PG 写路径保证水位与
|
||||
// durable event 同事务提交;缺行代表该账号还没有 update。
|
||||
func (s *UpdateEventStore) MaxContiguousPts(ctx context.Context, userID int64) (int, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue