General afrouter cleanup.
- Separated backend.go into multiple files.
- Replaced array indexing hack with enum pattern.
- Various renaming for better consistency.
- Removed a few unused structs.
- Replaced a thread with an atomic operation.
Change-Id: I2239692cac21ddb7f513b6d8c247ffa8789714ac
diff --git a/afrouter/afrouter/method-router.go b/afrouter/afrouter/method-router.go
index 5e11fa6..2c1ca4f 100644
--- a/afrouter/afrouter/method-router.go
+++ b/afrouter/afrouter/method-router.go
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-// gRPC affinity router with active/active backends
package afrouter
@@ -28,14 +27,14 @@
const NoMeta = "nometa"
type MethodRouter struct {
- name string
- service string
- mthdRt map[string]map[string]Router // map of [metadata][method]
+ name string
+ service string
+ methodRouter map[string]map[string]Router // map of [metadata][method]
}
func newMethodRouter(config *RouterConfig) (Router, error) {
- mr := MethodRouter{name: config.Name, service: config.ProtoService, mthdRt: make(map[string]map[string]Router)}
- mr.mthdRt[NoMeta] = make(map[string]Router) // For routes not needing metadata (all expcept binding at this time)
+ mr := MethodRouter{name: config.Name, service: config.ProtoService, methodRouter: make(map[string]map[string]Router)}
+ mr.methodRouter[NoMeta] = make(map[string]Router) // For routes not needing metadata (all expcept binding at this time)
log.Debugf("Processing MethodRouter config %v", *config)
if len(config.Routes) == 0 {
return nil, errors.New(fmt.Sprintf("Router %s must have at least one route", config.Name))
@@ -47,10 +46,10 @@
if err != nil {
return nil, err
}
- if rtv.Type == "binding" {
+ if rtv.Type == RouteTypeBinding {
idx1 = rtv.Binding.Field
- if _, ok := mr.mthdRt[idx1]; ok == false { // /First attempt on this key
- mr.mthdRt[idx1] = make(map[string]Router)
+ if _, ok := mr.methodRouter[idx1]; !ok { // /First attempt on this key
+ mr.methodRouter[idx1] = make(map[string]Router)
}
} else {
idx1 = NoMeta
@@ -63,11 +62,11 @@
return r, nil
} else {
log.Debugf("Setting router '%s' for single method '%s'", r.Name(), rtv.Methods[0])
- if _, ok := mr.mthdRt[idx1][rtv.Methods[0]]; ok == false {
- mr.mthdRt[idx1][rtv.Methods[0]] = r
+ if _, ok := mr.methodRouter[idx1][rtv.Methods[0]]; !ok {
+ mr.methodRouter[idx1][rtv.Methods[0]] = r
} else {
err := errors.New(fmt.Sprintf("Attempt to define method %s for 2 routes: %s & %s", rtv.Methods[0],
- r.Name(), mr.mthdRt[idx1][rtv.Methods[0]].Name()))
+ r.Name(), mr.methodRouter[idx1][rtv.Methods[0]].Name()))
log.Error(err)
return mr, err
}
@@ -75,11 +74,11 @@
default:
for _, m := range rtv.Methods {
log.Debugf("Processing Method %s", m)
- if _, ok := mr.mthdRt[idx1][m]; ok == false {
+ if _, ok := mr.methodRouter[idx1][m]; !ok {
log.Debugf("Setting router '%s' for method '%s'", r.Name(), m)
- mr.mthdRt[idx1][m] = r
+ mr.methodRouter[idx1][m] = r
} else {
- err := errors.New(fmt.Sprintf("Attempt to define method %s for 2 routes: %s & %s", m, r.Name(), mr.mthdRt[idx1][m].Name()))
+ err := errors.New(fmt.Sprintf("Attempt to define method %s for 2 routes: %s & %s", m, r.Name(), mr.methodRouter[idx1][m].Name()))
log.Error(err)
return mr, err
}
@@ -99,8 +98,8 @@
}
func (mr MethodRouter) GetMetaKeyVal(serverStream grpc.ServerStream) (string, string, error) {
- var rtrnK string = NoMeta
- var rtrnV string = ""
+ var rtrnK = NoMeta
+ var rtrnV = ""
// Get the metadata from the server stream
md, ok := metadata.FromIncomingContext(serverStream.Context())
@@ -109,8 +108,8 @@
}
// Determine if one of the method routing keys exists in the metadata
- for k, _ := range mr.mthdRt {
- if _, ok := md[k]; ok == true {
+ for k := range mr.methodRouter {
+ if _, ok := md[k]; ok {
rtrnV = md[k][0]
rtrnK = k
break
@@ -123,7 +122,7 @@
func (mr MethodRouter) ReplyHandler(sel interface{}) error {
switch sl := sel.(type) {
case *sbFrame:
- if r, ok := mr.mthdRt[NoMeta][sl.method]; ok == true {
+ if r, ok := mr.methodRouter[NoMeta][sl.method]; ok {
return r.ReplyHandler(sel)
}
// TODO: this case should also be an error
@@ -137,18 +136,18 @@
func (mr MethodRouter) Route(sel interface{}) *backend {
switch sl := sel.(type) {
case *nbFrame:
- if r, ok := mr.mthdRt[sl.metaKey][sl.mthdSlice[REQ_METHOD]]; ok == true {
+ if r, ok := mr.methodRouter[sl.metaKey][sl.methodInfo.method]; ok {
return r.Route(sel)
}
- log.Errorf("Attept to route on non-existent method '%s'", sl.mthdSlice[REQ_METHOD])
+ log.Errorf("Attept to route on non-existent method '%s'", sl.methodInfo.method)
return nil
default:
return nil
}
}
-func (mr MethodRouter) BackendCluster(mthd string, metaKey string) (*backendCluster, error) {
- if r, ok := mr.mthdRt[metaKey][mthd]; ok == true {
+func (mr MethodRouter) BackendCluster(mthd string, metaKey string) (*cluster, error) {
+ if r, ok := mr.methodRouter[metaKey][mthd]; ok {
return r.BackendCluster(mthd, metaKey)
}
err := errors.New(fmt.Sprintf("No backend cluster exists for method '%s' using meta key '%s'", mthd, metaKey))
@@ -156,8 +155,8 @@
return nil, err
}
-func (mr MethodRouter) FindBackendCluster(beName string) *backendCluster {
- for _, meta := range mr.mthdRt {
+func (mr MethodRouter) FindBackendCluster(beName string) *cluster {
+ for _, meta := range mr.methodRouter {
for _, r := range meta {
if rtrn := r.FindBackendCluster(beName); rtrn != nil {
return rtrn