frontend: remember to close database connections on stop

This commit is contained in:
George Tankersley 2018-12-11 03:12:49 -05:00
parent f4d918b5f5
commit 84e6de593f
2 changed files with 12 additions and 7 deletions

View File

@ -88,6 +88,7 @@ func main() {
"error": err, "error": err,
}).Fatal("couldn't create SQL streamer") }).Fatal("couldn't create SQL streamer")
} }
defer service.(*frontend.SqlStreamer).GracefulStop()
// Register service // Register service
rpc.RegisterCompactTxStreamerServer(server, service) rpc.RegisterCompactTxStreamerServer(server, service)

View File

@ -18,7 +18,7 @@ var (
) )
// the service type // the service type
type sqlStreamer struct { type SqlStreamer struct {
db *sql.DB db *sql.DB
} }
@ -34,10 +34,14 @@ func NewSQLiteStreamer(dbPath string) (rpc.CompactTxStreamerServer, error) {
return nil, err return nil, err
} }
return &sqlStreamer{db}, nil return &SqlStreamer{db}, nil
} }
func (s *sqlStreamer) GetLatestBlock(ctx context.Context, placeholder *rpc.ChainSpec) (*rpc.BlockID, error) { func (s *SqlStreamer) GracefulStop() error {
return s.db.Close()
}
func (s *SqlStreamer) GetLatestBlock(ctx context.Context, placeholder *rpc.ChainSpec) (*rpc.BlockID, error) {
// the ChainSpec type is an empty placeholder // the ChainSpec type is an empty placeholder
height, err := storage.GetCurrentHeight(ctx, s.db) height, err := storage.GetCurrentHeight(ctx, s.db)
if err != nil { if err != nil {
@ -47,7 +51,7 @@ func (s *sqlStreamer) GetLatestBlock(ctx context.Context, placeholder *rpc.Chain
return &rpc.BlockID{Height: uint64(height)}, nil return &rpc.BlockID{Height: uint64(height)}, nil
} }
func (s *sqlStreamer) GetBlock(ctx context.Context, id *rpc.BlockID) (*rpc.CompactBlock, error) { func (s *SqlStreamer) GetBlock(ctx context.Context, id *rpc.BlockID) (*rpc.CompactBlock, error) {
if id.Height == 0 && id.Hash == nil { if id.Height == 0 && id.Hash == nil {
return nil, ErrUnspecified return nil, ErrUnspecified
} }
@ -66,13 +70,13 @@ func (s *sqlStreamer) GetBlock(ctx context.Context, id *rpc.BlockID) (*rpc.Compa
return nil, ErrUnspecified return nil, ErrUnspecified
} }
func (s *sqlStreamer) GetBlockRange(*rpc.BlockRange, rpc.CompactTxStreamer_GetBlockRangeServer) error { func (s *SqlStreamer) GetBlockRange(*rpc.BlockRange, rpc.CompactTxStreamer_GetBlockRangeServer) error {
return ErrNoImpl return ErrNoImpl
} }
func (s *sqlStreamer) GetTransaction(context.Context, *rpc.TxFilter) (*rpc.RawTransaction, error) { func (s *SqlStreamer) GetTransaction(context.Context, *rpc.TxFilter) (*rpc.RawTransaction, error) {
return nil, ErrNoImpl return nil, ErrNoImpl
} }
func (s *sqlStreamer) SendTransaction(context.Context, *rpc.RawTransaction) (*rpc.SendResponse, error) { func (s *SqlStreamer) SendTransaction(context.Context, *rpc.RawTransaction) (*rpc.SendResponse, error) {
return nil, ErrNoImpl return nil, ErrNoImpl
} }