Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions fbconn/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ import (
"time"

"github.com/firebirdsql/fbx/fbproto"
"github.com/firebirdsql/fbx/internal/eventctx"
)

const eventLockRetryInterval = 5 * time.Millisecond

func (fbConn *FbConn) watchEventResponse(ctx context.Context) (context.Context, func(), error) {
if ctx == context.Background() {
return ctx, func() {}, nil
}
if ctx.Err() != nil {
return nil, nil, newContextAlreadyDoneError(ctx)
}
ctx, cancel := eventctx.ForResponse(ctx)
fbConn.contextWatcher.Watch(ctx)
return ctx, func() {
fbConn.contextWatcher.Unwatch()
cancel()
}, nil
}

func (fbConn *FbConn) lockEventOperation(ctx context.Context) error {
for {
err := fbConn.lock()
Expand Down Expand Up @@ -65,15 +81,11 @@ func (fbConn *FbConn) QueueEvent(ctx context.Context, handle *EventHandle) error
}
defer fbConn.unlock()

if ctx != context.Background() {
select {
case <-ctx.Done():
return newContextAlreadyDoneError(ctx)
default:
}
fbConn.contextWatcher.Watch(ctx)
defer fbConn.contextWatcher.Unwatch()
ctx, unwatch, err := fbConn.watchEventResponse(ctx)
if err != nil {
return err
}
defer unwatch()

eventID := handle.assignNewLocalID()
fbConn.registerEventHandle(eventID, handle)
Expand Down
34 changes: 16 additions & 18 deletions fbconn/events_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/firebirdsql/fbx/fbproto"
"github.com/firebirdsql/fbx/fbproto/vax"
"github.com/firebirdsql/fbx/internal/eventctx"
)

type eventChannel struct {
Expand Down Expand Up @@ -52,7 +53,10 @@ func (fbConn *FbConn) ensureEventChannel(ctx context.Context) (*eventChannel, er

host := fbConn.eventDialHost()
address := net.JoinHostPort(host, strconv.Itoa(auxPort))
auxConn, err := fbConn.config.DialFunc(ctx, "tcp", address)
// After advertising the port, the server waits for this connection.
dialCtx, cancel := eventctx.ForResponse(ctx)
auxConn, err := fbConn.config.DialFunc(dialCtx, "tcp", address)
cancel()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -84,15 +88,11 @@ func (fbConn *FbConn) requestAuxEventPort(ctx context.Context) (*eventChannel, i
}
defer fbConn.unlock()

if ctx != context.Background() {
select {
case <-ctx.Done():
return nil, 0, newContextAlreadyDoneError(ctx)
default:
}
fbConn.contextWatcher.Watch(ctx)
defer fbConn.contextWatcher.Unwatch()
ctx, unwatch, err := fbConn.watchEventResponse(ctx)
if err != nil {
return nil, 0, err
}
defer unwatch()

fbConn.eventMu.Lock()
channel := fbConn.eventChannel
Expand Down Expand Up @@ -159,24 +159,22 @@ func (fbConn *FbConn) readEventChannelLoop(channel *eventChannel) {
var eventBufferScratch []byte
for {
opcode, err := xdr.ReadInt32()
if err == nil && (opcode == fbproto.Op_disconnect || opcode == fbproto.Op_exit) {
err = io.EOF
}
if err != nil {
if !errors.Is(err, io.EOF) && !errors.Is(err, net.ErrClosed) {
if !errors.Is(err, net.ErrClosed) && !fbConn.IsClosed() {
fbConn.setEventError(fmt.Errorf("event channel read failed: %w", err))
if closeErr := channel.close(); closeErr != nil {
fbConn.setEventError(fmt.Errorf("event channel close after read failure: %w", closeErr))
}
}
if closeErr := channel.close(); closeErr != nil {
fbConn.setEventError(fmt.Errorf("event channel close after read failure: %w", closeErr))
}
return
}

switch opcode {
case fbproto.Op_dummy:
continue
case fbproto.Op_disconnect, fbproto.Op_exit:
if closeErr := channel.close(); closeErr != nil {
fbConn.setEventError(fmt.Errorf("event channel close on server disconnect failed: %w", closeErr))
}
return
case fbproto.Op_event:
eventID, eventCount, readErr := readSingleEventFromXDR(xdr, &eventBufferScratch)
if readErr != nil {
Expand Down
Loading
Loading