-
Notifications
You must be signed in to change notification settings - Fork 357
/
mq.go
94 lines (78 loc) · 2.61 KB
/
mq.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
// Copyright (c) 2021-present, Teamgram Studio (https://teamgram.io).
// All rights reserved.
//
// Author: teamgramio (teamgram.io@gmail.com)
//
package mq
import (
"context"
"encoding/json"
"fmt"
"strings"
kafka "github.com/teamgram/marmota/pkg/mq"
"github.com/teamgram/teamgram-server/app/messenger/sync/internal/core"
"github.com/teamgram/teamgram-server/app/messenger/sync/internal/svc"
"github.com/teamgram/teamgram-server/app/messenger/sync/sync"
"github.com/gogo/protobuf/proto"
"github.com/zeromicro/go-zero/core/logx"
)
// New new a grpc server.
func New(svcCtx *svc.ServiceContext, conf kafka.KafkaConsumerConf) *kafka.ConsumerGroup {
s := kafka.MustKafkaConsumer(&conf)
s.RegisterHandlers(
conf.Topics[0],
func(ctx context.Context, key string, value []byte) {
logx.WithContext(ctx).Infof("key: %s, value: %s", key, value)
switch strings.Split(key, "#")[0] {
case proto.MessageName((*sync.TLSyncUpdatesMe)(nil)):
c := core.New(ctx, svcCtx)
r := new(sync.TLSyncUpdatesMe)
if err := json.Unmarshal(value, r); err != nil {
c.Logger.Error(err.Error())
return
}
c.Logger.Infof("sync.updatesMe - request: %s", r.DebugString())
c.SyncUpdatesMe(r)
case proto.MessageName((*sync.TLSyncUpdatesNotMe)(nil)):
c := core.New(ctx, svcCtx)
r := new(sync.TLSyncUpdatesNotMe)
if err := json.Unmarshal(value, r); err != nil {
c.Logger.Error(err.Error())
return
}
c.Logger.Infof("sync.updatesNotMe - request: %s", r.DebugString())
c.SyncUpdatesNotMe(r)
case proto.MessageName((*sync.TLSyncPushUpdates)(nil)):
c := core.New(ctx, svcCtx)
r := new(sync.TLSyncPushUpdates)
if err := json.Unmarshal(value, r); err != nil {
c.Logger.Error(err.Error())
return
}
c.Logger.Infof("sync.pushUpdates - request: %s", r.DebugString())
c.SyncPushUpdates(r)
case proto.MessageName((*sync.TLSyncPushRpcResult)(nil)):
c := core.New(ctx, svcCtx)
r := new(sync.TLSyncPushRpcResult)
if err := json.Unmarshal(value, r); err != nil {
c.Logger.Error(err.Error())
return
}
c.Logger.Infof("sync.pushRpcResult - request: %s", r.DebugString())
c.SyncPushRpcResult(r)
case proto.MessageName((*sync.TLSyncBroadcastUpdates)(nil)):
c := core.New(ctx, svcCtx)
r := new(sync.TLSyncBroadcastUpdates)
if err := json.Unmarshal(value, r); err != nil {
c.Logger.Error(err.Error())
return
}
c.Logger.Infof("sync.broadcastUpdates - request: %s", r.DebugString())
c.SyncBroadcastUpdates(r)
default:
err := fmt.Errorf("invalid key: %s", key)
logx.Error(err.Error())
}
})
return s
}