-
Notifications
You must be signed in to change notification settings - Fork 17
/
service_context.go
64 lines (59 loc) · 2.1 KB
/
service_context.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
package svc
import (
"github.com/apache/pulsar-client-go/pulsar"
"github.com/luxun9527/gex/app/match/mq/internal/config"
"github.com/luxun9527/gex/app/match/mq/internal/dao/model"
"github.com/luxun9527/gex/app/match/mq/internal/dao/query"
"github.com/luxun9527/gex/app/order/rpc/orderservice"
pulsarConfig "github.com/luxun9527/gex/common/pkg/pulsar"
"github.com/luxun9527/gex/common/proto/define"
gpushPb "github.com/luxun9527/gpush/proto"
logger "github.com/luxun9527/zaplog"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/zrpc"
)
type ServiceContext struct {
MatchConsumer pulsar.Consumer
Config config.Config
OrderClient orderservice.OrderService
Query *query.Query
RedisClient *redis.Redis
WsClient gpushPb.ProxyClient
MatchDataChan chan *model.MatchData
SymbolInfo *define.SymbolInfo
}
func NewServiceContext(c config.Config) *ServiceContext {
logger.InitZapLogger(&c.LoggerConfig)
logx.SetWriter(logger.NewZapWriter(logger.GetZapLogger()))
logx.DisableStat()
var symbolInfo define.SymbolInfo
define.InitSymbolConfig(define.EtcdSymbolPrefix+c.Symbol, c.SymbolEtcdConfig, &symbolInfo)
client, err := c.PulsarConfig.BuildClient()
if err != nil {
logx.Severef("init pulsar client failed err %v", err)
}
topic := pulsarConfig.Topic{
Tenant: pulsarConfig.PublicTenant,
Namespace: pulsarConfig.GexNamespace,
Topic: pulsarConfig.MatchResultTopic + "_" + c.Symbol,
}
consumer, err := client.Subscribe(pulsar.ConsumerOptions{
Topic: topic.BuildTopic(),
SubscriptionName: pulsarConfig.MatchResultMatchSub,
Type: pulsar.Shared,
})
if err != nil {
logx.Severef("init pulsar consumer failed %v", logger.ErrorField(err))
}
sc := &ServiceContext{
MatchConsumer: consumer,
Config: c,
Query: query.Use(c.GormConf.MustNewGormClient()),
WsClient: gpushPb.NewProxyClient(zrpc.MustNewClient(c.WsConf).Conn()),
RedisClient: redis.MustNewRedis(c.RedisConf),
MatchDataChan: make(chan *model.MatchData),
SymbolInfo: &symbolInfo,
}
return sc
}