You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.9 KiB
84 lines
1.9 KiB
package grpc
|
|
|
|
import (
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"github.com/go-kratos/kratos/v2/selector"
|
|
"github.com/go-kratos/kratos/v2/transport"
|
|
)
|
|
|
|
var _ transport.Transporter = (*Transport)(nil)
|
|
|
|
// Transport is a gRPC transport.
|
|
type Transport struct {
|
|
endpoint string
|
|
operation string
|
|
reqHeader headerCarrier
|
|
replyHeader headerCarrier
|
|
nodeFilters []selector.NodeFilter
|
|
}
|
|
|
|
// Kind returns the transport kind.
|
|
func (tr *Transport) Kind() transport.Kind {
|
|
return transport.KindGRPC
|
|
}
|
|
|
|
// Endpoint returns the transport endpoint.
|
|
func (tr *Transport) Endpoint() string {
|
|
return tr.endpoint
|
|
}
|
|
|
|
// Operation returns the transport operation.
|
|
func (tr *Transport) Operation() string {
|
|
return tr.operation
|
|
}
|
|
|
|
// RequestHeader returns the request header.
|
|
func (tr *Transport) RequestHeader() transport.Header {
|
|
return tr.reqHeader
|
|
}
|
|
|
|
// ReplyHeader returns the reply header.
|
|
func (tr *Transport) ReplyHeader() transport.Header {
|
|
return tr.replyHeader
|
|
}
|
|
|
|
// NodeFilters returns the client select filters.
|
|
func (tr *Transport) NodeFilters() []selector.NodeFilter {
|
|
return tr.nodeFilters
|
|
}
|
|
|
|
type headerCarrier metadata.MD
|
|
|
|
// Get returns the value associated with the passed key.
|
|
func (mc headerCarrier) Get(key string) string {
|
|
vals := metadata.MD(mc).Get(key)
|
|
if len(vals) > 0 {
|
|
return vals[0]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Set stores the key-value pair.
|
|
func (mc headerCarrier) Set(key string, value string) {
|
|
metadata.MD(mc).Set(key, value)
|
|
}
|
|
|
|
// Add append value to key-values pair.
|
|
func (mc headerCarrier) Add(key string, value string) {
|
|
metadata.MD(mc).Append(key, value)
|
|
}
|
|
|
|
// Keys lists the keys stored in this carrier.
|
|
func (mc headerCarrier) Keys() []string {
|
|
keys := make([]string, 0, len(mc))
|
|
for k := range metadata.MD(mc) {
|
|
keys = append(keys, k)
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// Values returns a slice of values associated with the passed key.
|
|
func (mc headerCarrier) Values(key string) []string {
|
|
return metadata.MD(mc).Get(key)
|
|
}
|
|
|