mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-26 19:46:57 +08:00
grpc interceptor
This commit is contained in:
parent
d18c7e2c52
commit
f5c80ffd50
@ -7,11 +7,12 @@ import (
|
|||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
27
pkg/common/mw/intercept_chain.go
Normal file
27
pkg/common/mw/intercept_chain.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package mw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InterceptChain(intercepts ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor {
|
||||||
|
l := len(intercepts)
|
||||||
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
|
chain := func(currentInter grpc.UnaryServerInterceptor, currentHandler grpc.UnaryHandler) grpc.UnaryHandler {
|
||||||
|
return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) {
|
||||||
|
return currentInter(
|
||||||
|
currentCtx,
|
||||||
|
currentReq,
|
||||||
|
info,
|
||||||
|
currentHandler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chainHandler := handler
|
||||||
|
for i := l - 1; i >= 0; i-- {
|
||||||
|
chainHandler = chain(intercepts[i], chainHandler)
|
||||||
|
}
|
||||||
|
return chainHandler(ctx, req)
|
||||||
|
}
|
||||||
|
}
|
@ -3,12 +3,13 @@ package mw
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
|
||||||
"math"
|
"math"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
"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/common/log"
|
||||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
|
||||||
|
@ -28,7 +28,6 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
fmt.Println(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, rpcRegisterName)
|
|
||||||
zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
|
zkClient, err := openKeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
|
||||||
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName,
|
openKeeper.WithFreq(time.Hour), openKeeper.WithUserNameAndPassword(config.Config.Zookeeper.UserName,
|
||||||
config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10))
|
config.Config.Zookeeper.Password), openKeeper.WithRoundRobin(), openKeeper.WithTimeout(10))
|
||||||
@ -46,10 +45,10 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c
|
|||||||
prome.NewGrpcRequestCounter()
|
prome.NewGrpcRequestCounter()
|
||||||
prome.NewGrpcRequestFailedCounter()
|
prome.NewGrpcRequestFailedCounter()
|
||||||
prome.NewGrpcRequestSuccessCounter()
|
prome.NewGrpcRequestSuccessCounter()
|
||||||
|
unaryInterceptor := mw.InterceptChain(grpcPrometheus.UnaryServerInterceptor, grpcPrometheus.UnaryServerInterceptor)
|
||||||
options = append(options, []grpc.ServerOption{
|
options = append(options, []grpc.ServerOption{
|
||||||
//grpc.UnaryInterceptor(prome.UnaryServerInterceptorPrometheus),
|
|
||||||
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
|
grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor),
|
||||||
grpc.UnaryInterceptor(grpcPrometheus.UnaryServerInterceptor),
|
grpc.UnaryInterceptor(unaryInterceptor),
|
||||||
}...)
|
}...)
|
||||||
}
|
}
|
||||||
srv := grpc.NewServer(options...)
|
srv := grpc.NewServer(options...)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user