grpc conn reuse

This commit is contained in:
wangchuxiao
2023-05-30 16:15:11 +08:00
parent 1b109964ff
commit 85bf1f44e0
20 changed files with 160 additions and 267 deletions
+8 -7
View File
@@ -8,22 +8,23 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/conversation"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
)
func NewConversation(c discoveryregistry.SvcDiscoveryRegistry) *Conversation {
return &Conversation{c: c}
conn, err := c.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImConversationName)
if err != nil {
panic(err)
}
return &Conversation{conn: conn}
}
type Conversation struct {
c discoveryregistry.SvcDiscoveryRegistry
conn *grpc.ClientConn
}
func (o *Conversation) client(ctx context.Context) (conversation.ConversationClient, error) {
conn, err := o.c.GetConn(ctx, config.Config.RpcRegisterName.OpenImConversationName)
if err != nil {
return nil, err
}
return conversation.NewConversationClient(conn), nil
return conversation.NewConversationClient(o.conn), nil
}
func (o *Conversation) GetAllConversations(c *gin.Context) {
+8 -7
View File
@@ -7,24 +7,25 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/friend"
"google.golang.org/grpc"
"github.com/gin-gonic/gin"
)
func NewFriend(c discoveryregistry.SvcDiscoveryRegistry) *Friend {
return &Friend{c: c}
conn, err := c.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImFriendName)
if err != nil {
panic(err)
}
return &Friend{conn: conn}
}
type Friend struct {
c discoveryregistry.SvcDiscoveryRegistry
conn *grpc.ClientConn
}
func (o *Friend) client(ctx context.Context) (friend.FriendClient, error) {
conn, err := o.c.GetConn(ctx, config.Config.RpcRegisterName.OpenImFriendName)
if err != nil {
return nil, err
}
return friend.NewFriendClient(conn), nil
return friend.NewFriendClient(o.conn), nil
}
func (o *Friend) ApplyToAddFriend(c *gin.Context) {
+8 -9
View File
@@ -5,28 +5,27 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/group"
"google.golang.org/grpc"
"github.com/gin-gonic/gin"
)
func NewGroup(c discoveryregistry.SvcDiscoveryRegistry) *Group {
return &Group{c: c}
conn, err := c.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImGroupName)
if err != nil {
panic(err)
}
return &Group{conn: conn}
}
type Group struct {
c discoveryregistry.SvcDiscoveryRegistry
conn *grpc.ClientConn
}
func (o *Group) client(ctx context.Context) (group.GroupClient, error) {
conn, err := o.c.GetConn(ctx, config.Config.RpcRegisterName.OpenImGroupName)
if err != nil {
return nil, err
}
log.ZDebug(ctx, "get conn", "local", o.c.GetClientLocalConns())
return group.NewGroupClient(conn), nil
return group.NewGroupClient(o.conn), nil
}
func (o *Group) NewCreateGroup(c *gin.Context) {
+9 -13
View File
@@ -17,15 +17,20 @@ import (
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)
func NewMsg(c discoveryregistry.SvcDiscoveryRegistry) *Message {
return &Message{c: c, validate: validator.New()}
conn, err := c.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImMsgName)
if err != nil {
panic(err)
}
return &Message{conn: conn, validate: validator.New()}
}
type Message struct {
c discoveryregistry.SvcDiscoveryRegistry
conn *grpc.ClientConn
validate *validator.Validate
}
@@ -104,11 +109,7 @@ func (m Message) newUserSendMsgReq(c *gin.Context, params *apistruct.ManagementS
}
func (m *Message) client(ctx context.Context) (msg.MsgClient, error) {
conn, err := m.c.GetConn(ctx, config.Config.RpcRegisterName.OpenImMsgName)
if err != nil {
return nil, err
}
return msg.NewMsgClient(conn), nil
return msg.NewMsgClient(m.conn), nil
}
func (m *Message) GetSeq(c *gin.Context) {
@@ -208,12 +209,7 @@ func (m *Message) SendMessage(c *gin.Context) {
return
}
pbReq := m.newUserSendMsgReq(c, &params)
conn, err := m.c.GetConn(c, config.Config.RpcRegisterName.OpenImMsgName)
if err != nil {
apiresp.GinError(c, errs.ErrInternalServer)
return
}
client := msg.NewMsgClient(conn)
client := msg.NewMsgClient(m.conn)
var status int
respPb, err := client.SendMsg(c, pbReq)
if err != nil {
+9 -8
View File
@@ -14,22 +14,23 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/third"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
)
func NewThird(c discoveryregistry.SvcDiscoveryRegistry) *Third {
return &Third{c: c}
func NewThird(discov discoveryregistry.SvcDiscoveryRegistry) *Third {
conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImThirdName)
if err != nil {
panic(err)
}
return &Third{conn: conn}
}
type Third struct {
c discoveryregistry.SvcDiscoveryRegistry
conn *grpc.ClientConn
}
func (o *Third) client(ctx context.Context) (third.ThirdClient, error) {
conn, err := o.c.GetConn(ctx, config.Config.RpcRegisterName.OpenImThirdName)
if err != nil {
return nil, err
}
return third.NewThirdClient(conn), nil
return third.NewThirdClient(o.conn), nil
}
func (o *Third) ApplyPut(c *gin.Context) {
+10 -9
View File
@@ -7,6 +7,7 @@ import (
"github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tokenverify"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
"google.golang.org/grpc"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
@@ -15,20 +16,20 @@ import (
"github.com/gin-gonic/gin"
)
func NewUser(client discoveryregistry.SvcDiscoveryRegistry) *User {
return &User{c: client}
func NewUser(discov discoveryregistry.SvcDiscoveryRegistry) *User {
conn, err := discov.GetConn(context.Background(), config.Config.RpcRegisterName.OpenImThirdName)
if err != nil {
panic(err)
}
return &User{conn: conn}
}
type User struct {
c discoveryregistry.SvcDiscoveryRegistry
conn *grpc.ClientConn
}
func (u *User) client(ctx context.Context) (user.UserClient, error) {
conn, err := u.c.GetConn(ctx, config.Config.RpcRegisterName.OpenImUserName)
if err != nil {
return nil, err
}
return user.NewUserClient(conn), nil
return user.NewUserClient(u.conn), nil
}
func (u *User) UserRegister(c *gin.Context) {
@@ -58,6 +59,7 @@ func (u *User) AccountCheck(c *gin.Context) {
func (u *User) GetUsers(c *gin.Context) {
a2r.Call(user.UserClient.GetPaginationUsers, u.client, c)
}
func (u *User) GetUsersOnlineStatus(c *gin.Context) {
params := apistruct.ManagementSendMsgReq{}
if err := c.BindJSON(&params); err != nil {
@@ -68,5 +70,4 @@ func (u *User) GetUsersOnlineStatus(c *gin.Context) {
apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
return
}
}