@@ -6,53 +6,95 @@ import (
66 "google.golang.org/grpc"
77)
88
9- type contextModifierMiddleware struct {
10- cc grpc.ClientConnInterface
11- modifyCtx func (ctx context.Context ) context.Context
9+ var _ grpc.ClientConnInterface = (* middleware )(nil )
10+
11+ type (
12+ invoker func (context.Context , string , interface {}, interface {}, ... grpc.CallOption ) error
13+ streamer func (context.Context , * grpc.StreamDesc , string , ... grpc.CallOption ) (grpc.ClientStream , error )
14+ )
15+
16+ type middleware struct {
17+ invoke invoker
18+ newStream streamer
1219}
1320
14- func (c * contextModifierMiddleware ) Invoke (
21+ func (m * middleware ) Invoke (
1522 ctx context.Context , method string , args interface {}, reply interface {}, opts ... grpc.CallOption ,
1623) error {
17- return c . cc . Invoke ( c . modifyCtx ( ctx ) , method , args , reply , opts ... )
24+ return m . invoke ( ctx , method , args , reply , opts ... )
1825}
1926
20- func (c * contextModifierMiddleware ) NewStream (
27+ func (m * middleware ) NewStream (
2128 ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ,
2229) (grpc.ClientStream , error ) {
23- return c . cc . NewStream ( c . modifyCtx ( ctx ) , desc , method , opts ... )
30+ return m . newStream ( ctx , desc , method , opts ... )
2431}
2532
2633func WithContextModifier (
2734 cc grpc.ClientConnInterface ,
2835 modifyCtx func (ctx context.Context ) context.Context ,
2936) grpc.ClientConnInterface {
30- return & contextModifierMiddleware {
31- cc : cc ,
32- modifyCtx : modifyCtx ,
37+ return & middleware {
38+ invoke : func (ctx context.Context , method string , args interface {}, reply interface {}, opts ... grpc.CallOption ) error {
39+ ctx = modifyCtx (ctx )
40+ return cc .Invoke (ctx , method , args , reply , opts ... )
41+ },
42+ newStream : func (ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ) (
43+ grpc.ClientStream , error ,
44+ ) {
45+ ctx = modifyCtx (ctx )
46+ return cc .NewStream (ctx , desc , method , opts ... )
47+ },
3348 }
3449}
3550
36- type optionsAppenderMiddleware struct {
37- cc grpc.ClientConnInterface
38- opts []grpc.CallOption
39- }
40-
41- func (c * optionsAppenderMiddleware ) Invoke (
42- ctx context.Context , method string , args interface {}, reply interface {}, opts ... grpc.CallOption ,
43- ) error {
44- return c .cc .Invoke (ctx , method , args , reply , append (opts , c .opts ... )... )
51+ func WithAppendOptions (cc grpc.ClientConnInterface , appendOpts ... grpc.CallOption ) grpc.ClientConnInterface {
52+ return & middleware {
53+ invoke : func (ctx context.Context , method string , args interface {}, reply interface {}, opts ... grpc.CallOption ) error {
54+ opts = append (opts , appendOpts ... )
55+ return cc .Invoke (ctx , method , args , reply , opts ... )
56+ },
57+ newStream : func (ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ) (
58+ grpc.ClientStream , error ,
59+ ) {
60+ opts = append (opts , appendOpts ... )
61+ return cc .NewStream (ctx , desc , method , opts ... )
62+ },
63+ }
4564}
4665
47- func (c * optionsAppenderMiddleware ) NewStream (
48- ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ,
49- ) (grpc.ClientStream , error ) {
50- return c .cc .NewStream (ctx , desc , method , append (opts , c .opts ... )... )
66+ func WithBeforeFunc (
67+ cc grpc.ClientConnInterface ,
68+ before func (),
69+ ) grpc.ClientConnInterface {
70+ return & middleware {
71+ invoke : func (ctx context.Context , method string , args interface {}, reply interface {}, opts ... grpc.CallOption ) error {
72+ before ()
73+ return cc .Invoke (ctx , method , args , reply , opts ... )
74+ },
75+ newStream : func (ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ) (
76+ grpc.ClientStream , error ,
77+ ) {
78+ before ()
79+ return cc .NewStream (ctx , desc , method , opts ... )
80+ },
81+ }
5182}
5283
53- func WithAppendOptions (cc grpc.ClientConnInterface , opts ... grpc.CallOption ) grpc.ClientConnInterface {
54- return & optionsAppenderMiddleware {
55- cc : cc ,
56- opts : opts ,
84+ func WithAfterFunc (
85+ cc grpc.ClientConnInterface ,
86+ after func (),
87+ ) grpc.ClientConnInterface {
88+ return & middleware {
89+ invoke : func (ctx context.Context , method string , args interface {}, reply interface {}, opts ... grpc.CallOption ) error {
90+ defer after ()
91+ return cc .Invoke (ctx , method , args , reply , opts ... )
92+ },
93+ newStream : func (ctx context.Context , desc * grpc.StreamDesc , method string , opts ... grpc.CallOption ) (
94+ grpc.ClientStream , error ,
95+ ) {
96+ defer after ()
97+ return cc .NewStream (ctx , desc , method , opts ... )
98+ },
5799 }
58100}
0 commit comments