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