Skip to content
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

better session disposal #668

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
33 changes: 31 additions & 2 deletions platform/view/core/manager/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ func (ctx *ctx) GetSession(f view.View, party view.Identity) (view.Session, erro
if err != nil {
return nil, err
}
ctx.sessions[id.UniqueID()] = s
key := id.UniqueID()
ctx.sessions[key] = &disposableSession{
Session: s,
ctx: ctx,
key: key,
}
} else {
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("[%s] Reusing session [to:%s]", ctx.me, id)
Expand All @@ -267,7 +272,11 @@ func (ctx *ctx) GetSessionByID(id string, party view.Identity) (view.Session, er
if err != nil {
return nil, err
}
ctx.sessions[key] = s
ctx.sessions[key] = &disposableSession{
Session: s,
ctx: ctx,
key: key,
}
} else {
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("[%s] Reusing session with given id [id:%s][to:%s]", id, ctx.me, party)
Expand Down Expand Up @@ -367,7 +376,27 @@ func (ctx *ctx) safeInvoke(f func()) {
f()
}

func (ctx *ctx) disposeSession(key string) {
ctx.sessionsLock.Lock()
defer ctx.sessionsLock.Unlock()

delete(ctx.sessions, key)
}

type localContext interface {
disposableContext
cleanup()
}

type disposableSession struct {
view.Session
ctx *ctx
key string
}

// Close releases all the resources allocated by this session
func (s *disposableSession) Close() {
// remove from context
s.ctx.disposeSession(s.key)
s.Session.Close()
}
3 changes: 3 additions & 0 deletions platform/view/services/comm/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type NetworkStreamSession struct {
incoming chan *view.Message
streams map[*streamHandler]struct{}
closed bool
whoClosed string
mutex sync.Mutex
}

Expand All @@ -40,6 +41,7 @@ func (n *NetworkStreamSession) Info() view.SessionInfo {
Endpoint: n.endpointAddress,
EndpointPKID: n.endpointID,
Closed: n.closed,
WhoClosed: n.whoClosed,
}
n.mutex.Unlock()
return ret
Expand Down Expand Up @@ -103,6 +105,7 @@ func (n *NetworkStreamSession) closeInternal() {
}
close(n.incoming)
n.closed = true
n.whoClosed = string(debug.Stack())
n.streams = make(map[*streamHandler]struct{})

if logger.IsEnabledFor(zapcore.DebugLevel) {
Expand Down
2 changes: 2 additions & 0 deletions platform/view/services/session/bidi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package session
import (
"context"
"encoding/base64"
"runtime/debug"

"github.com/hyperledger-labs/fabric-smart-client/platform/view/view"
"github.com/pkg/errors"
Expand Down Expand Up @@ -134,4 +135,5 @@ func (s *localSession) Receive() <-chan *view.Message {

func (s *localSession) Close() {
s.info.Closed = true
s.info.WhoClosed = string(debug.Stack())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the WhoClosed was used for debugging the disposable session.

}
1 change: 1 addition & 0 deletions platform/view/view/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type SessionInfo struct {
Endpoint string
EndpointPKID []byte
Closed bool
WhoClosed string
}

func (i *SessionInfo) String() string {
Expand Down
Loading