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

consider easier APIs (as extensions, maybe) to use Entry #233

Closed
joshlong opened this issue Nov 8, 2018 · 1 comment · Fixed by #550
Closed

consider easier APIs (as extensions, maybe) to use Entry #233

joshlong opened this issue Nov 8, 2018 · 1 comment · Fixed by #550
Labels
kind/enhancement Category issues or prs related to enhancement.

Comments

@joshlong
Copy link

joshlong commented Nov 8, 2018

Issue Description

Type: feature request

Describe what happened (or what feature you want)

Please support a cleaner way to use SphU (I don't even know what SphU means - it's very confusing.).

I see that Sentinel uses Java 6, but will Sentinel ever support Java 7? (or 11?)

in Java 7, it would be possible to write:

try ( Entry e = SpuH.entry("my-resource")){
// do something
}

if Entry implemented Autocloseable and Autocloseable#close() called Entry#exit().

In the Spring world, we use *Template objects to achieve this kind of resource initialization and destruction. Imagine something like:

public class SentinelTemplate {

	public <T> T sync(String rn, EntryCallback<T> callback) {
		return this.sync(rn, callback, null);
	}

	public <T> T sync(String resourceName, EntryCallback<T> callback, BlockCallback<T> errorCallback) {
			Entry entry = null;
			try {
				entry = SphU.entry(resourceName);
				assert Objects.nonNull(entry) : "the entry should not be null";
				return callback.execute(entry);
			}
			catch (BlockException ex) {
				if (null != errorCallback) {
					return errorCallback.onBlock(ex);
				}
			}
			finally {
				if (null != entry) {
					entry.exit();
				}
			}
			return null;
		}
	} // SentinelTemplate


	interface BlockCallback<T> {

		T onBlock(BlockException be);
	}

	interface EntryCallback<T> {

		T execute(Entry entry);
	}

then, you could use that in consumer code somewhere:



	private final SentinelTemplate sentinelTemplate = new SentinelTemplate();

	public void handleRequest(Long customerId) {
		String result = this.sentinelTemplate.sync("read-value", new EntryCallback<String>() {
				@Override
				public String execute(Entry entry) {
					return "obtaining value @ " + Instant.now().toString() + " for customer ID# " + customerId;
				}
			},
			new BlockCallback<String>() {
				@Override
				public String onBlock(BlockException be) {
					return "oops!";
				}
			});

		// or even cleaner if you don't have any block exception handling 
		String result = this.sentinelTemplate.sync("read-value", new EntryCallback<String>() {
			@Override
			public String execute(Entry entry) {
				return "obtaining value @ " + Instant.now().toString();
			}
		});

		// or even cleaner if you are using Java 8 or later
        // (Java 8 is end-of-life in q1 2019...)
		String result = this.sentinelTemplate.sync("read-value",  
         entry -> "obtaining value @ " + Instant.now().toString() ,
         blockException -> "oops!"
        );

	}

Anything else we need to know?

thank you for open-sourcing something so cool!

@sczyh30 sczyh30 added the kind/enhancement Category issues or prs related to enhancement. label Nov 8, 2018
@sczyh30
Copy link
Member

sczyh30 commented Nov 8, 2018

Hi, thanks for your suggestions! For the magic SphU, it has a special meaning in Alibaba so we kept the original name :)

In fact we have several kinds of APIs (described here):

  • try and catch mode (try-with-resources mode later)
  • bool mode
  • annotation mode (@SentinelResource, which seems easier to use than SphU API)

It's a good idea to support JDK 1.7 try-with-resources style. Since next milestone version we are planning to drop support for JDK 1.6, so the minimum JDK version will be 1.7 and we can add support for Java 7 AutoCloseable then.

For *Template object, it seems elegant to use. I think it's necessary to support SentinelTemplate in Spring style. We'll implement this later (and make it more functional and maybe reactive) as an individual module.

These suggestions are helpful to us. Feel free to tell us if you have more advice or questions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Category issues or prs related to enhancement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants