1
1
package session
2
2
3
3
import (
4
+ "context"
4
5
"crypto/tls"
5
6
"fmt"
6
7
"sync"
7
8
"time"
8
9
9
10
"github.com/btcsuite/btcd/btcec/v2"
10
11
"github.com/lightninglabs/lightning-node-connect/mailbox"
12
+ "github.com/lightninglabs/taproot-assets/fn"
11
13
"github.com/lightningnetwork/lnd/keychain"
12
14
"google.golang.org/grpc"
13
15
"google.golang.org/grpc/credentials"
@@ -21,8 +23,9 @@ type GRPCServerCreator func(opts ...grpc.ServerOption) *grpc.Server
21
23
type mailboxSession struct {
22
24
server * grpc.Server
23
25
24
- wg sync.WaitGroup
25
- quit chan struct {}
26
+ cancel fn.Option [context.CancelFunc ]
27
+ wg sync.WaitGroup
28
+ quit chan struct {}
26
29
}
27
30
28
31
func newMailboxSession () * mailboxSession {
@@ -33,7 +36,8 @@ func newMailboxSession() *mailboxSession {
33
36
34
37
func (m * mailboxSession ) start (session * Session ,
35
38
serverCreator GRPCServerCreator , authData []byte ,
36
- onUpdate func (local , remote * btcec.PublicKey ) error ,
39
+ onUpdate func (ctx context.Context , local ,
40
+ remote * btcec.PublicKey ) error ,
37
41
onNewStatus func (s mailbox.ServerStatus )) error {
38
42
39
43
tlsConfig := & tls.Config {}
@@ -43,10 +47,13 @@ func (m *mailboxSession) start(session *Session,
43
47
44
48
ecdh := & keychain.PrivKeyECDH {PrivKey : session .LocalPrivateKey }
45
49
50
+ ctx , cancel := context .WithCancel (context .Background ())
51
+ m .cancel = fn .Some (cancel )
52
+
46
53
keys := mailbox .NewConnData (
47
54
ecdh , session .RemotePublicKey , session .PairingSecret [:],
48
55
authData , func (key * btcec.PublicKey ) error {
49
- return onUpdate (session .LocalPublicKey , key )
56
+ return onUpdate (ctx , session .LocalPublicKey , key )
50
57
}, nil ,
51
58
)
52
59
@@ -81,6 +88,7 @@ func (m *mailboxSession) run(mailboxServer *mailbox.Server) {
81
88
}
82
89
83
90
func (m * mailboxSession ) stop () {
91
+ m .cancel .WhenSome (func (fn context.CancelFunc ) { fn () })
84
92
m .server .Stop ()
85
93
close (m .quit )
86
94
m .wg .Wait ()
@@ -104,7 +112,8 @@ func NewServer(serverCreator GRPCServerCreator) *Server {
104
112
}
105
113
106
114
func (s * Server ) StartSession (session * Session , authData []byte ,
107
- onUpdate func (local , remote * btcec.PublicKey ) error ,
115
+ onUpdate func (ctx context.Context , local ,
116
+ remote * btcec.PublicKey ) error ,
108
117
onNewStatus func (s mailbox.ServerStatus )) (chan struct {}, error ) {
109
118
110
119
s .activeSessionsMtx .Lock ()
0 commit comments