2018-12-11 22:01:50 +01:00
|
|
|
package local
|
2018-09-24 14:43:53 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
|
|
|
"github.com/zrepl/zrepl/config"
|
2018-12-11 22:01:50 +01:00
|
|
|
"github.com/zrepl/zrepl/transport"
|
2019-03-22 19:41:12 +01:00
|
|
|
"github.com/zrepl/zrepl/util/socketpair"
|
2018-09-24 14:43:53 +02:00
|
|
|
)
|
|
|
|
|
2018-10-11 13:06:47 +02:00
|
|
|
var localListeners struct {
|
2019-03-22 19:41:12 +01:00
|
|
|
m map[string]*LocalListener // listenerName -> listener
|
2018-10-11 13:06:47 +02:00
|
|
|
init sync.Once
|
2019-03-22 19:41:12 +01:00
|
|
|
mtx sync.Mutex
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
func GetLocalListener(listenerName string) *LocalListener {
|
2018-10-11 13:06:47 +02:00
|
|
|
|
|
|
|
localListeners.init.Do(func() {
|
|
|
|
localListeners.m = make(map[string]*LocalListener)
|
2018-09-24 14:43:53 +02:00
|
|
|
})
|
2018-10-11 13:06:47 +02:00
|
|
|
|
|
|
|
localListeners.mtx.Lock()
|
|
|
|
defer localListeners.mtx.Unlock()
|
|
|
|
|
|
|
|
l, ok := localListeners.m[listenerName]
|
|
|
|
if !ok {
|
|
|
|
l = newLocalListener()
|
|
|
|
localListeners.m[listenerName] = l
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type connectRequest struct {
|
|
|
|
clientIdentity string
|
2019-03-22 19:41:12 +01:00
|
|
|
callback chan connectResult
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type connectResult struct {
|
2018-12-11 22:01:50 +01:00
|
|
|
conn transport.Wire
|
2019-03-22 19:41:12 +01:00
|
|
|
err error
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 13:06:47 +02:00
|
|
|
type LocalListener struct {
|
2018-09-24 14:43:53 +02:00
|
|
|
connects chan connectRequest
|
|
|
|
}
|
|
|
|
|
2018-10-11 13:06:47 +02:00
|
|
|
func newLocalListener() *LocalListener {
|
|
|
|
return &LocalListener{
|
|
|
|
connects: make(chan connectRequest),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect to the LocalListener from a client with identity clientIdentity
|
2018-12-11 22:01:50 +01:00
|
|
|
func (l *LocalListener) Connect(dialCtx context.Context, clientIdentity string) (conn transport.Wire, err error) {
|
2018-09-24 14:43:53 +02:00
|
|
|
|
|
|
|
// place request
|
|
|
|
req := connectRequest{
|
|
|
|
clientIdentity: clientIdentity,
|
2019-06-20 14:37:21 +02:00
|
|
|
// ensure non-blocking send in Accept, we don't necessarily read from callback before
|
|
|
|
// Accept writes to it
|
|
|
|
callback: make(chan connectResult, 1),
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case l.connects <- req:
|
|
|
|
case <-dialCtx.Done():
|
|
|
|
return nil, dialCtx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for listener response
|
|
|
|
select {
|
2019-03-22 19:41:12 +01:00
|
|
|
case connRes := <-req.callback:
|
2018-09-24 14:43:53 +02:00
|
|
|
conn, err = connRes.conn, connRes.err
|
|
|
|
case <-dialCtx.Done():
|
|
|
|
close(req.callback) // sending to the channel afterwards will panic, the listener has to catch this
|
|
|
|
conn, err = nil, dialCtx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type localAddr struct {
|
|
|
|
S string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (localAddr) Network() string { return "local" }
|
|
|
|
|
|
|
|
func (a localAddr) String() string { return a.S }
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
func (l *LocalListener) Addr() net.Addr { return localAddr{"<listening>"} }
|
2018-09-24 14:43:53 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
func (l *LocalListener) Accept(ctx context.Context) (*transport.AuthConn, error) {
|
2018-09-24 14:43:53 +02:00
|
|
|
respondToRequest := func(req connectRequest, res connectResult) (err error) {
|
2019-06-20 14:37:21 +02:00
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).
|
2018-09-24 14:43:53 +02:00
|
|
|
WithField("res.conn", res.conn).WithField("res.err", res.err).
|
|
|
|
Debug("responding to client request")
|
2019-06-20 14:37:21 +02:00
|
|
|
|
2020-02-23 23:24:12 +01:00
|
|
|
// contract between Connect and Accept is that Connect sends a req.callback
|
2019-06-20 14:37:21 +02:00
|
|
|
// into which we can send one result non-blockingly.
|
|
|
|
// We want to panic if that contract is violated (impl error)
|
|
|
|
//
|
|
|
|
// However, Connect also supports timeouts through context cancellation:
|
|
|
|
// Connect closes the channel into which we might send, which will panic.
|
|
|
|
//
|
|
|
|
// ==> distinguish those cases in defer
|
|
|
|
const clientCallbackBlocked = "client-provided callback did block on send"
|
2018-09-24 14:43:53 +02:00
|
|
|
defer func() {
|
|
|
|
errv := recover()
|
2019-06-20 14:37:21 +02:00
|
|
|
if errv == clientCallbackBlocked {
|
2020-02-23 23:24:12 +01:00
|
|
|
// this would be a violation of contract between Connect and Accept, see above
|
2019-06-20 14:37:21 +02:00
|
|
|
panic(clientCallbackBlocked)
|
|
|
|
} else {
|
|
|
|
transport.GetLogger(ctx).WithField("recover_err", errv).
|
|
|
|
Debug("panic on send to client callback, likely a legitimate client-side timeout")
|
|
|
|
}
|
2018-09-24 14:43:53 +02:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case req.callback <- res:
|
|
|
|
err = nil
|
|
|
|
default:
|
2019-06-20 14:37:21 +02:00
|
|
|
panic(clientCallbackBlocked)
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
close(req.callback)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).Debug("waiting for local client connect requests")
|
2018-09-24 14:43:53 +02:00
|
|
|
var req connectRequest
|
|
|
|
select {
|
|
|
|
case req = <-l.connects:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).WithField("client_identity", req.clientIdentity).Debug("got connect request")
|
2018-09-24 14:43:53 +02:00
|
|
|
if req.clientIdentity == "" {
|
|
|
|
res := connectResult{nil, fmt.Errorf("client identity must not be empty")}
|
|
|
|
if err := respondToRequest(req, res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("client connected with empty client identity")
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).Debug("creating socketpair")
|
2018-10-11 21:17:43 +02:00
|
|
|
left, right, err := socketpair.SocketPair()
|
2018-09-24 14:43:53 +02:00
|
|
|
if err != nil {
|
|
|
|
res := connectResult{nil, fmt.Errorf("server error: %s", err)}
|
|
|
|
if respErr := respondToRequest(req, res); respErr != nil {
|
|
|
|
// returning the socketpair error properly is more important than the error sent to the client
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).WithError(respErr).Error("error responding to client")
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).Debug("responding with left side of socketpair")
|
2018-09-24 14:43:53 +02:00
|
|
|
res := connectResult{left, nil}
|
|
|
|
if err := respondToRequest(req, res); err != nil {
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).WithError(err).Error("error responding to client")
|
2018-09-24 14:43:53 +02:00
|
|
|
if err := left.Close(); err != nil {
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).WithError(err).Error("cannot close left side of socketpair")
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
if err := right.Close(); err != nil {
|
2018-12-11 22:01:50 +01:00
|
|
|
transport.GetLogger(ctx).WithError(err).Error("cannot close right side of socketpair")
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 22:01:50 +01:00
|
|
|
return transport.NewAuthConn(right, req.clientIdentity), nil
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|
|
|
|
|
2018-10-11 13:06:47 +02:00
|
|
|
func (l *LocalListener) Close() error {
|
2018-09-24 14:43:53 +02:00
|
|
|
// FIXME: make sure concurrent Accepts return with error, and further Accepts return that error, too
|
|
|
|
// Example impl: for each accept, do context.WithCancel, and store the cancel in a list
|
|
|
|
// When closing, set a member variable to state=closed, make sure accept will exit early
|
|
|
|
// and then call all cancels in the list
|
|
|
|
// The code path from Accept entry over check if state=closed to list entry must be protected by a mutex.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
func LocalListenerFactoryFromConfig(g *config.Global, in *config.LocalServe) (transport.AuthenticatedListenerFactory, error) {
|
2018-10-11 13:06:47 +02:00
|
|
|
if in.ListenerName == "" {
|
|
|
|
return nil, fmt.Errorf("ListenerName must not be empty")
|
|
|
|
}
|
2018-12-11 22:01:50 +01:00
|
|
|
listenerName := in.ListenerName
|
2019-03-22 19:41:12 +01:00
|
|
|
lf := func() (transport.AuthenticatedListener, error) {
|
2018-12-11 22:01:50 +01:00
|
|
|
return GetLocalListener(listenerName), nil
|
|
|
|
}
|
|
|
|
return lf, nil
|
2018-09-24 14:43:53 +02:00
|
|
|
}
|