Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Optimize HTTP Router for Improved Performance and Code Efficiency #10

Open
nikita-shtimenko opened this issue Feb 25, 2024 · 0 comments

Comments

@nikita-shtimenko
Copy link

Problem:

The current implementation of the HTTP router suffers from redundancy and inefficiencies, particularly in route handling. Routes with the same path but different HTTP methods or handlers lead to duplicated route definitions, increased memory consumption, and slower lookup times.

Example:

package main

import (
	"fmt"
	"net/http"
	"test/pkg/flow"
)

func main() {
	mux := flow.New()
	mux.HandleFunc("/index", indexHandler, "GET", "POST")
	mux.HandleFunc("/index", indexPutHandler, "PUT")
	mux.HandleFunc("/index", indexDeleteHandler, "DELETE")

	fmt.Println(*mux.GetAllRoutes())
}

func indexHandler(w http.ResponseWriter, r *http.Request)       {}
func indexPutHandler(w http.ResponseWriter, r *http.Request)    {}
func indexDeleteHandler(w http.ResponseWriter, r *http.Request) {}
type Mux struct {
	NotFound         http.Handler
	MethodNotAllowed http.Handler
	Options          http.Handler
	routes           *[]route
	middlewares      []func(http.Handler) http.Handler
}

// for testing purposes
func (m *Mux) GetAllRoutes() *[]route {
	return m.routes
}

Output:

[{GET [ index] false 0x100a99160} {POST [ index] false 0x100a99160} {HEAD [ index] false 0x100a99160} {PUT [ index] false 0x100a99170} {DELETE [ index] false 0x100a99180}]

Proposed Solution:

To address these issues, I propose optimizing the HTTP router by consolidating routes with the same path into single route objects and internally mapping HTTP methods to their corresponding handlers. This approach reduces memory usage, streamlines route matching, and simplifies the codebase. By leveraging maps for method-handler associations, we achieve faster lookup times and improved runtime performance.

Implementation Details:

  1. Route Consolidation: Routes with the same path will be consolidated into single route objects.
  2. Method-Handler Mapping: Internally within each route object, HTTP methods will be mapped to their corresponding handlers using a map data structure.
  3. Route Matching Refinement: The route matching logic will be simplified to focus solely on comparing the request's path to the route's path. Once a matching route is found, the appropriate handler based on the request's HTTP method will be retrieved from the route's internal map (405 in case HTTP-Method is not supported).
  4. Code Refactoring: The codebase will be refactored to encapsulate method-handler associations within route objects, promoting better code organization and maintainability.

P.S This is my first-ever issue/proposal so feel free to correct and educate me in case I am wrong or missing something!
P.S.S @alexedwards In case this proposal seems reasonable I will be more than happy to create a PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant