Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).

appview/notify: make merged notifier calls concurrent

also uses reflection to unify implementations

Signed-off-by: oppiliappan <me@oppi.li>

authored by oppi.li and committed by

Tangled 3a2c41ab 8f0ac9d1

+42 -50
+42 -50
appview/notify/merged_notifier.go
··· 2 2 3 3 import ( 4 4 "context" 5 + "reflect" 6 + "sync" 5 7 6 8 "tangled.org/core/appview/models" 7 9 ) ··· 18 16 19 17 var _ Notifier = &mergedNotifier{} 20 18 21 - func (m *mergedNotifier) NewRepo(ctx context.Context, repo *models.Repo) { 22 - for _, notifier := range m.notifiers { 23 - notifier.NewRepo(ctx, repo) 19 + // fanout calls the same method on all notifiers concurrently 20 + func (m *mergedNotifier) fanout(method string, args ...any) { 21 + var wg sync.WaitGroup 22 + for _, n := range m.notifiers { 23 + wg.Add(1) 24 + go func(notifier Notifier) { 25 + defer wg.Done() 26 + v := reflect.ValueOf(notifier).MethodByName(method) 27 + in := make([]reflect.Value, len(args)) 28 + for i, arg := range args { 29 + in[i] = reflect.ValueOf(arg) 30 + } 31 + v.Call(in) 32 + }(n) 24 33 } 34 + wg.Wait() 35 + } 36 + 37 + func (m *mergedNotifier) NewRepo(ctx context.Context, repo *models.Repo) { 38 + m.fanout("NewRepo", ctx, repo) 25 39 } 26 40 27 41 func (m *mergedNotifier) NewStar(ctx context.Context, star *models.Star) { 28 - for _, notifier := range m.notifiers { 29 - notifier.NewStar(ctx, star) 30 - } 42 + m.fanout("NewStar", ctx, star) 31 43 } 44 + 32 45 func (m *mergedNotifier) DeleteStar(ctx context.Context, star *models.Star) { 33 - for _, notifier := range m.notifiers { 34 - notifier.DeleteStar(ctx, star) 35 - } 46 + m.fanout("DeleteStar", ctx, star) 36 47 } 37 48 38 49 func (m *mergedNotifier) NewIssue(ctx context.Context, issue *models.Issue) { 39 - for _, notifier := range m.notifiers { 40 - notifier.NewIssue(ctx, issue) 41 - } 50 + m.fanout("NewIssue", ctx, issue) 42 51 } 52 + 43 53 func (m *mergedNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment) { 44 - for _, notifier := range m.notifiers { 45 - notifier.NewIssueComment(ctx, comment) 46 - } 54 + m.fanout("NewIssueComment", ctx, comment) 47 55 } 48 56 49 57 func (m *mergedNotifier) NewIssueClosed(ctx context.Context, issue *models.Issue) { 50 - for _, notifier := range m.notifiers { 51 - notifier.NewIssueClosed(ctx, issue) 52 - } 58 + m.fanout("NewIssueClosed", ctx, issue) 53 59 } 54 60 55 61 func (m *mergedNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 56 - for _, notifier := range m.notifiers { 57 - notifier.NewFollow(ctx, follow) 58 - } 62 + m.fanout("NewFollow", ctx, follow) 59 63 } 64 + 60 65 func (m *mergedNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 61 - for _, notifier := range m.notifiers { 62 - notifier.DeleteFollow(ctx, follow) 63 - } 66 + m.fanout("DeleteFollow", ctx, follow) 64 67 } 65 68 66 69 func (m *mergedNotifier) NewPull(ctx context.Context, pull *models.Pull) { 67 - for _, notifier := range m.notifiers { 68 - notifier.NewPull(ctx, pull) 69 - } 70 + m.fanout("NewPull", ctx, pull) 70 71 } 72 + 71 73 func (m *mergedNotifier) NewPullComment(ctx context.Context, comment *models.PullComment) { 72 - for _, notifier := range m.notifiers { 73 - notifier.NewPullComment(ctx, comment) 74 - } 74 + m.fanout("NewPullComment", ctx, comment) 75 75 } 76 76 77 77 func (m *mergedNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) { 78 - for _, notifier := range m.notifiers { 79 - notifier.NewPullMerged(ctx, pull) 80 - } 78 + m.fanout("NewPullMerged", ctx, pull) 81 79 } 82 80 83 81 func (m *mergedNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) { 84 - for _, notifier := range m.notifiers { 85 - notifier.NewPullClosed(ctx, pull) 86 - } 82 + m.fanout("NewPullClosed", ctx, pull) 87 83 } 88 84 89 85 func (m *mergedNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) { 90 - for _, notifier := range m.notifiers { 91 - notifier.UpdateProfile(ctx, profile) 92 - } 86 + m.fanout("UpdateProfile", ctx, profile) 93 87 } 94 88 95 - func (m *mergedNotifier) NewString(ctx context.Context, string *models.String) { 96 - for _, notifier := range m.notifiers { 97 - notifier.NewString(ctx, string) 98 - } 89 + func (m *mergedNotifier) NewString(ctx context.Context, s *models.String) { 90 + m.fanout("NewString", ctx, s) 99 91 } 100 92 101 - func (m *mergedNotifier) EditString(ctx context.Context, string *models.String) { 102 - for _, notifier := range m.notifiers { 103 - notifier.EditString(ctx, string) 104 - } 93 + func (m *mergedNotifier) EditString(ctx context.Context, s *models.String) { 94 + m.fanout("EditString", ctx, s) 105 95 } 106 96 107 97 func (m *mergedNotifier) DeleteString(ctx context.Context, did, rkey string) { 108 - for _, notifier := range m.notifiers { 109 - notifier.DeleteString(ctx, did, rkey) 110 - } 98 + m.fanout("DeleteString", ctx, did, rkey) 111 99 }