github.com/blend/go-sdk@v1.20220411.3/grpcutil/unary_client_chain.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package grpcutil 9 10 import ( 11 "context" 12 13 "google.golang.org/grpc" 14 ) 15 16 // UnaryClientChain creates a single interceptor out of a chain of many interceptors. 17 // 18 // Execution is done in left-to-right order, including passing of context. 19 // For example ChainUnaryClient(one, two, three) will execute one before two before three. 20 func UnaryClientChain(interceptors ...grpc.UnaryClientInterceptor) grpc.UnaryClientInterceptor { 21 n := len(interceptors) 22 23 return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { 24 chainer := func(currentInter grpc.UnaryClientInterceptor, currentInvoker grpc.UnaryInvoker) grpc.UnaryInvoker { 25 return func(currentCtx context.Context, currentMethod string, currentReq, currentRepl interface{}, currentConn *grpc.ClientConn, currentOpts ...grpc.CallOption) error { 26 return currentInter(currentCtx, currentMethod, currentReq, currentRepl, currentConn, currentInvoker, currentOpts...) 27 } 28 } 29 chainedInvoker := invoker 30 for i := n - 1; i >= 0; i-- { 31 chainedInvoker = chainer(interceptors[i], chainedInvoker) 32 } 33 return chainedInvoker(ctx, method, req, reply, cc, opts...) 34 } 35 }