-
Notifications
You must be signed in to change notification settings - Fork 62
/
event.go
170 lines (143 loc) · 4.09 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package canal
import (
"context"
"errors"
"io"
"sync/atomic"
"github.com/goccy/go-json"
"github.com/segmentio/kafka-go"
"go.uber.org/zap"
"github.com/bangumi/server/internal/config"
"github.com/bangumi/server/internal/model"
"github.com/bangumi/server/internal/pkg/errgo"
"github.com/bangumi/server/internal/search"
"github.com/bangumi/server/internal/web/session"
)
func newEventHandler(
log *zap.Logger,
appConfig config.AppConfig,
session session.Manager,
reader *kafka.Reader,
search search.Client,
) *eventHandler {
return &eventHandler{
config: appConfig,
session: session,
reader: reader,
search: search,
log: log.Named("eventHandler"),
}
}
type eventHandler struct {
closed atomic.Bool
config config.AppConfig
session session.Manager
log *zap.Logger
search search.Client
reader *kafka.Reader
}
func (e *eventHandler) start() error {
for {
if e.closed.Load() {
return nil
}
msg, err := e.reader.FetchMessage(context.Background())
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return errgo.Wrap(err, "read message")
}
e.log.Error("error fetching msg", zap.Error(err))
continue
}
e.log.Debug("new message", zap.String("topic", msg.Topic))
err = e.onMessage(msg)
if err != nil {
e.log.Error("failed to handle kafka msg", zap.Error(err), zap.String("topic", msg.Topic))
continue
}
_ = e.reader.CommitMessages(context.Background(), msg)
}
}
func (e *eventHandler) Close() error {
e.closed.Store(true)
return errgo.Wrap(e.reader.Close(), "kafka.Close")
}
func (e *eventHandler) OnUserPasswordChange(id model.UserID) error {
e.log.Info("user change password", id.Zap())
if err := e.session.RevokeUser(context.Background(), id); err != nil {
e.log.Error("failed to revoke user", id.Zap(), zap.Error(err))
return errgo.Wrap(err, "session.RevokeUser")
}
return nil
}
func (e *eventHandler) onMessage(msg kafka.Message) error {
if len(msg.Value) == 0 {
// fake event, just ignore
// https://debezium.io/documentation/reference/stable/connectors/mysql.html#mysql-tombstone-events
return nil
}
var k messageKey
if err := json.Unmarshal(msg.Key, &k); err != nil {
return nil
}
var v messageValue
if err := json.Unmarshal(msg.Value, &v); err != nil {
return nil
}
var err error
switch msg.Topic {
case "chii.bangumi.chii_subject_fields":
err = e.OnSubjectField(k.Payload, v.Payload)
case "chii.bangumi.chii_subjects":
err = e.OnSubject(k.Payload, v.Payload)
case "chii.bangumi.chii_members":
err = e.OnUserChange(k.Payload, v.Payload)
}
return err
}
const (
opCreate = "c"
opDelete = "d"
opUpdate = "u"
opSnapshot = "r" // just ignore them, production debezium disable snapshot.
)
// https://debezium.io/documentation/reference/connectors/mysql.html
// Table 9. Overview of change event basic content
type messageKey struct {
Payload json.RawMessage `json:"payload"`
Schema struct {
Type string `json:"type"`
Name string `json:"name"`
Fields []struct {
Type string `json:"type"`
Field string `json:"field"`
Optional bool `json:"optional"`
} `json:"fields"`
Optional bool `json:"optional"`
} `json:"schema"`
}
type messageValue struct {
Payload payload `json:"payload"`
}
type payload struct {
Before json.RawMessage `json:"before"`
After json.RawMessage `json:"after"`
Source source `json:"source"`
Op string `json:"op"`
}
type source struct {
Table string `json:"table"`
}