| Conditions | 9 |
| Total Lines | 56 |
| Code Lines | 40 |
| 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:
| 1 | package balancer |
||
| 39 | func NewCheckEngineWithBalancer( |
||
| 40 | ctx context.Context, |
||
| 41 | checker invoke.Check, |
||
| 42 | schemaReader storage.SchemaReader, |
||
| 43 | dst *config.Distributed, |
||
| 44 | srv *config.GRPC, |
||
| 45 | authn *config.Authn, |
||
| 46 | ) (invoke.Check, error) { |
||
| 47 | var ( |
||
| 48 | creds credentials.TransportCredentials |
||
| 49 | options []grpc.DialOption |
||
| 50 | isSecure bool |
||
| 51 | err error |
||
| 52 | ) |
||
| 53 | |||
| 54 | // Set up TLS credentials if paths are provided |
||
| 55 | if srv.TLSConfig.CertPath != "" && srv.TLSConfig.KeyPath != "" { |
||
| 56 | isSecure = true |
||
| 57 | creds, err = credentials.NewClientTLSFromFile(srv.TLSConfig.CertPath, srv.TLSConfig.KeyPath) |
||
| 58 | if err != nil { |
||
| 59 | return nil, fmt.Errorf("could not load TLS certificate: %s", err) |
||
| 60 | } |
||
| 61 | } else { |
||
| 62 | creds = insecure.NewCredentials() |
||
| 63 | } |
||
| 64 | |||
| 65 | // Append common options |
||
| 66 | options = append( |
||
| 67 | options, |
||
| 68 | grpc.WithDefaultServiceConfig(grpcServicePolicy), |
||
| 69 | grpc.WithTransportCredentials(creds), |
||
| 70 | ) |
||
| 71 | |||
| 72 | // Handle authentication if enabled |
||
| 73 | if authn != nil && authn.Enabled { |
||
| 74 | token, err := setupAuthn(ctx, authn) |
||
| 75 | if err != nil { |
||
| 76 | return nil, err |
||
| 77 | } |
||
| 78 | if isSecure { |
||
| 79 | options = append(options, grpc.WithPerRPCCredentials(secureTokenCredentials{"authorization": "Bearer " + token})) |
||
| 80 | } else { |
||
| 81 | options = append(options, grpc.WithPerRPCCredentials(nonSecureTokenCredentials{"authorization": "Bearer " + token})) |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | conn, err := grpc.Dial(dst.Address, options...) |
||
| 86 | if err != nil { |
||
| 87 | return nil, err |
||
| 88 | } |
||
| 89 | |||
| 90 | return &Balancer{ |
||
| 91 | schemaReader: schemaReader, |
||
| 92 | checker: checker, |
||
| 93 | client: base.NewPermissionClient(conn), |
||
| 94 | }, nil |
||
| 95 | } |
||
| 155 |