forked from ThreeDotsLabs/watermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (117 loc) · 2.93 KB
/
main.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
package main
import (
"context"
stdSQL "database/sql"
"encoding/json"
"os"
"os/signal"
"syscall"
"github.com/ThreeDotsLabs/watermill/message"
driver "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill-sql/v3/pkg/sql"
)
const topic = "counter"
func main() {
db := createDB()
logger := watermill.NewStdLogger(false, false)
go runWatermillRouter(db, logger)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// no graceful shutdown, to increase chance of problems :-)
<-sigs
}
type messagePayload struct {
CounterUUID string `json:"counter_uuid"`
}
func runWatermillRouter(db *stdSQL.DB, logger watermill.LoggerAdapter) {
subscriber, err := sql.NewSubscriber(
db,
sql.SubscriberConfig{
SchemaAdapter: sql.DefaultMySQLSchema{},
OffsetsAdapter: sql.DefaultMySQLOffsetsAdapter{},
InitializeSchema: true,
},
logger,
)
if err != nil {
panic(err)
}
router, err := message.NewRouter(message.RouterConfig{}, logger)
if err != nil {
panic(err)
}
router.AddNoPublisherHandler(
"counter",
topic,
subscriber,
processMessage,
)
if err := router.Run(context.Background()); err != nil {
panic(err)
}
}
func processMessage(msg *message.Message) error {
tx, ok := sql.TxFromContext(msg.Context())
if !ok {
return errors.New("tx not found in message context")
}
payload := messagePayload{}
err := json.Unmarshal(msg.Payload, &payload)
if err != nil {
return errors.Wrap(err, "unable to unmarshal payload")
}
// let's do it more fragile, let's get the value from DB instead of simple increment
counterValue, err := dbCounterValue(msg.Context(), tx, payload.CounterUUID)
if err != nil {
return err
}
counterValue += 1
if err := updateDbCounter(msg.Context(), tx, payload.CounterUUID, counterValue); err != nil {
return err
}
return nil
}
func updateDbCounter(ctx context.Context, tx *stdSQL.Tx, counterUUD string, counterValue int) error {
_, err := tx.ExecContext(
ctx,
"INSERT INTO counter (id, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = ?",
counterUUD,
counterValue,
counterValue,
)
if err != nil {
return errors.Wrap(err, "can't update counter value")
}
return nil
}
func dbCounterValue(ctx context.Context, tx *stdSQL.Tx, counterUUID string) (int, error) {
var counterValue int
row := tx.QueryRowContext(ctx, "SELECT value from counter WHERE id = ?", counterUUID)
if err := row.Scan(&counterValue); err != nil {
switch err {
case stdSQL.ErrNoRows:
return 0, nil
default:
return 0, errors.Wrap(err, "can't get counter value")
}
}
return counterValue, nil
}
func createDB() *stdSQL.DB {
conf := driver.NewConfig()
conf.Net = "tcp"
conf.User = "root"
conf.Addr = "mysql"
conf.DBName = "example"
db, err := stdSQL.Open("mysql", conf.FormatDSN())
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
return db
}