Skip to content

Commit

Permalink
Move StartNonceSweeper out of NonceManager constructor (bp #181) (#182)
Browse files Browse the repository at this point in the history
Not all uses of the NonceManager require/expect a separate
go thread to be spawned to sweep for expired nonces.

Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
mergify[bot] committed Jul 28, 2020
1 parent b6aa376 commit f8b233c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 13 additions & 0 deletions lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/hyperledger/fabric-ca/lib/metadata"
"github.com/hyperledger/fabric-ca/lib/server/db"
dbutil "github.com/hyperledger/fabric-ca/lib/server/db/util"
idemix "github.com/hyperledger/fabric-ca/lib/server/idemix"
servermetrics "github.com/hyperledger/fabric-ca/lib/server/metrics"
"github.com/hyperledger/fabric-ca/lib/server/operations"
stls "github.com/hyperledger/fabric-ca/lib/tls"
Expand Down Expand Up @@ -195,6 +196,10 @@ func (s *Server) Start() (err error) {
return nil
}

for _, ca := range s.caMap {
startNonceSweeper(ca)
}

// Start listening and serving
err = s.listenAndServe()
if err != nil {
Expand All @@ -208,6 +213,14 @@ func (s *Server) Start() (err error) {
return nil
}

func startNonceSweeper(ca *CA) {
if nm, ok := ca.issuer.(interface{ NonceManager() idemix.NonceManager }); ok && nm != nil {
if ss, ok := nm.NonceManager().(interface{ StartNonceSweeper() }); ok {
ss.StartNonceSweeper()
}
}
}

// Stop the server
// WARNING: This forcefully closes the listening socket and may cause
// requests in transit to fail, and so is only used for testing.
Expand Down
17 changes: 7 additions & 10 deletions lib/server/idemix/nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func NewNonceManager(issuer MyIssuer, clock Clock, level int) (NonceManager, err
return nil, errors.Wrapf(err, fmt.Sprintf("Failed to parse idemix.noncesweepinterval config option while initializing Nonce manager for Issuer '%s'",
issuer.Name()))
}
mgr.startNonceSweeper()
return mgr, nil
}

Expand Down Expand Up @@ -131,24 +130,22 @@ func (nm *nonceManager) SweepExpiredNonces() error {
return nm.sweep(nm.clock.Now().UTC())
}

func (nm *nonceManager) startNonceSweeper() {
ticker := time.NewTicker(nm.nonceSweepInterval)
// StartNonceSweeper starts a separate thread that will remove expired
// nonces at the interval speciifed by the idemix.noncesweepinterval. This
// function should be called while initializing the server.
func (nm *nonceManager) StartNonceSweeper() {
go func() {
ticker := time.NewTicker(nm.nonceSweepInterval)
for t := range ticker.C {
log.Debugf("Cleaning up expired nonces for CA '%s'", nm.issuer.Name())
nm.sweep(t.UTC())
}
}()
}

// sweep deletes all nonces that have expired (whose expiry is less than current timestamp)
func (nm *nonceManager) sweep(curTime time.Time) error {
err := nm.removeExpiredNoncesFromDB(curTime)
if err != nil {
log.Errorf("Failed to deleted expired nonces from DB for CA %s: %s", nm.issuer.Name(), err.Error())
return err
}
return nil
log.Debugf("Cleaning up expired nonces for CA '%s'", nm.issuer.Name())
return nm.removeExpiredNoncesFromDB(curTime)
}

// Gets the specified nonce from DB and removes it from the DB
Expand Down

0 comments on commit f8b233c

Please sign in to comment.