refactor grpc-proxy backends to enhance responses from local backends with metadata in one2many mode

This commit is contained in:
Pavel Sviderski
2024-12-04 14:47:31 +10:00
parent 9755a6973d
commit 00dbbec62b
3 changed files with 159 additions and 148 deletions
+6 -12
View File
@@ -9,8 +9,9 @@ import (
"sync"
)
// LocalBackend is a proxy.Backend implementation that proxies to a local gRPC server listening on a Unix socket.
// LocalBackend is a proxy.One2ManyResponder implementation that proxies to a local gRPC server listening on a Unix socket.
type LocalBackend struct {
One2ManyResponder
sockPath string
mu sync.RWMutex
@@ -22,12 +23,15 @@ var _ proxy.Backend = (*LocalBackend)(nil)
// NewLocalBackend returns a new LocalBackend for the given Unix socket path.
func NewLocalBackend(sockPath string) *LocalBackend {
return &LocalBackend{
One2ManyResponder: One2ManyResponder{
machine: "local",
},
sockPath: sockPath,
}
}
func (b *LocalBackend) String() string {
return "local"
return b.machine
}
// GetConnection returns a gRPC connection to the local server listening on the Unix socket.
@@ -57,16 +61,6 @@ func (b *LocalBackend) GetConnection(ctx context.Context, _ string) (context.Con
return outCtx, b.conn, err
}
// AppendInfo is called to enhance response from the backend with additional data.
func (b *LocalBackend) AppendInfo(_ bool, resp []byte) ([]byte, error) {
return resp, nil
}
// BuildError is called to convert error from upstream into response field.
func (b *LocalBackend) BuildError(bool, error) ([]byte, error) {
return nil, nil
}
// Close closes the upstream gRPC connection.
func (b *LocalBackend) Close() {
b.mu.Lock()