| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package electron |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"math" |
| 25 |
"reflect" |
| 26 |
"time" |
| 27 |
) |
| 28 |
|
| 29 |
// Timeout is the error returned if an operation does not complete on time. |
| 30 |
// |
| 31 |
// Methods named *Timeout in this package take time.Duration timeout parameter. |
| 32 |
// |
| 33 |
// If timeout > 0 and there is no result available before the timeout, they |
| 34 |
// return a zero or nil value and Timeout as an error. |
| 35 |
// |
| 36 |
// If timeout == 0 they will return a result if one is immediately available or |
| 37 |
// nil/zero and Timeout as an error if not. |
| 38 |
// |
| 39 |
// If timeout == Forever the function will return only when there is a result or |
| 40 |
// some non-timeout error occurs. |
| 41 |
// |
| 42 |
var Timeout = fmt.Errorf("timeout") |
| 43 |
|
| 44 |
// Forever can be used as a timeout parameter to indicate wait forever. |
| 45 |
const Forever time.Duration = math.MaxInt64 |
| 46 |
|
| 47 |
// timedReceive receives on channel (which can be a chan of any type), waiting |
| 48 |
// up to timeout. |
| 49 |
// |
| 50 |
// timeout==0 means do a non-blocking receive attempt. timeout < 0 means block |
| 51 |
// forever. Other values mean block up to the timeout. |
| 52 |
// |
| 53 |
// Returns error Timeout on timeout, Closed on channel close. |
| 54 |
func timedReceive(channel interface{}, timeout time.Duration) (interface{}, error) { |
| 55 |
cases := []reflect.SelectCase{ |
| 56 |
reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(channel)}, |
| 57 |
} |
| 58 |
if timeout == 0 { // Non-blocking |
| 59 |
cases = append(cases, reflect.SelectCase{Dir: reflect.SelectDefault}) |
| 60 |
} else { // Block up to timeout |
| 61 |
cases = append(cases, |
| 62 |
reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(After(timeout))}) |
| 63 |
} |
| 64 |
chosen, value, ok := reflect.Select(cases) |
| 65 |
switch { |
| 66 |
case chosen == 0 && ok: |
| 67 |
return value.Interface(), nil |
| 68 |
case chosen == 0 && !ok: |
| 69 |
return nil, Closed |
| 70 |
default: |
| 71 |
return nil, Timeout |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// After is like time.After but returns a nil channel if timeout == Forever |
| 76 |
// since selecting on a nil channel will never return. |
| 77 |
func After(timeout time.Duration) <-chan time.Time { |
| 78 |
if timeout == Forever { |
| 79 |
return nil |
| 80 |
} else { |
| 81 |
return time.After(timeout) |
| 82 |
} |
| 83 |
} |