Skip to content

Proposed Binder feature: requireExplicitScopes() #1111

@jhaber

Description

@jhaber

One common source of Guice issues we have is our developers forgetting to bind stateful objects as singletons (like a cache, JDBC pool, HTTP client, rate limiters, etc.), which causes problems because the default is unscoped. Hopefully this is caught before it makes it to production, but this exact issue has caused large outages for our application in the past. One idea I had to make this less likely is to add a new option to the binder interface that will require bindings have an explicit scope (inspired by the requireExplicitBindings option). If you want an unscoped binding, that's fine, just bind to Scopes.NO_SCOPE. But at least this way all scoping choices must be explicit. It will hopefully be much easier to catch in code review that someone is binding a cache to Scopes.NO_SCOPE rather than having to notice that a scope was omitted. One detail I'm unsure of is whether scope annotations on the class itself should satisfy this validation, but I'm leaning towards yes (in which case we might also want to add a @NoScope or @Unscoped annotation for convenience).

Another way to satisfy this use-case would be to provide a way to change the default scope (which was already proposed in #887), in which case we could write a scope that simply throws an exception and set it as the default.

Interested to hear feedback on this proposal, thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions