Skip to content

Commit

Permalink
applied Tim's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pkazmierczak committed Sep 21, 2023
1 parent eb1aba3 commit dad0e65
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions client/widmgr/widmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,16 @@ func (m *WIDMgr) get(id cstructs.TaskIdentity) *structs.SignedWorkloadIdentity {
// The caller must call the returned func to stop watching and ensure the
// watched id actually exists, otherwise the channel never returns a result.
func (m *WIDMgr) Watch(id cstructs.TaskIdentity) (<-chan *structs.SignedWorkloadIdentity, func()) {
m.watchersLock.Lock()
defer m.watchersLock.Unlock()

// If Shutdown has been called return a closed chan
if m.stopCtx.Err() != nil {
c := make(chan *structs.SignedWorkloadIdentity)
close(c)
return c, func() {}
}

m.watchersLock.Lock()
defer m.watchersLock.Unlock()

// Buffer of 1 so sends don't block on receives
c := make(chan *structs.SignedWorkloadIdentity, 1)
m.watchers[id] = make([]chan *structs.SignedWorkloadIdentity, 0)
Expand Down Expand Up @@ -187,13 +187,13 @@ func (m *WIDMgr) Shutdown() {

// getIdentities fetches all signed identities or returns an error.
func (m *WIDMgr) getIdentities() error {
m.lastTokenLock.Lock()
defer m.lastTokenLock.Unlock()

if len(m.widSpecs) == 0 {
return nil
}

m.lastTokenLock.Lock()
defer m.lastTokenLock.Unlock()

reqs := make([]*structs.WorkloadIdentityRequest, 0, len(m.widSpecs))
for taskName, widspecs := range m.widSpecs {
for _, widspec := range widspecs {
Expand Down Expand Up @@ -246,6 +246,11 @@ func (m *WIDMgr) renew() {
}
}

if len(reqs) == 0 {
m.logger.Trace("no workload identities expire")
return
}

renewNow := false
minExp := time.Time{}

Expand Down Expand Up @@ -275,11 +280,6 @@ func (m *WIDMgr) renew() {
}
}

if len(reqs) == 0 {
m.logger.Trace("no workload identities expire")
return
}

var wait time.Duration
if !renewNow {
wait = helper.ExpiryToRenewTime(minExp, time.Now, m.minWait)
Expand All @@ -293,12 +293,8 @@ func (m *WIDMgr) renew() {
for {
// we need to handle stopCtx.Err() and manually stop the subscribers
if err := m.stopCtx.Err(); err != nil {
// close watchers
for _, w := range m.watchers {
for _, c := range w {
close(c)
}
}
// close watchers and shutdown
m.Shutdown()
return
}

Expand All @@ -308,12 +304,8 @@ func (m *WIDMgr) renew() {
case <-timer.C:
m.logger.Trace("getting new signed identities", "num", len(reqs))
case <-m.stopCtx.Done():
// close watchers
for _, w := range m.watchers {
for _, c := range w {
close(c)
}
}
// close watchers and shutdown
m.Shutdown()
return
}

Expand Down

0 comments on commit dad0e65

Please sign in to comment.