github.com/searKing/golang/go@v1.2.117/exp/net/grpc/wrap.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package grpc
     6  
     7  import "context"
     8  
     9  func Wrap[REQ any, RESP any](next func(ctx context.Context, req REQ) (RESP, error),
    10  	wrappers ...func(handler func(ctx context.Context, req REQ) (RESP, error)) func(ctx context.Context, req REQ) (RESP, error)) func(
    11  	ctx context.Context, req REQ) (RESP, error) {
    12  	for i := range wrappers {
    13  		w := wrappers[len(wrappers)-1-i]
    14  		if w != nil {
    15  			next = w(next)
    16  		}
    17  	}
    18  	return next
    19  }