khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package utils |
| 17 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 18 | import ( |
| 19 | "google.golang.org/grpc/codes" |
| 20 | "google.golang.org/grpc/status" |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 21 | "os" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 22 | "reflect" |
| 23 | "time" |
| 24 | ) |
| 25 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 26 | type DeviceID struct { |
| 27 | Id string |
| 28 | } |
| 29 | |
| 30 | type LogicalDeviceID struct { |
| 31 | Id string |
| 32 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 33 | |
khenaidoo | 631fe54 | 2019-05-31 15:44:43 -0400 | [diff] [blame] | 34 | func GetHostName() string { |
| 35 | return os.Getenv("HOSTNAME") |
| 36 | } |
| 37 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 38 | //WaitForNilOrErrorResponses waits on a variadic number of channels for either a nil response or an error |
| 39 | //response. If an error is received from a given channel then the returned error array will contain that error. |
| 40 | //The error will be at the index corresponding to the order in which the channel appear in the parameter list. |
| 41 | //If no errors is found then nil is returned. This method also takes in a timeout in milliseconds. If a |
| 42 | //timeout is obtained then this function will stop waiting for the remaining responses and abort. |
| 43 | func WaitForNilOrErrorResponses(timeout int64, chnls ...chan interface{}) []error { |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 44 | if len(chnls) == 0 { |
| 45 | return nil |
| 46 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 47 | // Create a timeout channel |
| 48 | tChnl := make(chan *interface{}) |
| 49 | go func() { |
| 50 | time.Sleep(time.Duration(timeout) * time.Millisecond) |
| 51 | tChnl <- nil |
| 52 | }() |
| 53 | |
| 54 | errorsReceived := false |
| 55 | errors := make([]error, len(chnls)) |
| 56 | cases := make([]reflect.SelectCase, len(chnls)+1) |
| 57 | for i, ch := range chnls { |
| 58 | cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)} |
| 59 | } |
| 60 | // Add the timeout channel |
| 61 | cases[len(chnls)] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(tChnl)} |
| 62 | |
| 63 | resultsReceived := make([]bool, len(errors)+1) |
| 64 | remaining := len(cases) - 1 |
| 65 | for remaining > 0 { |
| 66 | index, value, ok := reflect.Select(cases) |
| 67 | if !ok { // closed channel |
| 68 | //Set the channel at that index to nil to disable this case, hence preventing it from interfering with other cases. |
| 69 | cases[index].Chan = reflect.ValueOf(nil) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 70 | errors[index] = status.Error(codes.Internal, "channel closed") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 71 | errorsReceived = true |
| 72 | } else if index == len(chnls) { // Timeout has occurred |
| 73 | for k := range errors { |
| 74 | if !resultsReceived[k] { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 75 | errors[k] = status.Error(codes.Aborted, "timeout") |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | errorsReceived = true |
| 79 | break |
| 80 | } else if value.IsNil() { // Nil means a good response |
| 81 | //do nothing |
| 82 | } else if err, ok := value.Interface().(error); ok { // error returned |
| 83 | errors[index] = err |
| 84 | errorsReceived = true |
| 85 | } else { // unknown value |
| 86 | errors[index] = status.Errorf(codes.Internal, "%s", value) |
| 87 | errorsReceived = true |
| 88 | } |
| 89 | resultsReceived[index] = true |
| 90 | remaining -= 1 |
| 91 | } |
| 92 | |
| 93 | if errorsReceived { |
| 94 | return errors |
| 95 | } |
| 96 | return nil |
| 97 | } |