Skip to content

Commit 6af370b

Browse files
authored
perf: replace gob with msgpack (#270)
1 parent 684b33c commit 6af370b

File tree

18 files changed

+105
-75
lines changed

18 files changed

+105
-75
lines changed

app/app.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/webhookx-io/webhookx/admin"
1111
"github.com/webhookx-io/webhookx/admin/api"
1212
"github.com/webhookx-io/webhookx/config"
13+
"github.com/webhookx-io/webhookx/constants"
1314
"github.com/webhookx-io/webhookx/db"
1415
"github.com/webhookx-io/webhookx/db/entities"
1516
"github.com/webhookx-io/webhookx/db/migrator"
@@ -321,13 +322,14 @@ func registerEventHandler(bus *eventbus.EventBus) {
321322
}
322323
bus.Broadcast(eventbus.EventCRUD, eventData)
323324
})
324-
bus.Subscribe(eventbus.EventCRUD, func(data interface{}) {
325-
eventData := data.(*eventbus.CrudData)
326-
err := mcache.Invalidate(context.TODO(), eventData.CacheKey)
325+
bus.Subscribe(eventbus.EventCRUD, func(d interface{}) {
326+
data := d.(*eventbus.CrudData)
327+
cacheKey := constants.CacheKeyFrom(data.CacheName)
328+
err := mcache.Invalidate(context.TODO(), cacheKey.Build(data.ID))
327329
if err != nil {
328-
zap.S().Errorf("failed to invalidate cache: key=%s %v", eventData.CacheKey, err)
330+
zap.S().Errorf("failed to invalidate cache: key=%s %v", cacheKey.Build(data.ID), err)
329331
}
330-
bus.Broadcast(fmt.Sprintf("%s.crud", eventData.Entity), eventData)
332+
bus.Broadcast(fmt.Sprintf("%s.crud", data.Entity), data)
331333
})
332334
}
333335

constants/cache_key.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package constants
2+
3+
import "strings"
4+
5+
// CacheKey cache key definition.
6+
// format "webhookx:<name>:<version>:<id>"
7+
type CacheKey struct {
8+
Name string
9+
Version string
10+
}
11+
12+
func (c CacheKey) Build(id string) string {
13+
var sb strings.Builder
14+
sb.WriteString("webhookx:")
15+
sb.WriteString(c.Name)
16+
sb.WriteString(":")
17+
sb.WriteString(c.Version)
18+
sb.WriteString(":")
19+
sb.WriteString(id)
20+
return sb.String()
21+
}
22+
23+
var (
24+
EventCacheKey = register(CacheKey{"events", "v1"})
25+
EndpointCacheKey = register(CacheKey{"endpoints", "v1"})
26+
EndpointPluginsKey = register(CacheKey{"endpoint_plugins", "v1"})
27+
SourcePluginsKey = register(CacheKey{"source_plugins", "v1"})
28+
SourceCacheKey = register(CacheKey{"sources", "v1"})
29+
WorkspaceCacheKey = register(CacheKey{"workspaces", "v1"})
30+
AttemptCacheKey = register(CacheKey{"attempts", "v1"})
31+
PluginCacheKey = register(CacheKey{"plugins", "v1"})
32+
AttemptDetailCacheKey = register(CacheKey{"attempt_details", "v1"})
33+
WorkspaceEndpointsKey = register(CacheKey{"workspaces_endpoints", "v1"})
34+
)
35+
36+
var registry = map[string]CacheKey{}
37+
38+
func register(ck CacheKey) CacheKey {
39+
registry[ck.Name] = ck
40+
return ck
41+
}
42+
43+
func CacheKeyFrom(name string) CacheKey {
44+
return registry[name]
45+
}

constants/constants.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package constants
22

33
import (
44
"github.com/webhookx-io/webhookx/config"
5-
"strings"
65
"time"
76
)
87

@@ -27,32 +26,6 @@ const (
2726
RequeueInterval = time.Second * 60
2827
)
2928

30-
type CacheKey string
31-
32-
func (c CacheKey) Build(id string) string {
33-
var sb strings.Builder
34-
sb.WriteString(Namespace)
35-
sb.WriteString(":")
36-
sb.WriteString(string(c))
37-
sb.WriteString(":")
38-
sb.WriteString(id)
39-
return sb.String()
40-
}
41-
42-
const (
43-
Namespace string = "webhookx"
44-
EventCacheKey CacheKey = "events"
45-
EndpointCacheKey CacheKey = "endpoints"
46-
EndpointPluginsKey CacheKey = "endpoint_plugins"
47-
SourcePluginsKey CacheKey = "source_plugins"
48-
SourceCacheKey CacheKey = "sources"
49-
WorkspaceCacheKey CacheKey = "workspaces"
50-
AttemptCacheKey CacheKey = "attempts"
51-
PluginCacheKey CacheKey = "plugins"
52-
AttemptDetailCacheKey CacheKey = "attempt_details"
53-
WorkspaceEndpointsKey CacheKey = "workspaces_endpoints"
54-
)
55-
5629
type Header struct {
5730
Name string
5831
Value string

db/dao/attempt_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewAttemptDao(db *sqlx.DB, bus *eventbus.EventBus, workspace bool) AttemptD
3333
EntityName: "attempt",
3434
Workspace: workspace,
3535
CachePropagate: false,
36-
CacheKey: constants.AttemptCacheKey,
36+
CacheName: constants.AttemptCacheKey.Name,
3737
}
3838
return &attemptDao{
3939
DAO: NewDAO[entities.Attempt](db, bus, opts),

db/dao/attempt_detail_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewAttemptDetailDao(db *sqlx.DB, bus *eventbus.EventBus, workspace bool) At
2323
EntityName: "attempt_detail",
2424
Workspace: workspace,
2525
CachePropagate: false,
26-
CacheKey: constants.AttemptDetailCacheKey,
26+
CacheName: constants.AttemptDetailCacheKey.Name,
2727
}
2828
return &attemptDetailDao{
2929
DAO: NewDAO[entities.AttemptDetail](db, bus, opts),

db/dao/dao.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
sq "github.com/Masterminds/squirrel"
1010
"github.com/jmoiron/sqlx"
11-
"github.com/webhookx-io/webhookx/constants"
1211
"github.com/webhookx-io/webhookx/db/errs"
1312
"github.com/webhookx-io/webhookx/db/query"
1413
"github.com/webhookx-io/webhookx/db/transaction"
@@ -51,7 +50,7 @@ type Options struct {
5150
EntityName string
5251
Workspace bool
5352
CachePropagate bool
54-
CacheKey constants.CacheKey
53+
CacheName string
5554
}
5655

5756
func NewDAO[T any](db *sqlx.DB, bus *eventbus.EventBus, opts Options) *DAO[T] {
@@ -381,10 +380,10 @@ func (dao *DAO[T]) Upsert(ctx context.Context, fields []string, entity *T) error
381380

382381
func (dao *DAO[T]) propagateEvent(id string, entity *T) {
383382
data := &eventbus.CrudData{
384-
ID: id,
385-
CacheKey: dao.opts.CacheKey.Build(id),
386-
Entity: dao.opts.EntityName,
387-
Data: utils.Must(json.Marshal(entity)),
383+
ID: id,
384+
CacheName: dao.opts.CacheName,
385+
Entity: dao.opts.EntityName,
386+
Data: utils.Must(json.Marshal(entity)),
388387
}
389388
wid := reflect.ValueOf(*entity).FieldByName("WorkspaceId")
390389
if wid.IsValid() {

db/dao/endpoint_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewEndpointDAO(db *sqlx.DB, bus *eventbus.EventBus, workspace bool) Endpoin
1717
EntityName: "endpoint",
1818
Workspace: workspace,
1919
CachePropagate: true,
20-
CacheKey: constants.EndpointCacheKey,
20+
CacheName: constants.EndpointCacheKey.Name,
2121
}
2222
return &endpointDAO{
2323
DAO: NewDAO[entities.Endpoint](db, bus, opts),

db/dao/event_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewEventDao(db *sqlx.DB, bus *eventbus.EventBus, workspace bool) EventDAO {
2020
EntityName: "event",
2121
Workspace: workspace,
2222
CachePropagate: false,
23-
CacheKey: constants.EventCacheKey,
23+
CacheName: constants.EventCacheKey.Name,
2424
}
2525
return &eventDao{
2626
DAO: NewDAO[entities.Event](db, bus, opts),

db/dao/plugin_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewPluginDAO(db *sqlx.DB, bus *eventbus.EventBus, workspace bool) PluginDAO
2020
EntityName: "plugin",
2121
Workspace: workspace,
2222
CachePropagate: true,
23-
CacheKey: constants.PluginCacheKey,
23+
CacheName: constants.PluginCacheKey.Name,
2424
}
2525
return &pluginDAO{
2626
DAO: NewDAO[entities.Plugin](db, bus, opts),

db/dao/source_dao.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewSourceDAO(db *sqlx.DB, bus *eventbus.EventBus, workspace bool) SourceDAO
1717
EntityName: "source",
1818
Workspace: workspace,
1919
CachePropagate: true,
20-
CacheKey: constants.SourceCacheKey,
20+
CacheName: constants.SourceCacheKey.Name,
2121
}
2222
return &sourceDAO{
2323
DAO: NewDAO[entities.Source](db, bus, opts),

0 commit comments

Comments
 (0)