UpsertEdge fetches an Edge from the store and updates it using the given callback. If the Edge doesn't exist yet, it creates a new one with the default TTL. If the Edge is complete after applying the callback, it's completed and removed.
(key string, side Side, update Callback)
| 99 | // doesn't exist yet, it creates a new one with the default TTL. |
| 100 | // If the Edge is complete after applying the callback, it's completed and removed. |
| 101 | func (s *store) UpsertEdge(key string, side Side, update Callback) (isNew bool, err error) { |
| 102 | s.mtx.Lock() |
| 103 | defer s.mtx.Unlock() |
| 104 | |
| 105 | if storedEdge, ok := s.m[key]; ok { |
| 106 | edge := storedEdge.Value.(*Edge) |
| 107 | update(edge) |
| 108 | |
| 109 | if edge.isComplete() { |
| 110 | s.onComplete(edge) |
| 111 | s.deleteEdge(storedEdge) |
| 112 | } |
| 113 | |
| 114 | return false, nil |
| 115 | } |
| 116 | |
| 117 | if s.hasDroppedCounterpart(key, side) { |
| 118 | return true, ErrDroppedSpanSide |
| 119 | } |
| 120 | |
| 121 | edge := s.grabEdge(key) |
| 122 | update(edge) |
| 123 | |
| 124 | if edge.isComplete() { |
| 125 | s.onComplete(edge) |
| 126 | s.returnEdge(edge) |
| 127 | return true, nil |
| 128 | } |
| 129 | |
| 130 | // Check we can add new edges |
| 131 | if s.l.Len() >= s.maxItems { |
| 132 | // todo: try to evict expired items |
| 133 | s.returnEdge(edge) |
| 134 | return false, ErrTooManyItems |
| 135 | } |
| 136 | |
| 137 | ele := s.l.PushBack(edge) |
| 138 | s.m[key] = ele |
| 139 | |
| 140 | return true, nil |
| 141 | } |
| 142 | |
| 143 | // Expire evicts all expired items in the store. |
| 144 | func (s *store) Expire() { |
nothing calls this directly
no test coverage detected