Skip to content

Commit 9463f7d

Browse files
authored
fix: fix connection notify (#3335)
Signed-off-by: Song Gao <[email protected]>
1 parent 1414313 commit 9463f7d

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

pkg/connection/conn.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
package connection
1616

1717
import (
18-
rawContext "context"
1918
"sync"
2019
"sync/atomic"
2120

2221
"github.com/lf-edge/ekuiper/contract/v2/api"
2322

24-
"github.com/lf-edge/ekuiper/v2/internal/conf"
2523
"github.com/lf-edge/ekuiper/v2/internal/topo/context"
2624
"github.com/lf-edge/ekuiper/v2/pkg/modules"
2725
)
@@ -32,10 +30,8 @@ type ConnWrapper struct {
3230
conn modules.Connection
3331
err error
3432
l sync.RWMutex
35-
// context for connection wait only
36-
waitCtx rawContext.Context
37-
// context for the lifecycle
38-
stop rawContext.CancelFunc
33+
readCh chan struct{}
34+
detachCh chan struct{}
3935
}
4036

4137
func (cw *ConnWrapper) setConn(conn modules.Connection, err error) {
@@ -50,7 +46,8 @@ func (cw *ConnWrapper) Wait(connectorCtx api.StreamContext) (modules.Connection,
5046
select {
5147
case <-connectorCtx.Done():
5248
connectorCtx.GetLogger().Infof("stop waiting connection")
53-
case <-cw.waitCtx.Done():
49+
case <-cw.readCh:
50+
case <-cw.detachCh:
5451
}
5552
cw.l.RLock()
5653
defer cw.l.RUnlock()
@@ -63,20 +60,16 @@ func (cw *ConnWrapper) IsInitialized() bool {
6360
return cw.initialized
6461
}
6562

66-
func newConnWrapper(callerCtx api.StreamContext, meta *Meta) *ConnWrapper {
67-
callerCtx.GetLogger().Infof("creating new connection wrapper")
68-
wctx, onConnect := rawContext.WithCancel(rawContext.Background())
69-
contextLogger := conf.Log.WithField("conn", meta.ID)
70-
connCtx, stop := context.WithValue(context.Background(), context.LoggerKey, contextLogger).WithCancel()
63+
func newConnWrapper(ctx api.StreamContext, meta *Meta) *ConnWrapper {
7164
cw := &ConnWrapper{
72-
ID: meta.ID,
73-
waitCtx: wctx,
74-
stop: stop,
65+
ID: meta.ID,
66+
readCh: make(chan struct{}),
67+
detachCh: make(chan struct{}),
7568
}
7669
go func() {
77-
conn, err := createConnection(connCtx, meta)
70+
conn, err := createConnection(ctx, meta)
7871
cw.setConn(conn, err)
79-
onConnect()
72+
close(cw.readCh)
8073
}()
8174
return cw
8275
}

pkg/connection/pool.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,7 @@ func detachConnection(ctx api.StreamContext, conId string) error {
321321
globalConnectionManager.connectionPool[conId] = meta
322322
conf.Log.Infof("detachConnection remove conn:%v,ref:%v", conId, refId)
323323
if !meta.Named && meta.GetRefCount() == 0 {
324-
if meta.cw.stop != nil {
325-
meta.cw.stop()
326-
}
324+
close(meta.cw.detachCh)
327325
conn, err := meta.cw.Wait(ctx)
328326
if conn != nil && err == nil {
329327
conn.Close(ctx)

0 commit comments

Comments
 (0)