Commit 7f257399 by Li Feifei

结构修改

parent 008a4b79
package helper package middleware
import ( import (
"context" "context"
...@@ -9,7 +9,8 @@ import ( ...@@ -9,7 +9,8 @@ import (
"strconv" "strconv"
"time" "time"
"golang.org/x/time/rate" "im-microservice/helper"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
...@@ -25,7 +26,7 @@ func permissionError(msg string) error { ...@@ -25,7 +26,7 @@ func permissionError(msg string) error {
func checksum(req map[string]string) error { func checksum(req map[string]string) error {
err, AppSecret := GetSecretByKey(req["Appkey"]) err, AppSecret := helper.GetSecretByKey(req["Appkey"])
if err != nil { if err != nil {
return status.Error(codes.NotFound, "appkey不存在") return status.Error(codes.NotFound, "appkey不存在")
} }
...@@ -53,8 +54,7 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...@@ -53,8 +54,7 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) { handler grpc.UnaryHandler) (interface{}, error) {
if info.FullMethod != health_path { if info.FullMethod != health_path {
limiter := rate.NewLimiter(rate.Every(100*time.Millisecond), 1)
if limiter.Allow() {
request_map := make(map[string]string) request_map := make(map[string]string)
s := fmt.Sprintf("%v", req) s := fmt.Sprintf("%v", req)
...@@ -87,9 +87,6 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, ...@@ -87,9 +87,6 @@ func Auth(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
if err := checksum(request_map); err != nil { if err := checksum(request_map); err != nil {
return nil, err return nil, err
} }
} else {
return nil, status.Error(codes.Unauthenticated, "请求超过限制,请稍后再试")
}
} }
return handler(ctx, req) return handler(ctx, req)
......
package middleware
import (
"context"
"time"
"golang.org/x/time/rate"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func Interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
if info.FullMethod != health_path {
limiter := rate.NewLimiter(rate.Every(100*time.Millisecond), 1)
if limiter.Allow() {
return handler(ctx, req)
} else {
return nil, status.Error(codes.Unauthenticated, "请求超过限制,请稍后再试")
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment