| Conditions | 4 |
| Total Lines | 55 |
| Code Lines | 32 |
| 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 |
||
| 99 | func (c *Balancer) Check(ctx context.Context, request *base.PermissionCheckRequest) (*base.PermissionCheckResponse, error) { |
||
| 100 | // Fetch the EntityDefinition for the given tenant, entity type, and schema version. |
||
| 101 | en, _, err := c.schemaReader.ReadEntityDefinition(ctx, request.GetTenantId(), request.GetEntity().GetType(), request.GetMetadata().GetSchemaVersion()) |
||
| 102 | if err != nil { |
||
| 103 | slog.Error(err.Error()) |
||
| 104 | // If an error occurs while reading the entity definition, deny permission and return the error. |
||
| 105 | return &base.PermissionCheckResponse{ |
||
| 106 | Can: base.CheckResult_CHECK_RESULT_DENIED, |
||
| 107 | Metadata: &base.PermissionCheckResponseMetadata{ |
||
| 108 | CheckCount: 0, |
||
| 109 | }, |
||
| 110 | }, err |
||
| 111 | } |
||
| 112 | |||
| 113 | isRelational := engines.IsRelational(en, request.GetPermission()) |
||
| 114 | |||
| 115 | // Create a new xxhash instance. |
||
| 116 | h := xxhash.New() |
||
| 117 | |||
| 118 | // Generate a unique key for the request based on its relational state. |
||
| 119 | // This key helps in distributing the request. |
||
| 120 | _, err = h.Write([]byte(engines.GenerateKey(request, isRelational))) |
||
| 121 | if err != nil { |
||
| 122 | slog.Error(err.Error()) |
||
| 123 | return &base.PermissionCheckResponse{ |
||
| 124 | Can: base.CheckResult_CHECK_RESULT_DENIED, |
||
| 125 | Metadata: &base.PermissionCheckResponseMetadata{ |
||
| 126 | CheckCount: 0, |
||
| 127 | }, |
||
| 128 | }, err |
||
| 129 | } |
||
| 130 | k := hex.EncodeToString(h.Sum(nil)) |
||
| 131 | |||
| 132 | // Add a timeout of 2 seconds to the context and also set the generated key as a value. |
||
| 133 | withTimeout, cancel := context.WithTimeout(context.WithValue(ctx, balancer.Key, k), 4*time.Second) |
||
| 134 | defer cancel() |
||
| 135 | |||
| 136 | // Logging the intention to forward the request to the underlying client. |
||
| 137 | slog.Debug("Forwarding request with key to the underlying client", slog.String("key", k)) |
||
| 138 | |||
| 139 | // Perform the actual permission check by making a call to the underlying client. |
||
| 140 | response, err := c.client.Check(withTimeout, request) |
||
| 141 | if err != nil { |
||
| 142 | // Log the error and return it. |
||
| 143 | slog.Error(err.Error()) |
||
| 144 | return &base.PermissionCheckResponse{ |
||
| 145 | Can: base.CheckResult_CHECK_RESULT_DENIED, |
||
| 146 | Metadata: &base.PermissionCheckResponseMetadata{ |
||
| 147 | CheckCount: 0, |
||
| 148 | }, |
||
| 149 | }, err |
||
| 150 | } |
||
| 151 | |||
| 152 | // Return the response received from the client. |
||
| 153 | return response, nil |
||
| 154 | } |
||
| 155 |