(ctx context.Context)
| 103 | } |
| 104 | |
| 105 | func (s *Service) process(ctx context.Context) (int, time.Duration) { |
| 106 | // toRenew are mappings which are due for renewal |
| 107 | // toUpdate are the remaining mappings, which will only be updated if one of |
| 108 | // the old IGDs has gone away, or a new IGD has appeared, but only if we |
| 109 | // actually need to perform a renewal. |
| 110 | var toRenew, toUpdate []*Mapping |
| 111 | |
| 112 | renewIn := time.Duration(s.cfg.Options().NATRenewalM) * time.Minute |
| 113 | if renewIn == 0 { |
| 114 | // We always want to do renewal so lets just pick a nice sane number. |
| 115 | renewIn = 30 * time.Minute |
| 116 | } |
| 117 | |
| 118 | s.mut.RLock() |
| 119 | for _, mapping := range s.mappings { |
| 120 | mapping.mut.RLock() |
| 121 | expires := mapping.expires |
| 122 | mapping.mut.RUnlock() |
| 123 | |
| 124 | if expires.Before(time.Now()) { |
| 125 | toRenew = append(toRenew, mapping) |
| 126 | } else { |
| 127 | toUpdate = append(toUpdate, mapping) |
| 128 | mappingRenewIn := time.Until(expires) |
| 129 | if mappingRenewIn < renewIn { |
| 130 | renewIn = mappingRenewIn |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | s.mut.RUnlock() |
| 135 | |
| 136 | // Don't do anything, unless we really need to renew |
| 137 | if len(toRenew) == 0 { |
| 138 | return -1, renewIn |
| 139 | } |
| 140 | |
| 141 | nats := discoverAll(ctx, time.Duration(s.cfg.Options().NATRenewalM)*time.Minute, time.Duration(s.cfg.Options().NATTimeoutS)*time.Second) |
| 142 | |
| 143 | for _, mapping := range toRenew { |
| 144 | s.updateMapping(ctx, mapping, nats, true) |
| 145 | } |
| 146 | |
| 147 | for _, mapping := range toUpdate { |
| 148 | s.updateMapping(ctx, mapping, nats, false) |
| 149 | } |
| 150 | |
| 151 | return len(nats), renewIn |
| 152 | } |
| 153 | |
| 154 | func (s *Service) scheduleProcess() { |
| 155 | select { |
no test coverage detected