Skip to content

Commit 3eeffbe

Browse files
committed
Upgrade jsonrpc.Service's bootstrap workflow
1. add StopServersAndWait to Service instance; 2. export api.models.OpenSQLiteDBAsGorp function for convenient use; 3. update cqld's bootstrap.
1 parent 5895796 commit 3eeffbe

3 files changed

Lines changed: 41 additions & 13 deletions

File tree

api/models/models.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,32 @@ func InitModels(dbFile string) error {
1818
return initChainDBConnection(dbFile)
1919
}
2020

21-
func initChainDBConnection(dbFile string) error {
22-
dsn := fmt.Sprintf("%s?_journal=WAL&mode=ro", dbFile)
21+
// OpenSQLiteDBAsGorp opens a sqlite database an wrapped it in gorp.DbMap.
22+
func OpenSQLiteDBAsGorp(dbFile, mode string, maxOpen, maxIdle int) (db *gorp.DbMap, err error) {
23+
dsn := fmt.Sprintf("%s?_journal=WAL&mode=%s", dbFile, mode)
2324
underdb, err := sql.Open("sqlite3", dsn)
2425
if err != nil {
25-
return errors.WithMessage(err, "unable to open chain.db")
26+
return nil, errors.Wrapf(err, "unable to open database %q", dsn)
2627
}
27-
chaindb = &gorp.DbMap{
28+
underdb.SetMaxOpenConns(maxOpen)
29+
underdb.SetMaxIdleConns(maxIdle)
30+
31+
if err := underdb.Ping(); err != nil {
32+
return nil, errors.Wrapf(err, "ping to database %q failed", dsn)
33+
}
34+
35+
db = &gorp.DbMap{
2836
Db: underdb,
2937
Dialect: gorp.SqliteDialect{},
3038
}
39+
return db, nil
40+
}
41+
42+
func initChainDBConnection(dbFile string) (err error) {
43+
chaindb, err = OpenSQLiteDBAsGorp(dbFile, "ro", 100, 30)
44+
if err != nil {
45+
return err
46+
}
3147

3248
// register tables
3349
chaindb.AddTableWithName(Block{}, "indexed_blocks").SetKeys(false, "Height")

api/service.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/CovenantSQL/CovenantSQL/api/models"
14-
1514
"github.com/CovenantSQL/CovenantSQL/utils/log"
1615
"github.com/gorilla/websocket"
1716
"github.com/pkg/errors"
@@ -27,6 +26,15 @@ type Service struct {
2726
WriteTimeout time.Duration
2827

2928
stopChan chan struct{}
29+
stopped chan struct{}
30+
}
31+
32+
// NewService creates a new Service.
33+
func NewService() *Service {
34+
return &Service{
35+
stopChan: make(chan struct{}),
36+
stopped: make(chan struct{}),
37+
}
3038
}
3139

3240
// StartServers start API servers in a non-blocking way, fatal on errors.
@@ -39,15 +47,19 @@ func (s *Service) StopServers() {
3947
close(s.stopChan)
4048
}
4149

50+
// StopServersAndWait wait servers to stop.
51+
func (s *Service) StopServersAndWait() {
52+
s.StopServers()
53+
<-s.stopped
54+
}
55+
4256
// RunServers start API servers in a blocking way, fatal on errors.
4357
func (s *Service) RunServers() {
4458
// setup database
4559
if err := models.InitModels(s.DBFile); err != nil {
4660
log.WithError(err).Fatal("api: init models failed")
4761
return
4862
}
49-
50-
s.stopChan = make(chan struct{})
5163
wg := sync.WaitGroup{}
5264

5365
if s.WebsocketAddr != "" {
@@ -119,4 +131,5 @@ func (s *Service) runWebsocketServer(wg *sync.WaitGroup) {
119131
}
120132
cancel()
121133
log.Warn("api: websocket server stopped")
134+
close(s.stopped)
122135
}

cmd/cqld/bootstrap.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,11 @@ func runNode(nodeID proto.NodeID, listenAddr string) (err error) {
159159

160160
// start json-rpc server
161161
if wsapiAddr != "" {
162-
jsonrpcServer := &api.Service{
163-
DBFile: conf.GConf.BP.ChainFileName,
164-
WebsocketAddr: wsapiAddr,
165-
ReadTimeout: 60 * time.Second,
166-
WriteTimeout: 60 * time.Second,
167-
}
162+
jsonrpcServer := api.NewService()
163+
jsonrpcServer.DBFile = conf.GConf.BP.ChainFileName
164+
jsonrpcServer.WebsocketAddr = wsapiAddr
165+
jsonrpcServer.ReadTimeout = 60 * time.Second
166+
jsonrpcServer.WriteTimeout = 60 * time.Second
168167
jsonrpcServer.StartServers()
169168
}
170169

0 commit comments

Comments
 (0)