-
Notifications
You must be signed in to change notification settings - Fork 788
/
custom_meta_handler.go
113 lines (99 loc) · 3.73 KB
/
custom_meta_handler.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright 2024 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 remote
import (
"context"
)
var (
_ MetaHandler = (*customMetaHandler)(nil)
_ StreamingMetaHandler = (*customMetaHandler)(nil)
)
// CustomMetaHandlerOption is the option for initializing customMetaHandler
type CustomMetaHandlerOption func(*customMetaHandler)
// NewCustomMetaHandler is used to initialize a customMetaHandler with only the needed functions
// Usage example:
//
// serverOption := server.WithMetaHandler(remote.NewCustomMetaHandler(remote.WithOnReadStream(
// func(ctx context.Context) (context.Context, error) {
// return context.WithValue(ctx, "key", "value"), nil
// },
// )))
func NewCustomMetaHandler(opts ...CustomMetaHandlerOption) MetaHandler {
handler := &customMetaHandler{}
for _, opt := range opts {
opt(handler)
}
return handler
}
// WithOnReadStream is used to set the onReadStream function of customMetaHandler
func WithOnReadStream(fn func(ctx context.Context) (context.Context, error)) CustomMetaHandlerOption {
return func(handler *customMetaHandler) {
handler.onReadStream = fn
}
}
// WithOnConnectStream is used to set the onConnectStream function of customMetaHandler
func WithOnConnectStream(fn func(ctx context.Context) (context.Context, error)) CustomMetaHandlerOption {
return func(handler *customMetaHandler) {
handler.onConnectStream = fn
}
}
// WithWriteMeta is used to set the writeMeta function of customMetaHandler
func WithWriteMeta(fn func(ctx context.Context, msg Message) (context.Context, error)) CustomMetaHandlerOption {
return func(handler *customMetaHandler) {
handler.writeMeta = fn
}
}
// WithReadMeta is used to set the readMeta function of customMetaHandler
func WithReadMeta(fn func(ctx context.Context, msg Message) (context.Context, error)) CustomMetaHandlerOption {
return func(handler *customMetaHandler) {
handler.readMeta = fn
}
}
// customMetaHandler is useful when you only want to implement only part of the methods
type customMetaHandler struct {
writeMeta func(ctx context.Context, msg Message) (context.Context, error)
readMeta func(ctx context.Context, msg Message) (context.Context, error)
onReadStream func(ctx context.Context) (context.Context, error)
onConnectStream func(ctx context.Context) (context.Context, error)
}
// WriteMeta implements the MetaHandler interface.
func (c *customMetaHandler) WriteMeta(ctx context.Context, msg Message) (context.Context, error) {
if c.writeMeta != nil {
return c.writeMeta(ctx, msg)
}
return ctx, nil
}
// ReadMeta implements the MetaHandler interface.
func (c *customMetaHandler) ReadMeta(ctx context.Context, msg Message) (context.Context, error) {
if c.readMeta != nil {
return c.readMeta(ctx, msg)
}
return ctx, nil
}
// OnConnectStream implements the StreamingMetaHandler interface.
func (c *customMetaHandler) OnConnectStream(ctx context.Context) (context.Context, error) {
if c.onConnectStream != nil {
return c.onConnectStream(ctx)
}
return ctx, nil
}
// OnReadStream implements the StreamingMetaHandler interface.
func (c *customMetaHandler) OnReadStream(ctx context.Context) (context.Context, error) {
if c.onReadStream != nil {
return c.onReadStream(ctx)
}
return ctx, nil
}