sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [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 | // gRPC affinity router with active/active backends |
| 17 | |
| 18 | package afrouter |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
| 22 | "errors" |
| 23 | "google.golang.org/grpc" |
| 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | RT_RPC_AFFINITY_MESSAGE = iota+1 |
| 28 | RT_RPC_AFFINITY_HEADER = iota+1 |
| 29 | RT_BINDING = iota+1 |
| 30 | RT_ROUND_ROBIN = iota+1 |
| 31 | ) |
| 32 | |
| 33 | // String names for display in error messages. |
| 34 | var rTypeNames = []string{"","rpc_affinity_message","rpc_affinity_header","binding", "round_robin"} |
| 35 | var rAssnNames = []string{"","round_robin"} |
| 36 | |
| 37 | var allRouters map[string]Router = make(map[string]Router) |
| 38 | |
| 39 | // The router interface |
| 40 | type Router interface { |
| 41 | Name() (string) |
| 42 | Route(interface{}) *backend |
| 43 | Service() (string) |
| 44 | BackendCluster(string, string) (*backendCluster, error) |
| 45 | FindBackendCluster(string) (*backendCluster) |
| 46 | ReplyHandler(interface{}) error |
| 47 | GetMetaKeyVal(serverStream grpc.ServerStream) (string,string,error) |
| 48 | } |
| 49 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 50 | func newRouter(config *RouterConfig) (Router, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 51 | r,err := newMethodRouter(config) |
| 52 | if err == nil { |
| 53 | allRouters[r.Name()] = r |
| 54 | } |
| 55 | return r, err |
| 56 | } |
| 57 | |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 58 | func newSubRouter(rconf *RouterConfig, config *RouteConfig) (Router, error) { |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 59 | idx := strIndex(rTypeNames, config.Type) |
sslobodr | 5f0b5a3 | 2019-01-24 07:45:19 -0500 | [diff] [blame] | 60 | switch idx { |
| 61 | case RT_RPC_AFFINITY_MESSAGE: |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 62 | r,err := newAffinityRouter(rconf, config) |
sslobodr | 5f0b5a3 | 2019-01-24 07:45:19 -0500 | [diff] [blame] | 63 | if err == nil { |
| 64 | allRouters[rconf.Name+config.Name] = r |
| 65 | } |
| 66 | return r, err |
| 67 | case RT_BINDING: |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 68 | r,err := newBindingRouter(rconf, config) |
sslobodr | 5f0b5a3 | 2019-01-24 07:45:19 -0500 | [diff] [blame] | 69 | if err == nil { |
| 70 | allRouters[rconf.Name+config.Name] = r |
| 71 | } |
| 72 | return r, err |
| 73 | case RT_ROUND_ROBIN: |
sslobodr | cd37bc5 | 2019-01-24 11:47:16 -0500 | [diff] [blame] | 74 | r,err := newRoundRobinRouter(rconf, config) |
sslobodr | 5f0b5a3 | 2019-01-24 07:45:19 -0500 | [diff] [blame] | 75 | if err == nil { |
| 76 | allRouters[rconf.Name+config.Name] = r |
| 77 | } |
| 78 | return r, err |
| 79 | default: |
| 80 | return nil, errors.New(fmt.Sprintf("Internal error, undefined router type: %s", config.Type)) |
| 81 | } |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 82 | |
sslobodr | 392ebd5 | 2019-01-18 12:41:49 -0500 | [diff] [blame] | 83 | return nil, errors.New(fmt.Sprintf("Unrecognized router type '%s'",config.Type)) |
| 84 | } |