Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
180 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/H-BF/corlib/pkg/conventions" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
// AddMetaToOutgoingUnaryInterceptor - specifies fixed headers for every rpc as kv array | ||
func AddMetaToOutgoingUnaryInterceptor(kv ...string) grpc.UnaryClientInterceptor { | ||
return func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { | ||
if len(kv) != 0 { | ||
ctx = metadata.AppendToOutgoingContext(ctx, kv...) | ||
} | ||
return invoker(ctx, method, req, reply, cc, opts...) | ||
} | ||
} | ||
|
||
// AddMetaToOutgoingStreamInterceptor - specifies fixed headers for every rpc as kv array | ||
func AddMetaToOutgoingStreamInterceptor(kv ...string) grpc.StreamClientInterceptor { | ||
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
ctx = metadata.AppendToOutgoingContext(ctx, kv...) | ||
return streamer(ctx, desc, cc, method, opts...) | ||
} | ||
} | ||
|
||
// HostNamePropagator add host name into outgoing metadata | ||
type HostNamePropagator string | ||
|
||
// ClientUnary unary client interceptor | ||
func (h HostNamePropagator) ClientUnary() grpc.UnaryClientInterceptor { | ||
return AddMetaToOutgoingUnaryInterceptor(conventions.HostNameHeader, string(h)) | ||
} | ||
|
||
// ClientStream stream client interceptor | ||
func (h HostNamePropagator) ClientStream() grpc.StreamClientInterceptor { | ||
return AddMetaToOutgoingStreamInterceptor(conventions.HostNameHeader, string(h)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/H-BF/corlib | ||
|
||
go 1.18 | ||
go 1.20 | ||
|
||
require ( | ||
github.com/cenkalti/backoff/v4 v4.1.3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.