Skip to content

martian: remove Context and Session#683

Merged
mmatczuk merged 20 commits intomainfrom
mmt/remove_session
Feb 12, 2024
Merged

martian: remove Context and Session#683
mmatczuk merged 20 commits intomainfrom
mmt/remove_session

Conversation

@mmatczuk
Copy link
Contributor

@mmatczuk mmatczuk commented Feb 8, 2024

This patchset is a significant refactoring of the martian core.
It's a continuation of work initiated in #444.

The RequestModifier and ResponseModifier errors are handled and appropriate error response is generated based on ErrorResponse(). Subsequently the connection hijacking functionality is removed.

Proxy connection proxyConn is introduced, and proxy code is restructured to more files focused around specific topics. The proxyConn and proxyHandler have similar structure for easier navigation and compatibility. The Proxy remains for configuration and generic shared items. This has been done in #673 but we expand on it here.

A TraceID is added and it takes the responsibility of request ID generation and duration tracking. The custom Context and Session structs are removed resulting in streamlined function interfaces. TraceID is attached to request context on creation.

Logging is streamlined, single responsibility principle is applied allowing for logger stacking. Each logger has its domain.

Fixes #525
Fixes #523
Fixes #497
Fixes #498

@mmatczuk mmatczuk requested a review from a team as a code owner February 8, 2024 11:55
@mmatczuk mmatczuk force-pushed the mmt/remove_session branch 6 times, most recently from b781075 to 2cd0e39 Compare February 9, 2024 12:58
@mmatczuk mmatczuk changed the title martian: remove session hijacking martian: remove Context and Session Feb 9, 2024
Session hijacking is a design mistake.
Without it Proxy has full ownership of the connections.
This also simplifies the overall workflow and makes code less error-prone.

Fixes #497
Unify error handling for all callers of writeResponse().
Reduce the scope of writeResponse() and unify proxyConn and proxyHandler response writing API.
Unify error logging in proxyConn and proxyHandler.
The returned modifier error is debug logged and error response is created.
The error response is passed to ResponseModifier, if that fails error is error logged.
The error response is sent back to client as normal response.

Fixes #498
Fixes #525
Remove Session as it has no purpose beyond hijacking.
Move secure connection detection to newProxyConn(),
thanks to that we no longer call locking ConnectionState() on every request.
ErrorStatus enables communication between martian packages and ErrorResponse() implementations.
ErrorStatus can be returned as a request or response modification error and instructs ErrorResponse() what is the desired status code.
As we handle RequestModifier errors now it makes sense to fail fast.
The RequestModifier responsibility has been moved to martian.ErrorStatus, and will be handled in Forwarder.
The use RT skipping is limited to testing only, as on RequestModifier error the RT is automatically skipped.
Replace RT skipping per context with global Proxy.TestingSkipRoundTrip for testing purposes.
Add traceID type responsible for the request trace ID generation.
The traceID is attached to request context on read.
Remove Context struct as it serves no purpose.
Remove all logic from logger the package just map calls from package level function to the provided Logger impl.
Add context to the Logger interface, this allows to add custom logger wrappers using the context information.
Move trace ID logging logic close to traceID.
Use logger stacking to add features to logs.

This is only temporary and traceID should be moved to the toplevel log package.
To make it work we would need to change the main Logger interface, see #544.

var _ martianlog.Logger = contextLogger{}

func (l contextLogger) Errorf(_ context.Context, format string, args ...interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use any instead of interface{}.

BTW the linter should have caught it.

@Choraden
Copy link
Contributor

Choraden commented Feb 9, 2024

I'm afraid, this does not fully fix #498 as it still reports denied requests as errors in metrics.

forwarder_proxy_errors_total{reason="denied"} 1

Let's open another one or do not resolve it here.

@Choraden
Copy link
Contributor

Choraden commented Feb 9, 2024

While trying new via loop detection mechanism, I found #688.

This PR did not introduce this. It exists in the old approach as well.

Treat label "-" as ignore indicator, and do not record deny errors in errors metric.

Fixes #498
@mmatczuk mmatczuk merged commit 9f22053 into main Feb 12, 2024
@mmatczuk mmatczuk deleted the mmt/remove_session branch February 12, 2024 11:50
}

hp.metrics.error(label)
if label != "-" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A constant would fit better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants