forked from cloudwego/kitex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_stream.go
77 lines (69 loc) · 3.2 KB
/
option_stream.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
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package server
import (
"context"
"fmt"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/utils"
)
// WithRecvMiddleware adds middleware for server to handle response.
// It's used for intercepting stream.RecvMsg (called by Recv or CloseAndRecv) calls
func WithRecvMiddleware(mw endpoint.RecvMiddleware) Option {
mwb := func(ctx context.Context) endpoint.RecvMiddleware {
return mw
}
return Option{F: func(o *Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithRecvMiddleware(%+v)", utils.GetFuncName(mw)))
o.Streaming.RecvMiddlewareBuilders = append(o.Streaming.RecvMiddlewareBuilders, mwb)
}}
}
// WithRecvMiddlewareBuilder adds middleware that depend on a per-server context for server to handle response
func WithRecvMiddlewareBuilder(mwb endpoint.RecvMiddlewareBuilder) Option {
return Option{F: func(o *Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithRecvMiddlewareBuilder(%+v)", utils.GetFuncName(mwb)))
o.Streaming.RecvMiddlewareBuilders = append(o.Streaming.RecvMiddlewareBuilders, mwb)
}}
}
// WithSendMiddleware adds middleware for server to handle request.
// It's used for intercepting stream.SendMsg (called by Send or SendAndClose) calls
func WithSendMiddleware(mw endpoint.SendMiddleware) Option {
mwb := func(ctx context.Context) endpoint.SendMiddleware {
return mw
}
return Option{F: func(o *Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithSendMiddleware(%+v)", utils.GetFuncName(mw)))
o.Streaming.SendMiddlewareBuilders = append(o.Streaming.SendMiddlewareBuilders, mwb)
}}
}
// WithSendMiddlewareBuilder adds middleware that depend on a per-server context for server to handle request
func WithSendMiddlewareBuilder(mwb endpoint.SendMiddlewareBuilder) Option {
return Option{F: func(o *Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithSendMiddlewareBuilder(%+v)", utils.GetFuncName(mwb)))
o.Streaming.SendMiddlewareBuilders = append(o.Streaming.SendMiddlewareBuilders, mwb)
}}
}
// WithCompatibleMiddlewareForUnary allows Unary APIs to use the same middleware as non-streaming APIs
// For thrift unary APIs over HTTP2, it's enabled by default.
// For grpc(protobuf) streaming, it's disabled by default for backward compatibility, and can be enabled manually.
// With this option, the req/resp passed to the middleware are the real args and result;
// For Unary APIs requests, recv/send middlewares will be skipped (still effective for other streaming API requests)
func WithCompatibleMiddlewareForUnary() Option {
return Option{F: func(o *Options, di *utils.Slice) {
di.Push("WithCompatibleMiddlewareForUnary")
o.RemoteOpt.CompatibleMiddlewareForUnary = true
}}
}