Skip to content

Commit d6778c2

Browse files
authoredJan 14, 2025··
sprinkle new and fix almost all contexts (#373)
1 parent c767fee commit d6778c2

File tree

4 files changed

+27
-31
lines changed

4 files changed

+27
-31
lines changed
 

‎database/bfgd/database_ext_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ type btcBlocksNtfn struct {
296296
expected *bfgd.BtcBlock
297297
}
298298

299-
func (b *btcBlocksNtfn) handleBtcBlocksNotification(table string, action string, payload, payloadOld interface{}) {
299+
func (b *btcBlocksNtfn) handleBtcBlocksNotification(pctx context.Context, table string, action string, payload, payloadOld interface{}) {
300300
defer b.cancel()
301301
bb := payload.(*bfgd.BtcBlock)
302302
if !reflect.DeepEqual(*b.expected, *bb) {

‎database/database.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Hemi Labs, Inc.
1+
// Copyright (c) 2024-2025 Hemi Labs, Inc.
22
// Use of this source code is governed by the MIT License,
33
// which can be found in the LICENSE file.
44

@@ -376,7 +376,7 @@ func (tz TimeZone) Value() (driver.Value, error) {
376376
var _ driver.Valuer = (*TimeZone)(nil)
377377

378378
// NotificationCallback is a callback function for a database notification.
379-
type NotificationCallback func(string, string, interface{}, interface{})
379+
type NotificationCallback func(context.Context, string, string, interface{}, interface{})
380380

381381
// NotificationName identifies a database notification type.
382382
type NotificationName string

‎database/postgres/postgres.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2024 Hemi Labs, Inc.
1+
// Copyright (c) 2024-2025 Hemi Labs, Inc.
22
// Use of this source code is governed by the MIT License,
33
// which can be found in the LICENSE file.
44

@@ -96,7 +96,7 @@ type ntfnWrapper struct {
9696
DataOld json.RawMessage `json:"data_old"` // JSON payload for old row
9797
}
9898

99-
func (p *Database) ntfnEventHandler(pqn *pq.Notification) {
99+
func (p *Database) ntfnEventHandler(ctx context.Context, pqn *pq.Notification) {
100100
log.Tracef("ntfnEventHandler notify: %v", spew.Sdump(pqn))
101101

102102
// A nil notification can be received on database shutdown.
@@ -142,7 +142,7 @@ func (p *Database) ntfnEventHandler(pqn *pq.Notification) {
142142
log.Tracef("ntfnEventHandler: payload new %v", spew.Sdump(payloadNew))
143143
log.Tracef("ntfnEventHandler: payload old %v", spew.Sdump(payloadOld))
144144

145-
pn.callback(nw.Table, nw.Action, payloadNew, payloadOld)
145+
pn.callback(ctx, nw.Table, nw.Action, payloadNew, payloadOld)
146146
}
147147

148148
// ntfnListenHandler listens for database notifications.
@@ -176,7 +176,7 @@ func (p *Database) ntfnListenHandler(ctx context.Context) {
176176
case pqn := <-p.listener.Notify:
177177
// TODO: Consider limiting the number of notifications being
178178
// processed at the same time.
179-
go p.ntfnEventHandler(pqn)
179+
go p.ntfnEventHandler(ctx, pqn)
180180

181181
case <-time.After(60 * time.Second):
182182
go func() {

‎service/bfg/bfg.go

+20-24
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,11 @@ func (s *Server) invalidBlockChecker(ctx context.Context) {
329329
}
330330

331331
// handleRequest is called as a go routine to handle a long-lived command.
332-
func (s *Server) handleRequest(parentCtx context.Context, bws *bfgWs, wsid string, cmd protocol.Command, handler func(ctx context.Context) (any, error)) {
332+
func (s *Server) handleRequest(pctx context.Context, bws *bfgWs, wsid string, cmd protocol.Command, handler func(ctx context.Context) (any, error)) {
333333
log.Tracef("handleRequest: %v", bws.addr)
334334
defer log.Tracef("handleRequest exit: %v", bws.addr)
335335

336-
ctx, cancel := context.WithTimeout(bws.requestContext,
337-
time.Duration(s.cfg.RequestTimeout)*time.Second)
336+
ctx, cancel := context.WithTimeout(pctx, time.Duration(s.cfg.RequestTimeout)*time.Second)
338337
defer cancel()
339338

340339
select {
@@ -393,8 +392,8 @@ func (s *Server) handleBitcoinBalance(ctx context.Context, bbr *bfgapi.BitcoinBa
393392
}, nil
394393
}
395394

396-
func (s *Server) handleOneBroadcastRequest(ctx context.Context, highPriority bool) {
397-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
395+
func (s *Server) handleOneBroadcastRequest(pctx context.Context, highPriority bool) {
396+
ctx, cancel := context.WithTimeout(pctx, 5*time.Second)
398397
defer cancel()
399398

400399
serializedTx, err := s.db.BtcTransactionBroadcastRequestGetNext(ctx, highPriority)
@@ -803,8 +802,8 @@ type bfgWs struct {
803802
addr string
804803
conn *protocol.WSConn
805804
sessionId string
806-
listenerName string // "public" or "private"
807-
requestContext context.Context
805+
listenerName string // "public" or "private"
806+
requestContext context.Context // XXX get rid of this
808807
notify map[notificationId]struct{}
809808
publicKey []byte
810809
}
@@ -1086,8 +1085,7 @@ func (s *Server) handleWebsocketPublic(w http.ResponseWriter, r *http.Request) {
10861085
}
10871086

10881087
// Must complete handshake in WSHandshakeTimeout.
1089-
hsCtx, hsCancel := context.WithTimeout(context.Background(),
1090-
protocol.WSHandshakeTimeout)
1088+
hsCtx, hsCancel := context.WithTimeout(r.Context(), protocol.WSHandshakeTimeout)
10911089
defer hsCancel()
10921090

10931091
authenticator, err := auth.NewSecp256k1AuthServer()
@@ -1421,16 +1419,16 @@ func hemiL2KeystonesToDb(l2ks []hemi.L2Keystone) []bfgd.L2Keystone {
14211419
return dbks
14221420
}
14231421

1424-
func (s *Server) refreshCacheAndNotifiyL2Keystones() {
1425-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
1422+
func (s *Server) refreshCacheAndNotifiyL2Keystones(pctx context.Context) {
1423+
ctx, cancel := context.WithTimeout(pctx, 10*time.Second)
14261424
defer cancel()
14271425

14281426
s.refreshL2KeystoneCache(ctx)
14291427
go s.handleL2KeystonesNotification()
14301428
}
14311429

1432-
func (s *Server) saveL2Keystones(ctx context.Context, l2k []hemi.L2Keystone) {
1433-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
1430+
func (s *Server) saveL2Keystones(pctx context.Context, l2k []hemi.L2Keystone) {
1431+
ctx, cancel := context.WithTimeout(pctx, 5*time.Second)
14341432
defer cancel()
14351433

14361434
ks := hemiL2KeystonesToDb(l2k)
@@ -1441,7 +1439,7 @@ func (s *Server) saveL2Keystones(ctx context.Context, l2k []hemi.L2Keystone) {
14411439
return
14421440
}
14431441

1444-
go s.refreshCacheAndNotifiyL2Keystones()
1442+
go s.refreshCacheAndNotifiyL2Keystones(pctx)
14451443
}
14461444

14471445
func (s *Server) handleNewL2Keystones(ctx context.Context, nlkr *bfgapi.NewL2KeystonesRequest) (any, error) {
@@ -1482,9 +1480,7 @@ func handle(service string, mux *http.ServeMux, pattern string, handler func(htt
14821480
log.Infof("handle (%v): %v", service, pattern)
14831481
}
14841482

1485-
func (s *Server) handleStateUpdates(table string, action string, payload, payloadOld interface{}) {
1486-
ctx := context.Background()
1487-
1483+
func (s *Server) handleStateUpdates(ctx context.Context, table string, action string, payload, payloadOld interface{}) {
14881484
// get the last known canonical chain height
14891485
s.mtx.RLock()
14901486
heightBefore := s.canonicalChainHeight
@@ -1509,7 +1505,7 @@ func (s *Server) handleStateUpdates(table string, action string, payload, payloa
15091505
s.mtx.Unlock()
15101506
}
15111507

1512-
func (s *Server) handleAccessPublicKeys(table string, action string, payload, payloadOld interface{}) {
1508+
func (s *Server) handleAccessPublicKeys(ctx context.Context, table string, action string, payload, payloadOld interface{}) {
15131509
log.Tracef("received payloads: %v, %v", payload, payloadOld)
15141510

15151511
if action != "DELETE" {
@@ -1543,8 +1539,8 @@ func (s *Server) handleAccessPublicKeys(table string, action string, payload, pa
15431539
s.mtx.Unlock()
15441540
}
15451541

1546-
func (s *Server) handleL2KeystonesChange(table string, action string, payload, payloadOld any) {
1547-
go s.refreshCacheAndNotifiyL2Keystones()
1542+
func (s *Server) handleL2KeystonesChange(ctx context.Context, table string, action string, payload, payloadOld any) {
1543+
go s.refreshCacheAndNotifiyL2Keystones(ctx)
15481544
}
15491545

15501546
func (s *Server) fetchRemoteL2Keystones(pctx context.Context) {
@@ -1592,7 +1588,7 @@ func (s *Server) handleBFGWebsocketReadUnauth(ctx context.Context, conn *protoco
15921588
}
15931589
}
15941590

1595-
func (s *Server) callBFG(parrentCtx context.Context, msg any) (any, error) {
1591+
func (s *Server) callBFG(pctx context.Context, msg any) (any, error) {
15961592
log.Tracef("callBFG %T", msg)
15971593
defer log.Tracef("callBFG exit %T", msg)
15981594

@@ -1601,7 +1597,7 @@ func (s *Server) callBFG(parrentCtx context.Context, msg any) (any, error) {
16011597
ch: make(chan any),
16021598
}
16031599

1604-
ctx, cancel := context.WithTimeout(parrentCtx, s.bfgCallTimeout)
1600+
ctx, cancel := context.WithTimeout(pctx, s.bfgCallTimeout)
16051601
defer cancel()
16061602

16071603
// attempt to send
@@ -1629,11 +1625,11 @@ func (s *Server) callBFG(parrentCtx context.Context, msg any) (any, error) {
16291625
// Won't get here
16301626
}
16311627

1632-
func (s *Server) handleBFGCallCompletion(parrentCtx context.Context, conn *protocol.Conn, bc bfgCmd) {
1628+
func (s *Server) handleBFGCallCompletion(pctx context.Context, conn *protocol.Conn, bc bfgCmd) {
16331629
log.Tracef("handleBFGCallCompletion")
16341630
defer log.Tracef("handleBFGCallCompletion exit")
16351631

1636-
ctx, cancel := context.WithTimeout(parrentCtx, s.bfgCallTimeout)
1632+
ctx, cancel := context.WithTimeout(pctx, s.bfgCallTimeout)
16371633
defer cancel()
16381634

16391635
log.Tracef("handleBFGCallCompletion: %v", spew.Sdump(bc.msg))

0 commit comments

Comments
 (0)