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

Phase 2: Implement Server and serve command #2

Open
wants to merge 24 commits into
base: cli
Choose a base branch
from
Open

Conversation

prog-supdex
Copy link
Collaborator

This pull request implements the server functionality and the serve command.
It includes significant refactoring for modularity and better test coverage

Key Features:

  • Added core server logic, including background cleanup routines for inactive nodes.
  • Implemented license distribution strategies (fifo, lifo, random) with the --strategy flag
  • Added validation to ensure only valid values for the --strategy flag
  • Added unit tests focusing on license claiming and node cleanup

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

internal/server/handler.go Outdated Show resolved Hide resolved
Copy link
Member

@ezekg ezekg left a comment

Choose a reason for hiding this comment

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

Initial review. Looking good. In addition to the line comments — for CLI commands, I'd like to clean up error messaging, because right now this is very verbose:

./relay add -vvvv --file tmp/2.lic --key $(cat tmp/2.key) --public-key $(cat tmp/keygen.pub)
# time=2024-09-27T10:36:39.330-05:00 level=ERROR msg="failed to insert license into store" licenseID=e9b6f649-d8c2-49e3-9d41-aab32f0d08a3 error="UNIQUE constraint failed: licenses.key"
# error creating license record: failed to insert license: UNIQUE constraint failed: licenses.key
# Error: failed to insert license: UNIQUE constraint failed: licenses.key
# time=2024-09-27T10:36:39.330-05:00 level=ERROR msg="failed to execute command" error="failed to insert license: UNIQUE constraint failed: licenses.key"

This type of logging is fine for the server (e.g. timestamps, etc.), but the CLI should have better error messaging, with a single clear and colored error message. There are also duplicate error logs in the above example that I'd like to consolidate.

@@ -55,13 +65,13 @@ func Run() int {

rootCmd.PersistentFlags().StringVar(&cfg.DB.DatabaseFilePath, "database", "./relay.sqlite", "specify an alternate database path")
rootCmd.PersistentFlags().CountVarP(&cfg.Logger.Verbosity, "verbose", "v", "counted verbosity")
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this flag is working as expected. By default, the log level should be error, then:

  • -v should be error.
  • -vv should be warn.
  • -vvv should be info.
  • -vvvv+ should be debug.

Right now, I'm not really sure what the order is, but e.g. it seems like -vv enables debug logging. The expected behavior would be the more -v flags, the more verbose logging should get.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh yea, my bad

I have fixed it


handler := server.NewHandler(srv.Manager())
router := mux.NewRouter()
handler.RegisterRoutes(router)
Copy link
Member

Choose a reason for hiding this comment

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

Can we add general server logging to stdout e.g. I want to log 200s, 404s, 500s, etc.?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have added the logger middleware, so now we can track HTTP requests like this

2024-09-28 18:40:32 INF server/middleware.go:35 HTTP request method=PUT path=/v1/nodes/329ecfa4c5d24084b416fc35a306cf65 status=201 remote_addr=127.0.0.1:57798 user_agent=curl/7.81.0 duration=8.893744ms

I also updated the CLI responses to be more user-friendly and added some color for better readability

return store.ClaimUnclaimedLicenseFIFO(ctx, nodeID)
case "lifo":
return store.ClaimUnclaimedLicenseLIFO(ctx, nodeID)
case "random":
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
case "random":
case "rand":

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nice catch, fixed!

}

cmd.Flags().IntVarP(&cfg.ServerPort, "port", "p", cfg.ServerPort, "Port to run the relay server on")
cmd.Flags().DurationVarP(&cfg.TTL, "ttl", "t", cfg.TTL, "Time-to-live for license claims")
Copy link
Member

Choose a reason for hiding this comment

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

Let's add a validation to this flag for it to be at minimum 30s.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done!
I also added tests for it

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

Successfully merging this pull request may close these issues.

3 participants