Takahiro Suzuki | 241c10e | 2020-12-17 20:17:57 +0900 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 gRPC authors. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | // Package connectivity defines connectivity semantics. |
| 20 | // For details, see https://github.com/grpc/grpc/blob/master/doc/connectivity-semantics-and-api.md. |
| 21 | // All APIs in this package are experimental. |
| 22 | package connectivity |
| 23 | |
| 24 | import ( |
| 25 | "context" |
| 26 | |
| 27 | "google.golang.org/grpc/grpclog" |
| 28 | ) |
| 29 | |
| 30 | // State indicates the state of connectivity. |
| 31 | // It can be the state of a ClientConn or SubConn. |
| 32 | type State int |
| 33 | |
| 34 | func (s State) String() string { |
| 35 | switch s { |
| 36 | case Idle: |
| 37 | return "IDLE" |
| 38 | case Connecting: |
| 39 | return "CONNECTING" |
| 40 | case Ready: |
| 41 | return "READY" |
| 42 | case TransientFailure: |
| 43 | return "TRANSIENT_FAILURE" |
| 44 | case Shutdown: |
| 45 | return "SHUTDOWN" |
| 46 | default: |
| 47 | grpclog.Errorf("unknown connectivity state: %d", s) |
| 48 | return "Invalid-State" |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | const ( |
| 53 | // Idle indicates the ClientConn is idle. |
| 54 | Idle State = iota |
| 55 | // Connecting indicates the ClientConn is connecting. |
| 56 | Connecting |
| 57 | // Ready indicates the ClientConn is ready for work. |
| 58 | Ready |
| 59 | // TransientFailure indicates the ClientConn has seen a failure but expects to recover. |
| 60 | TransientFailure |
| 61 | // Shutdown indicates the ClientConn has started shutting down. |
| 62 | Shutdown |
| 63 | ) |
| 64 | |
| 65 | // Reporter reports the connectivity states. |
| 66 | type Reporter interface { |
| 67 | // CurrentState returns the current state of the reporter. |
| 68 | CurrentState() State |
| 69 | // WaitForStateChange blocks until the reporter's state is different from the given state, |
| 70 | // and returns true. |
| 71 | // It returns false if <-ctx.Done() can proceed (ctx got timeout or got canceled). |
| 72 | WaitForStateChange(context.Context, State) bool |
| 73 | } |