MSP-6703 fix race conditions
This commit is contained in:
1 parent
2f614a5c75
commit
99d4aaca45
3 files changed
+11
-9
No files matched your search
@@ -123,7 +123,7 @@ func main(ctx context.Context, r *Rqlite) error {
|
||||
log.Fatalf("failed to create store: %s", err.Error())
|
||||
}
|
||||
|
||||
r.store = str
|
||||
r.storePtr.Store(str)
|
||||
|
||||
credStr, err := credentialsStore(r.credentials)
|
||||
if err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/rqlite/rqlite/v9/auth"
|
||||
@@ -25,7 +26,7 @@ type Rqlite struct {
|
||||
credentials []auth.Credential
|
||||
|
||||
localHttpAddr string
|
||||
store *store.Store
|
||||
storePtr *atomic.Pointer[store.Store]
|
||||
closed chan struct{}
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
@@ -40,6 +41,7 @@ func New(
|
||||
internalClientCredential: internalClientCredential,
|
||||
credentials: credentials,
|
||||
closed: make(chan struct{}),
|
||||
storePtr: &atomic.Pointer[store.Store]{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,10 +53,10 @@ func (r *Rqlite) Run(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (r *Rqlite) WaitForLeader(timeout time.Duration) error {
|
||||
if r.store == nil {
|
||||
if r.storePtr.Load() == nil {
|
||||
return ErrNotRun
|
||||
}
|
||||
_, err := r.store.WaitForLeader(timeout)
|
||||
_, err := r.storePtr.Load().WaitForLeader(timeout)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "wait for leader in rqlite store")
|
||||
}
|
||||
@@ -62,14 +64,14 @@ func (r *Rqlite) WaitForLeader(timeout time.Duration) error {
|
||||
}
|
||||
|
||||
func (r *Rqlite) IsLeader() bool {
|
||||
if r.store == nil {
|
||||
if r.storePtr.Load() == nil {
|
||||
return false
|
||||
}
|
||||
return r.store.IsLeader()
|
||||
return r.storePtr.Load().IsLeader()
|
||||
}
|
||||
|
||||
func (r *Rqlite) SqlDB() (*sql.DB, error) {
|
||||
if r.store == nil {
|
||||
if r.storePtr.Load() == nil {
|
||||
return nil, ErrNotRun
|
||||
}
|
||||
|
||||
|
||||
@@ -99,11 +99,11 @@ func (s *Service) Run(ctx context.Context) error {
|
||||
}()
|
||||
time.Sleep(1 * time.Second) // optimistically wait for store initialization
|
||||
|
||||
waitingCtx, stopWaiting := context.WithCancel(ctx)
|
||||
waitingCtx, waitingDone := context.WithCancel(ctx)
|
||||
go s.writeWaitingLogs(waitingCtx)
|
||||
|
||||
err := s.rqlite.WaitForLeader(waitForLeaderTimeout)
|
||||
stopWaiting()
|
||||
waitingDone()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "wait for leader")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user