Download this file
| 1 | package hostfiles |
| 2 | |
| 3 | import ( |
| 4 | "sort" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // queue holds, per path, the time by which its first unpublished |
| 9 | // modification must be published. A later modification of a path already |
| 10 | // waiting does not move its deadline, so a file written without pause is |
| 11 | // still published every period. |
| 12 | type queue struct { |
| 13 | d time.Duration |
| 14 | due map[string]time.Time |
| 15 | } |
| 16 | |
| 17 | func newQueue(d time.Duration) *queue { return &queue{d: d, due: map[string]time.Time{}} } |
| 18 | |
| 19 | func (q *queue) Touch(rel string, now time.Time) { |
| 20 | if _, waiting := q.due[rel]; !waiting { |
| 21 | q.due[rel] = now.Add(q.d) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Due removes and returns every path whose deadline has arrived. |
| 26 | func (q *queue) Due(now time.Time) []string { |
| 27 | var out []string |
| 28 | for rel, at := range q.due { |
| 29 | if !at.After(now) { |
| 30 | out = append(out, rel) |
| 31 | delete(q.due, rel) |
| 32 | } |
| 33 | } |
| 34 | sort.Strings(out) |
| 35 | return out |
| 36 | } |
| 37 | |