this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add a user cache on the bgs (#816)

should cut ~3% of all DB usage on the prod relay

authored by

Whyrusleeping and committed by
GitHub
a4229037 05b87518

+88 -23
+69 -7
bgs/bgs.go
··· 27 27 "github.com/bluesky-social/indigo/models" 28 28 "github.com/bluesky-social/indigo/repomgr" 29 29 "github.com/bluesky-social/indigo/xrpc" 30 + lru "github.com/hashicorp/golang-lru/v2" 30 31 "golang.org/x/sync/semaphore" 31 32 "golang.org/x/time/rate" 32 33 ··· 87 88 88 89 // Management of Compaction 89 90 compactor *Compactor 91 + 92 + // User cache 93 + userCache *lru.Cache[string, *User] 90 94 } 91 95 92 96 type PDSResync struct { ··· 136 140 db.AutoMigrate(models.PDS{}) 137 141 db.AutoMigrate(models.DomainBan{}) 138 142 143 + uc, _ := lru.New[string, *User](1_000_000) 144 + 139 145 bgs := &BGS{ 140 146 Index: ix, 141 147 db: db, ··· 151 157 consumers: make(map[uint64]*SocketConsumer), 152 158 153 159 pdsResyncs: make(map[uint]*PDSResync), 160 + 161 + userCache: uc, 154 162 } 155 163 156 164 ix.CreateExternalUser = bgs.createExternalUser ··· 521 529 522 530 // UpstreamStatus is the state of the user as reported by the upstream PDS 523 531 UpstreamStatus string `gorm:"index"` 532 + 533 + lk sync.Mutex 534 + } 535 + 536 + func (u *User) SetTakenDown(v bool) { 537 + u.lk.Lock() 538 + defer u.lk.Unlock() 539 + u.TakenDown = v 540 + } 541 + 542 + func (u *User) GetTakenDown() bool { 543 + u.lk.Lock() 544 + defer u.lk.Unlock() 545 + return u.TakenDown 546 + } 547 + 548 + func (u *User) SetTombstoned(v bool) { 549 + u.lk.Lock() 550 + defer u.lk.Unlock() 551 + u.Tombstoned = v 552 + } 553 + 554 + func (u *User) GetTombstoned() bool { 555 + u.lk.Lock() 556 + defer u.lk.Unlock() 557 + return u.Tombstoned 558 + } 559 + 560 + func (u *User) SetUpstreamStatus(v string) { 561 + u.lk.Lock() 562 + defer u.lk.Unlock() 563 + u.UpstreamStatus = v 564 + } 565 + 566 + func (u *User) GetUpstreamStatus() string { 567 + u.lk.Lock() 568 + defer u.lk.Unlock() 569 + return u.UpstreamStatus 524 570 } 525 571 526 572 type addTargetBody struct { ··· 771 817 ctx, span := tracer.Start(ctx, "lookupUserByDid") 772 818 defer span.End() 773 819 820 + cu, ok := bgs.userCache.Get(did) 821 + if ok { 822 + return cu, nil 823 + } 824 + 774 825 var u User 775 826 if err := bgs.db.Find(&u, "did = ?", did).Error; err != nil { 776 827 return nil, err ··· 779 830 if u.ID == 0 { 780 831 return nil, gorm.ErrRecordNotFound 781 832 } 833 + 834 + bgs.userCache.Add(did, &u) 782 835 783 836 return &u, nil 784 837 } ··· 840 893 u.Did = evt.Repo 841 894 } 842 895 843 - span.SetAttributes(attribute.String("upstream_status", u.UpstreamStatus)) 896 + ustatus := u.GetUpstreamStatus() 897 + span.SetAttributes(attribute.String("upstream_status", ustatus)) 844 898 845 - if u.TakenDown || u.UpstreamStatus == events.AccountStatusTakendown { 846 - span.SetAttributes(attribute.Bool("taken_down_by_relay_admin", u.TakenDown)) 899 + if u.GetTakenDown() || ustatus == events.AccountStatusTakendown { 900 + span.SetAttributes(attribute.Bool("taken_down_by_relay_admin", u.GetTakenDown())) 847 901 log.Debugw("dropping commit event from taken down user", "did", evt.Repo, "seq", evt.Seq, "pdsHost", host.Host) 848 902 return nil 849 903 } 850 904 851 - if u.UpstreamStatus == events.AccountStatusSuspended { 905 + if ustatus == events.AccountStatusSuspended { 852 906 log.Debugw("dropping commit event from suspended user", "did", evt.Repo, "seq", evt.Seq, "pdsHost", host.Host) 853 907 return nil 854 908 } 855 909 856 - if u.UpstreamStatus == events.AccountStatusDeactivated { 910 + if ustatus == events.AccountStatusDeactivated { 857 911 log.Debugw("dropping commit event from deactivated user", "did", evt.Repo, "seq", evt.Seq, "pdsHost", host.Host) 858 912 return nil 859 913 } ··· 877 931 } 878 932 } 879 933 880 - if u.Tombstoned { 934 + if u.GetTombstoned() { 881 935 span.SetAttributes(attribute.Bool("tombstoned", true)) 882 936 // we've checked the authority of the users PDS, so reinstate the account 883 937 if err := bgs.db.Model(&User{}).Where("id = ?", u.ID).UpdateColumn("tombstoned", false).Error; err != nil { 884 938 return fmt.Errorf("failed to un-tombstone a user: %w", err) 885 939 } 940 + u.SetTombstoned(false) 886 941 887 942 ai, err := bgs.Index.LookupUser(ctx, u.ID) 888 943 if err != nil { ··· 1041 1096 return fmt.Errorf("failed to look up user by did: %w", err) 1042 1097 } 1043 1098 1044 - if u.TakenDown { 1099 + if u.GetTakenDown() { 1045 1100 shouldBeActive = false 1046 1101 status = &events.AccountStatusTakendown 1047 1102 } ··· 1370 1425 if err := bgs.db.Model(User{}).Where("id = ?", u.ID).Update("upstream_status", events.AccountStatusActive).Error; err != nil { 1371 1426 return fmt.Errorf("failed to set user active status: %w", err) 1372 1427 } 1428 + u.SetUpstreamStatus(events.AccountStatusActive) 1373 1429 case events.AccountStatusDeactivated: 1374 1430 if err := bgs.db.Model(User{}).Where("id = ?", u.ID).Update("upstream_status", events.AccountStatusDeactivated).Error; err != nil { 1375 1431 return fmt.Errorf("failed to set user deactivation status: %w", err) 1376 1432 } 1433 + u.SetUpstreamStatus(events.AccountStatusDeactivated) 1377 1434 case events.AccountStatusSuspended: 1378 1435 if err := bgs.db.Model(User{}).Where("id = ?", u.ID).Update("upstream_status", events.AccountStatusSuspended).Error; err != nil { 1379 1436 return fmt.Errorf("failed to set user suspension status: %w", err) 1380 1437 } 1438 + u.SetUpstreamStatus(events.AccountStatusSuspended) 1381 1439 case events.AccountStatusTakendown: 1382 1440 if err := bgs.db.Model(User{}).Where("id = ?", u.ID).Update("upstream_status", events.AccountStatusTakendown).Error; err != nil { 1383 1441 return fmt.Errorf("failed to set user taken down status: %w", err) 1384 1442 } 1443 + u.SetUpstreamStatus(events.AccountStatusTakendown) 1385 1444 1386 1445 if err := bgs.db.Model(&models.ActorInfo{}).Where("uid = ?", u.ID).UpdateColumns(map[string]any{ 1387 1446 "handle": nil, ··· 1396 1455 }).Error; err != nil { 1397 1456 return err 1398 1457 } 1458 + u.SetUpstreamStatus(events.AccountStatusDeleted) 1399 1459 1400 1460 if err := bgs.db.Model(&models.ActorInfo{}).Where("uid = ?", u.ID).UpdateColumns(map[string]any{ 1401 1461 "handle": nil, ··· 1422 1482 if err := bgs.db.Model(User{}).Where("id = ?", u.ID).Update("taken_down", true).Error; err != nil { 1423 1483 return err 1424 1484 } 1485 + u.SetTakenDown(true) 1425 1486 1426 1487 if err := bgs.repoman.TakeDownRepo(ctx, u.ID); err != nil { 1427 1488 return err ··· 1443 1504 if err := bgs.db.Model(User{}).Where("id = ?", u.ID).Update("taken_down", false).Error; err != nil { 1444 1505 return err 1445 1506 } 1507 + u.SetTakenDown(false) 1446 1508 1447 1509 return nil 1448 1510 }
+1 -1
bgs/compactor.go
··· 349 349 return state, nil 350 350 } 351 351 352 - func (c *Compactor) EnqueueRepo(ctx context.Context, user User, fast bool) { 352 + func (c *Compactor) EnqueueRepo(ctx context.Context, user *User, fast bool) { 353 353 ctx, span := otel.Tracer("compactor").Start(ctx, "EnqueueRepo") 354 354 defer span.End() 355 355 log.Infow("enqueueing compaction for repo", "repo", user.Did, "uid", user.ID, "fast", fast)
+18 -15
bgs/handlers.go
··· 34 34 return nil, echo.NewHTTPError(http.StatusInternalServerError, "failed to lookup user") 35 35 } 36 36 37 - if u.Tombstoned { 37 + if u.GetTombstoned() { 38 38 return nil, fmt.Errorf("account was deleted") 39 39 } 40 40 41 - if u.TakenDown { 41 + if u.GetTakenDown() { 42 42 return nil, fmt.Errorf("account was taken down by the Relay") 43 43 } 44 44 45 - if u.UpstreamStatus == events.AccountStatusTakendown { 45 + ustatus := u.GetUpstreamStatus() 46 + if ustatus == events.AccountStatusTakendown { 46 47 return nil, fmt.Errorf("account was taken down by its PDS") 47 48 } 48 49 49 - if u.UpstreamStatus == events.AccountStatusDeactivated { 50 + if ustatus == events.AccountStatusDeactivated { 50 51 return nil, fmt.Errorf("account is temporarily deactivated") 51 52 } 52 53 53 - if u.UpstreamStatus == events.AccountStatusSuspended { 54 + if ustatus == events.AccountStatusSuspended { 54 55 return nil, fmt.Errorf("account is suspended by its PDS") 55 56 } 56 57 ··· 91 92 return nil, echo.NewHTTPError(http.StatusInternalServerError, "failed to lookup user") 92 93 } 93 94 94 - if u.Tombstoned { 95 + if u.GetTombstoned() { 95 96 return nil, fmt.Errorf("account was deleted") 96 97 } 97 98 98 - if u.TakenDown { 99 + if u.GetTakenDown() { 99 100 return nil, fmt.Errorf("account was taken down by the Relay") 100 101 } 101 102 102 - if u.UpstreamStatus == events.AccountStatusTakendown { 103 + ustatus := u.GetUpstreamStatus() 104 + if ustatus == events.AccountStatusTakendown { 103 105 return nil, fmt.Errorf("account was taken down by its PDS") 104 106 } 105 107 106 - if u.UpstreamStatus == events.AccountStatusDeactivated { 108 + if ustatus == events.AccountStatusDeactivated { 107 109 return nil, fmt.Errorf("account is temporarily deactivated") 108 110 } 109 111 110 - if u.UpstreamStatus == events.AccountStatusSuspended { 112 + if ustatus == events.AccountStatusSuspended { 111 113 return nil, fmt.Errorf("account is suspended by its PDS") 112 114 } 113 115 ··· 253 255 return nil, echo.NewHTTPError(http.StatusInternalServerError, "failed to lookup user") 254 256 } 255 257 256 - if u.Tombstoned { 258 + if u.GetTombstoned() { 257 259 return nil, fmt.Errorf("account was deleted") 258 260 } 259 261 260 - if u.TakenDown { 262 + if u.GetTakenDown() { 261 263 return nil, fmt.Errorf("account was taken down by the Relay") 262 264 } 263 265 264 - if u.UpstreamStatus == events.AccountStatusTakendown { 266 + ustatus := u.GetUpstreamStatus() 267 + if ustatus == events.AccountStatusTakendown { 265 268 return nil, fmt.Errorf("account was taken down by its PDS") 266 269 } 267 270 268 - if u.UpstreamStatus == events.AccountStatusDeactivated { 271 + if ustatus == events.AccountStatusDeactivated { 269 272 return nil, fmt.Errorf("account is temporarily deactivated") 270 273 } 271 274 272 - if u.UpstreamStatus == events.AccountStatusSuspended { 275 + if ustatus == events.AccountStatusSuspended { 273 276 return nil, fmt.Errorf("account is suspended by its PDS") 274 277 } 275 278