Repository navigation
Add Unix domain socket support for Firebird 6 #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
79b18ba
2e8ea92
62517c1
8a0fc94
18f7766
e1eed3e
d1942ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,8 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "net" | ||
| "strconv" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/firebirdsql/fbx/fbproto" | ||
| "github.com/firebirdsql/fbx/fbproto/vax" | ||
|
|
@@ -42,31 +42,34 @@ func (fbConn *FbConn) ensureEventChannel(ctx context.Context) (*eventChannel, er | |
| return channel, nil | ||
| } | ||
|
|
||
| existingChannel, auxPort, err := fbConn.requestAuxEventPort(ctx) | ||
| // Keep the main connection busy until the server accepts the auxiliary | ||
| // connection or we cancel it. | ||
| if err := fbConn.lockEventOperation(ctx); err != nil { | ||
| return nil, err | ||
| } | ||
| defer fbConn.unlock() | ||
|
|
||
| existingChannel, host, port, err := fbConn.requestAuxEventAddress(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if existingChannel != nil { | ||
| return existingChannel, nil | ||
| } | ||
|
|
||
| host := fbConn.eventDialHost() | ||
| address := net.JoinHostPort(host, strconv.Itoa(auxPort)) | ||
| auxConn, err := fbConn.config.DialFunc(ctx, "tcp", address) | ||
| // A zero port identifies the Unix path returned by Firebird, which may be relative. | ||
| network, address := "unix", host | ||
| if port != 0 { | ||
| network, address = NetworkAddress(host, port) | ||
| } | ||
| auxConn, err := fbConn.config.DialFunc(ctx, network, address) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once Firebird has returned the auxiliary address, it is waiting for the second connection. If FBX rejects the address or the dial fails here, On protocols 13+, libfbclient sends
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both paths now close the connection and send op_abort_aux_connection on protocol 13+. The tests verify that Ping returns a connection-closed error before its deadline. I also covered the case where Firebird rejects the request before returning an address. In that case, Ping still succeeds. |
||
| if err != nil { | ||
| fbConn.abortAuxEventConnection() | ||
| return nil, err | ||
| } | ||
|
|
||
| channel = newEventChannel(auxConn) | ||
| fbConn.eventMu.Lock() | ||
| if fbConn.eventChannel != nil { | ||
| existingChannel = fbConn.eventChannel | ||
| fbConn.eventMu.Unlock() | ||
| if closeErr := channel.close(); closeErr != nil { | ||
| fbConn.setEventError(fmt.Errorf("event channel close failed: %w", closeErr)) | ||
| } | ||
| return existingChannel, nil | ||
| } | ||
| fbConn.eventChannel = channel | ||
| fbConn.eventErr = nil | ||
| if fbConn.eventHandles == nil { | ||
|
|
@@ -78,16 +81,11 @@ func (fbConn *FbConn) ensureEventChannel(ctx context.Context) (*eventChannel, er | |
| return channel, nil | ||
| } | ||
|
|
||
| func (fbConn *FbConn) requestAuxEventPort(ctx context.Context) (*eventChannel, int, error) { | ||
| if err := fbConn.lockEventOperation(ctx); err != nil { | ||
| return nil, 0, err | ||
| } | ||
| defer fbConn.unlock() | ||
|
|
||
| func (fbConn *FbConn) requestAuxEventAddress(ctx context.Context) (*eventChannel, string, uint16, error) { | ||
| if ctx != context.Background() { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return nil, 0, newContextAlreadyDoneError(ctx) | ||
| return nil, "", 0, newContextAlreadyDoneError(ctx) | ||
| default: | ||
| } | ||
| fbConn.contextWatcher.Watch(ctx) | ||
|
|
@@ -98,38 +96,65 @@ func (fbConn *FbConn) requestAuxEventPort(ctx context.Context) (*eventChannel, i | |
| channel := fbConn.eventChannel | ||
| fbConn.eventMu.Unlock() | ||
| if channel != nil { | ||
| return channel, 0, nil | ||
| return channel, "", 0, nil | ||
| } | ||
|
|
||
| if err := fbConn.client.WriteMessage(&fbproto.OpConnectRequest{}); err != nil { | ||
| if err := fbConn.withWriteLockAndFlush(func() error { | ||
| return fbConn.client.WriteMessage(&fbproto.OpConnectRequest{}) | ||
| }); err != nil { | ||
| fbConn.asyncClose() | ||
| return nil, 0, err | ||
| } | ||
| if err := fbConn.flushWithPotentialWriteReadDeadlock(); err != nil { | ||
| fbConn.asyncClose() | ||
| return nil, 0, err | ||
| return nil, "", 0, err | ||
| } | ||
|
|
||
| msg, err := fbConn.receiveMessage() | ||
| if err != nil { | ||
| fbConn.asyncCloseAfterRead(err) | ||
| return nil, 0, normalizeTimeoutError(ctx, err) | ||
| return nil, "", 0, normalizeTimeoutError(ctx, err) | ||
| } | ||
|
|
||
| switch m := msg.(type) { | ||
| case *fbproto.OpResponse: | ||
| if isAbsolutePath(fbConn.eventHost) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hijack now saves the selected host, and Construct restores it. I added a regression test that queues an event after reconstructing a Unix connection. |
||
| // Firebird sends the auxiliary socket path as raw bytes for Unix connections. | ||
| path := string(m.Data) | ||
| if path == "" { | ||
| fbConn.abortAuxEventConnection() | ||
| return nil, "", 0, errors.New("invalid auxiliary event socket path") | ||
| } | ||
| return nil, path, 0, nil | ||
| } | ||
| auxPort, parseErr := parseAuxEventPort(m.Data) | ||
| if parseErr != nil { | ||
| return nil, 0, parseErr | ||
| fbConn.abortAuxEventConnection() | ||
| return nil, "", 0, parseErr | ||
| } | ||
| return nil, auxPort, nil | ||
| return nil, fbConn.eventDialHost(), uint16(auxPort), nil | ||
| case *fbproto.ErrorResponse: | ||
| return nil, 0, ErrorResponseToFbError(m) | ||
| return nil, "", 0, ErrorResponseToFbError(m) | ||
| default: | ||
| return nil, 0, fmt.Errorf("unexpected response message type %T while requesting event channel", m) | ||
| fbConn.asyncClose() | ||
| return nil, "", 0, fmt.Errorf("unexpected response message type %T while requesting event channel", m) | ||
| } | ||
| } | ||
|
|
||
| // abortAuxEventConnection is called with the connection locked after the server | ||
| // has supplied an auxiliary address. The main connection is discarded even | ||
| // when the server does not support aborting the pending accept. | ||
| func (fbConn *FbConn) abortAuxEventConnection() { | ||
| defer fbConn.asyncClose() | ||
| if fbConn.protocolVersion < fbproto.PROTOCOL_VERSION13 { | ||
| return | ||
| } | ||
|
|
||
| fbConn.contextWatcher.Unwatch() | ||
| if err := fbConn.conn.SetWriteDeadline(time.Now().Add(cleanupGracePeriod)); err != nil { | ||
| return | ||
| } | ||
| _ = fbConn.withWriteLockAndFlush(func() error { | ||
| return fbConn.client.WriteMessage(&fbproto.OpAbortAuxConnection{}) | ||
| }) | ||
| } | ||
|
|
||
| func (fbConn *FbConn) eventDialHost() string { | ||
| host := fbConn.eventHost | ||
| if host == "" { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package fbconn | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestQueueEventAfterConstructUnix(t *testing.T) { | ||
| connString := os.Getenv("FBX_TEST_UNIX_SOCKET_DATABASE") | ||
| if connString == "" { | ||
| t.Skip("set FBX_TEST_UNIX_SOCKET_DATABASE to test Unix socket connections") | ||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) | ||
| defer cancel() | ||
|
|
||
| conn, err := Connect(ctx, connString) | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, conn.Close(ctx)) }() | ||
| if conn.ServerVersion().Major < 6 { | ||
| t.Skip("Unix domain sockets require Firebird 6 or later") | ||
| } | ||
|
|
||
| require.NoError(t, conn.SyncConn(ctx)) | ||
| hijacked, err := conn.Hijack() | ||
| require.NoError(t, err) | ||
| restored, err := Construct(hijacked) | ||
| if err != nil { | ||
| require.NoError(t, hijacked.Conn.Close()) | ||
| } | ||
| require.NoError(t, err) | ||
| defer func() { require.NoError(t, restored.Close(ctx)) }() | ||
|
|
||
| handle, err := restored.CreateEventHandle("FBX_CONSTRUCT_UNIX", &testEventHandler{}) | ||
| require.NoError(t, err) | ||
| require.NoError(t, restored.QueueEvent(ctx, handle)) | ||
| require.NoError(t, restored.CancelEvent(ctx, handle)) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also support URI syntax when connecting with socket. Some pgx tests were cleaned up, but now can be restored or used as reference to check if we properly parse URI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the original pgx cases. The existing parser already accepts firebird:///employee?host=/run/firebird/firebird.sock. I documented that form and added parsing tests for Linux and Windows paths, plus a percent-encoded path in the URI host.