| Conditions | 10 |
| Total Lines | 69 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like balancer.NewCheckEngineWithBalancer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package balancer |
||
| 31 | func NewCheckEngineWithBalancer( |
||
| 32 | ctx context.Context, |
||
| 33 | checker invoke.Check, |
||
| 34 | schemaReader storage.SchemaReader, |
||
| 35 | no string, |
||
| 36 | dst *config.Distributed, |
||
| 37 | srv *config.GRPC, |
||
| 38 | authn *config.Authn, |
||
| 39 | ) (invoke.Check, error) { |
||
| 40 | var ( |
||
| 41 | creds credentials.TransportCredentials |
||
| 42 | options []grpc.DialOption |
||
| 43 | isSecure bool |
||
| 44 | err error |
||
| 45 | ) |
||
| 46 | |||
| 47 | // Set up TLS credentials if paths are provided |
||
| 48 | if srv.TLSConfig.Enabled && srv.TLSConfig.CertPath != "" { |
||
| 49 | isSecure = true |
||
| 50 | creds, err = credentials.NewClientTLSFromFile(srv.TLSConfig.CertPath, no) |
||
| 51 | if err != nil { |
||
| 52 | return nil, fmt.Errorf("could not load TLS certificate: %w", err) |
||
|
|
|||
| 53 | } |
||
| 54 | } else { |
||
| 55 | creds = insecure.NewCredentials() |
||
| 56 | } |
||
| 57 | |||
| 58 | bc := &balancer.Config{ |
||
| 59 | PartitionCount: dst.PartitionCount, |
||
| 60 | ReplicationFactor: dst.ReplicationFactor, |
||
| 61 | Load: dst.Load, |
||
| 62 | PickerWidth: dst.PickerWidth, |
||
| 63 | } |
||
| 64 | |||
| 65 | bcjson, err := bc.ServiceConfigJSON() |
||
| 66 | if err != nil { |
||
| 67 | return nil, err |
||
| 68 | } |
||
| 69 | |||
| 70 | // Append common options |
||
| 71 | options = append( |
||
| 72 | options, |
||
| 73 | grpc.WithDefaultServiceConfig(bcjson), |
||
| 74 | grpc.WithTransportCredentials(creds), |
||
| 75 | ) |
||
| 76 | |||
| 77 | // Handle authentication if enabled |
||
| 78 | if authn != nil && authn.Enabled { |
||
| 79 | token, err := setupAuthn(ctx, authn) |
||
| 80 | if err != nil { |
||
| 81 | return nil, err |
||
| 82 | } |
||
| 83 | if isSecure { |
||
| 84 | options = append(options, grpc.WithPerRPCCredentials(secureTokenCredentials{"authorization": "Bearer " + token})) |
||
| 85 | } else { |
||
| 86 | options = append(options, grpc.WithPerRPCCredentials(nonSecureTokenCredentials{"authorization": "Bearer " + token})) |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | conn, err := grpc.NewClient(dst.Address, options...) |
||
| 91 | if err != nil { |
||
| 92 | return nil, err |
||
| 93 | } |
||
| 94 | |||
| 95 | return &Balancer{ |
||
| 96 | schemaReader: schemaReader, |
||
| 97 | checker: checker, |
||
| 98 | client: base.NewPermissionClient(conn), |
||
| 99 | }, nil |
||
| 100 | } |
||
| 143 |