* If timeout less than 0, If Queue is full, return (nil, ErrFullQueue). * If timeout equals to 0, block until put a value into Queue. * If timeout greater than 0, wait timeout seconds until put a value into Queue, if timeout passed, return (nil, ErrFullQueue).
(val interface{}, timeout float64)
| 161 | // * If timeout greater than 0, wait timeout seconds until put a value into Queue, |
| 162 | // if timeout passed, return (nil, ErrFullQueue). |
| 163 | func (q *Queue) Put(val interface{}, timeout float64) error { |
| 164 | q.mutex.Lock() |
| 165 | q.clearPending() |
| 166 | isfull := q.isfull() |
| 167 | if timeout < 0.0 && isfull { |
| 168 | q.mutex.Unlock() |
| 169 | return ErrFullQueue |
| 170 | } |
| 171 | |
| 172 | if !isfull { |
| 173 | if !q.notifyGetter(nil, val) { |
| 174 | q.put(val) |
| 175 | } |
| 176 | q.mutex.Unlock() |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | e := q.newPutter() |
| 181 | q.mutex.Unlock() |
| 182 | w := e.Value.(waiter) |
| 183 | if timeout == 0.0 { |
| 184 | <-w |
| 185 | } else { |
| 186 | select { |
| 187 | case <-w: |
| 188 | case <-time.After(time.Duration(timeout) * time.Second): |
| 189 | return ErrFullQueue |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | q.mutex.Lock() |
| 194 | if !q.notifyGetter(e, val) { |
| 195 | q.put(e) |
| 196 | } |
| 197 | q.mutex.Unlock() |
| 198 | return nil |
| 199 | } |
| 200 | |
| 201 | func (q *Queue) size() int { |
| 202 | return q.items.Len() |
no test coverage detected