Skip to content

Commit

Permalink
fix(plc4go/spi): fix timing issue when closing cached connection
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed May 15, 2023
1 parent 1be5d9f commit c857f83
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
17 changes: 6 additions & 11 deletions plc4go/pkg/api/cache/plcConnectionLease.go
Expand Up @@ -22,12 +22,12 @@ package cache
import (
"context"
"fmt"
"time"

plc4go "github.com/apache/plc4x/plc4go/pkg/api"
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
"github.com/apache/plc4x/plc4go/spi"
_default "github.com/apache/plc4x/plc4go/spi/default"
"github.com/apache/plc4x/plc4go/spi/utils"
"time"
)

type plcConnectionLease struct {
Expand Down Expand Up @@ -93,7 +93,7 @@ func (t *plcConnectionLease) Close() <-chan plc4go.PlcConnectionCloseResult {
panic("Called 'Close' on a closed cached connection")
}

result := make(chan plc4go.PlcConnectionCloseResult)
result := make(chan plc4go.PlcConnectionCloseResult, 1)

go func() {
// Check if the connection is still alive, if it is, put it back into the cache
Expand Down Expand Up @@ -133,16 +133,11 @@ func (t *plcConnectionLease) Close() <-chan plc4go.PlcConnectionCloseResult {
// Return the connection to the connection container and don't actually close it.
err := t.connectionContainer.returnConnection(newState)

// Finish closing the connection.
timeout := time.NewTimer(10 * time.Millisecond)
defer utils.CleanupTimer(timeout)
select {
case result <- _default.NewDefaultPlcConnectionCloseResultWithTraces(t, err, traces):
case <-timeout.C:
}

// Detach the connection from this lease, so it can no longer be used by the client.
t.connection = nil

// Finish closing the connection.
result <- _default.NewDefaultPlcConnectionCloseResultWithTraces(t, err, traces)
}()

return result
Expand Down
28 changes: 28 additions & 0 deletions plc4go/pkg/api/cache/plcConnectionLease_test.go
Expand Up @@ -639,3 +639,31 @@ func TestLeasedPlcConnection_BrowseRequestBuilder(t *testing.T) {
t.Errorf("Timeout")
}
}

func Test_plcConnectionLease_String(t1 *testing.T) {
type fields struct {
connectionContainer *connectionContainer
leaseId uint32
connection tracedPlcConnection
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "String it",
want: "plcConnectionLease{connectionContainer: <nil>, leaseId: 0, connection: %!s(<nil>)}",
},
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
t := &plcConnectionLease{
connectionContainer: tt.fields.connectionContainer,
leaseId: tt.fields.leaseId,
connection: tt.fields.connection,
}
assert.Equalf(t1, tt.want, t.String(), "String()")
})
}
}

0 comments on commit c857f83

Please sign in to comment.