2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-01-19 00:51:49 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2018-02-06 21:03:09 +00:00
|
|
|
"fmt"
|
2018-01-19 00:51:49 +00:00
|
|
|
"io"
|
2018-02-01 01:35:21 +00:00
|
|
|
"sync"
|
2018-01-19 00:51:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StreamingRpcHeader is the first struct serialized after entering the
|
|
|
|
// streaming RPC mode. The header is used to dispatch to the correct method.
|
|
|
|
type StreamingRpcHeader struct {
|
|
|
|
// Method is the name of the method to invoke.
|
|
|
|
Method string
|
2018-02-06 21:03:09 +00:00
|
|
|
}
|
2018-01-21 01:19:55 +00:00
|
|
|
|
2018-02-06 21:03:09 +00:00
|
|
|
// StreamingRpcAck is used to acknowledge receiving the StreamingRpcHeader and
|
2018-03-11 18:43:21 +00:00
|
|
|
// routing to the requested handler.
|
2018-02-06 21:03:09 +00:00
|
|
|
type StreamingRpcAck struct {
|
2018-02-16 01:08:58 +00:00
|
|
|
// Error is used to return whether an error occurred establishing the
|
2018-02-06 21:03:09 +00:00
|
|
|
// streaming RPC. This error occurs before entering the RPC handler.
|
|
|
|
Error string
|
2018-01-19 00:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StreamingRpcHandler defines the handler for a streaming RPC.
|
2018-01-21 01:19:55 +00:00
|
|
|
type StreamingRpcHandler func(conn io.ReadWriteCloser)
|
2018-01-19 00:51:49 +00:00
|
|
|
|
2018-03-11 18:41:13 +00:00
|
|
|
// StreamingRpcRegistry is used to add and retrieve handlers
|
|
|
|
type StreamingRpcRegistry struct {
|
2018-01-19 00:51:49 +00:00
|
|
|
registry map[string]StreamingRpcHandler
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:41:13 +00:00
|
|
|
// NewStreamingRpcRegistry creates a new registry. All registrations of
|
2018-01-19 00:51:49 +00:00
|
|
|
// handlers should be done before retrieving handlers.
|
2018-03-11 18:41:13 +00:00
|
|
|
func NewStreamingRpcRegistry() *StreamingRpcRegistry {
|
|
|
|
return &StreamingRpcRegistry{
|
2018-01-19 00:51:49 +00:00
|
|
|
registry: make(map[string]StreamingRpcHandler),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register registers a new handler for the given method name
|
2018-03-11 18:41:13 +00:00
|
|
|
func (s *StreamingRpcRegistry) Register(method string, handler StreamingRpcHandler) {
|
2018-01-19 00:51:49 +00:00
|
|
|
s.registry[method] = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHandler returns a handler for the given method or an error if it doesn't exist.
|
2018-03-11 18:41:13 +00:00
|
|
|
func (s *StreamingRpcRegistry) GetHandler(method string) (StreamingRpcHandler, error) {
|
2018-01-19 00:51:49 +00:00
|
|
|
h, ok := s.registry[method]
|
|
|
|
if !ok {
|
2018-02-06 21:03:09 +00:00
|
|
|
return nil, fmt.Errorf("%s: %q", ErrUnknownMethod, method)
|
2018-01-19 00:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
2018-02-01 01:35:21 +00:00
|
|
|
|
|
|
|
// Bridge is used to just link two connections together and copy traffic
|
2018-02-13 22:54:27 +00:00
|
|
|
func Bridge(a, b io.ReadWriteCloser) {
|
2018-02-01 01:35:21 +00:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-01-14 20:46:35 +00:00
|
|
|
_, _ = io.Copy(a, b)
|
2018-02-01 01:35:21 +00:00
|
|
|
a.Close()
|
|
|
|
b.Close()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-01-14 20:46:35 +00:00
|
|
|
_, _ = io.Copy(b, a)
|
2018-02-01 01:35:21 +00:00
|
|
|
a.Close()
|
|
|
|
b.Close()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
}
|