github.com/blend/go-sdk@v1.20220411.3/grpcutil/stream_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 // StreamClientChain 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 ChainStreamClient(one, two, three) will execute one before two before three. 20 func StreamClientChain(interceptors ...grpc.StreamClientInterceptor) grpc.StreamClientInterceptor { 21 n := len(interceptors) 22 23 return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { 24 chainer := func(currentInter grpc.StreamClientInterceptor, currentStreamer grpc.Streamer) grpc.Streamer { 25 return func(currentCtx context.Context, currentDesc *grpc.StreamDesc, currentConn *grpc.ClientConn, currentMethod string, currentOpts ...grpc.CallOption) (grpc.ClientStream, error) { 26 return currentInter(currentCtx, currentDesc, currentConn, currentMethod, currentStreamer, currentOpts...) 27 } 28 } 29 30 chainedStreamer := streamer 31 for i := n - 1; i >= 0; i-- { 32 chainedStreamer = chainer(interceptors[i], chainedStreamer) 33 } 34 35 return chainedStreamer(ctx, desc, cc, method, opts...) 36 } 37 }