{"action":"created","comment":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/33386744","id":33386744,"diff_hunk":"@@ -0,0 +1,226 @@\n+package io.grpc;\n+\n+import com.google.common.base.Preconditions;\n+\n+import java.io.Closeable;\n+import java.util.concurrent.Callable;\n+import java.util.concurrent.Executor;\n+import java.util.concurrent.ExecutorService;\n+import java.util.concurrent.ScheduledExecutorService;\n+\n+import javax.annotation.Nullable;\n+\n+\n+/**\n+ * --Add warning about using this too liberally--\n+ * --Add warning about using this with containers that unload Classloaders--\n+ *\n+ *\n+ */\n+// Discussion point. Using java.io.Closeable here so we can inherit AutoCloseable where its available.\n+public final class Context implements Closeable {\n+\n+  /**\n+   * The logical root context which is {@link #current()} if no other context is bound. This context\n+   * is already closed and will not retain any listeners.\n+   */\n+  public static final Context ROOT = new Context();\n+  static {\n+    ROOT.close();\n+  }\n+\n+  /**\n+   * A listener notified on context {@link #close()}.\n+   */\n+  public interface Listener {\n+    /**\n+     * @param context the newly closed context.\n+     */\n+    public void closed(Context context);\n+  }\n+\n+  /**\n+   * Create a new context with the given key value set.\n+   */\n+  public <V> Context withValue(Key<V> k1, V v1) {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  public <V1, V2> Context withValues(Key<V1> k1, V1 v1, Key<V2> k2, V2 v2) {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  public <V1, V2, V3> Context withValues(Key<V1> k1, V1 v1, Key<V2> k2, V2 v2, Key<V3> k3, V3 v3) {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  /**\n+   * Create a new context with the given listener\n+   */\n+  public <V> Context withListener(Listener listener) {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  /**\n+   * Create a new context that does not cascade closure from its parent\n+   */\n+  public <V> Context withNoCascade() {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  /**\n+   * Create a new context that cascades closure from its parent\n+   */\n+  public <V> Context withCascade() {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  /**\n+   * Return the context associated with the current thread, will never return {@code null} as\n+   * the {@link #ROOT} context is implicitly associated with all threads.\n+   */\n+  public static Context current() {\n+    return null;\n+  }\n+\n+  /**\n+   * Detach the current context from the thread and restore the context that was previously\n+   * attached to the thread as the 'current' context. It is perfectly fine to attach a\n+   * context which is already closed.\n+   *\n+   * @throws java.lang.IllegalStateException if this context is not {@link #current()}.\n+   * @return the {@link #current()} context after {@code this} has been detached.\n+   */\n+  // Discussion point: is the return useful? inconsistency with other fluent methods if we have them\n+  public Context detach() {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+\n+  /**\n+   * Attach this context to the thread and make it current, the previously current context\n+   * will be restored when detach is called.\n+   * @return\n+   */\n+  // Discussion point, make fluent?\n+  public void attach() {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+\n+  /**\n+   * Close this context and optionally provide a cause for the closure, all listeners are notified\n+   * immediately on the calling thread\n+   *\n+   * @return {@code true} if this call closed the context and notified listeners, {@code false} if\n+   *          the context was already closed.\n+   */\n+  public boolean close(@Nullable Throwable cause) {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  /**\n+   * Close the context normally.\n+   */\n+  @Override\n+  // Discussion point: Cant return boolean here and be Closeable\n+  public void close() {\n+    close(null);\n+  }\n+\n+  /**\n+   * Has this context been closed.\n+   */\n+  public boolean isClosed() {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+  /**\n+   * Return the cause of the closure or {@code null} if context closed without a cause.\n+   */\n+  @Nullable\n+  public Throwable cause() {\n+    throw new UnsupportedOperationException();\n+  }\n+\n+\n+  public Runnable wrap(final Runnable r) {\n+    return new Runnable() {\n+      @Override\n+      public void run() {\n+        attach();\n+        try {\n+          r.run();\n+        } finally {\n+          detach();\n+        }\n+      }\n+    };\n+  }\n+\n+  public <C> Callable<C> wrap(final Callable<C> c) {\n+    return new Callable<C>() {\n+      @Override\n+      public C call() throws Exception {\n+        attach();\n+        try {\n+          return c.call();\n+        } finally {\n+          detach();\n+        }\n+      }\n+    };\n+  }\n+\n+  public ExecutorService wrap(final ExecutorService e) {\n+    // TODO\n+    return null;\n+  }\n+\n+  public ScheduledExecutorService wrap(final ScheduledExecutorService e) {\n+    // TODO\n+    return null;\n+  }\n+\n+  // Discussion point, for Java 6+ would be nice to be able to wrap ForkJoinPool/Task\n+  // and some of the newer stuff in Java8 too.\n+\n+  /**\n+   * Create a {@link Key} with the given name.\n+   */\n+  public static <T> Key<T> key(String name) {\n+    return new Key<T>(name);\n+  }\n+\n+  /**\n+   * Create a {@link Key} with the given name and default value.\n+   */\n+  public static <T> Key<T> keyWithDefault(String name, T defaultValue) {\n+    return new Key<T>(name, defaultValue);\n+  }\n+\n+  /**\n+   *\n+   * @param <T>\n+   */\n+  public static class Key<T> {","path":"core/src/main/java/io/grpc/Context.java","position":206,"original_position":206,"commit_id":"20474d3e192845430b05ba27cd4c20acd2587606","original_commit_id":"20474d3e192845430b05ba27cd4c20acd2587606","user":{"login":"nmittler","id":6493296,"avatar_url":"https://avatars.githubusercontent.com/u/6493296?v=3","gravatar_id":"","url":"https://api.github.com/users/nmittler","html_url":"https://github.com/nmittler","followers_url":"https://api.github.com/users/nmittler/followers","following_url":"https://api.github.com/users/nmittler/following{/other_user}","gists_url":"https://api.github.com/users/nmittler/gists{/gist_id}","starred_url":"https://api.github.com/users/nmittler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nmittler/subscriptions","organizations_url":"https://api.github.com/users/nmittler/orgs","repos_url":"https://api.github.com/users/nmittler/repos","events_url":"https://api.github.com/users/nmittler/events{/privacy}","received_events_url":"https://api.github.com/users/nmittler/received_events","type":"User","site_admin":false},"body":"+1 `key` does seem strange here.","created_at":"2015-06-26T19:16:40Z","updated_at":"2015-06-26T19:16:40Z","html_url":"https://github.com/grpc/grpc-java/pull/562#discussion_r33386744","pull_request_url":"https://api.github.com/repos/grpc/grpc-java/pulls/562","_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/33386744"},"html":{"href":"https://github.com/grpc/grpc-java/pull/562#discussion_r33386744"},"pull_request":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/562"}}},"pull_request":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/562","id":38487483,"html_url":"https://github.com/grpc/grpc-java/pull/562","diff_url":"https://github.com/grpc/grpc-java/pull/562.diff","patch_url":"https://github.com/grpc/grpc-java/pull/562.patch","issue_url":"https://api.github.com/repos/grpc/grpc-java/issues/562","number":562,"state":"open","locked":false,"title":"Initial context API","user":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"body":"@jhump @ejona86 @nmittler \r\n\r\nThis is an initial skeleton for the proposed Context API, lets use this PR to iterate on what we want.\r\n\r\nHere are some general goals....\r\n\r\n- Provide for cascading closure (similar to cancellation in Go's Context)\r\n- Simple & small enough that its OK to digest on Android\r\n- Provides some simple type-safety for values but strictly limits keys to strings\r\n- Interaction with ThreadLocal tries not to be 'magical', anything attached must be explicitly detached. GRPC guarantees that it's code will be safe in this regard.\r\n- Easy to adapt to your IOC container of choice\r\n\r\n\r\n\r\nFixes https://github.com/grpc/grpc-java/issues/262","created_at":"2015-06-24T21:04:59Z","updated_at":"2015-06-26T19:16:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d33d06ea03a6c1bb1ad197d432d7dbbb15985076","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/grpc/grpc-java/pulls/562/commits","review_comments_url":"https://api.github.com/repos/grpc/grpc-java/pulls/562/comments","review_comment_url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/grpc/grpc-java/issues/562/comments","statuses_url":"https://api.github.com/repos/grpc/grpc-java/statuses/20474d3e192845430b05ba27cd4c20acd2587606","head":{"label":"louiscryan:ContextAPI","ref":"ContextAPI","sha":"20474d3e192845430b05ba27cd4c20acd2587606","user":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"repo":{"id":31382190,"name":"grpc-java","full_name":"louiscryan/grpc-java","owner":{"login":"louiscryan","id":9046588,"avatar_url":"https://avatars.githubusercontent.com/u/9046588?v=3","gravatar_id":"","url":"https://api.github.com/users/louiscryan","html_url":"https://github.com/louiscryan","followers_url":"https://api.github.com/users/louiscryan/followers","following_url":"https://api.github.com/users/louiscryan/following{/other_user}","gists_url":"https://api.github.com/users/louiscryan/gists{/gist_id}","starred_url":"https://api.github.com/users/louiscryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louiscryan/subscriptions","organizations_url":"https://api.github.com/users/louiscryan/orgs","repos_url":"https://api.github.com/users/louiscryan/repos","events_url":"https://api.github.com/users/louiscryan/events{/privacy}","received_events_url":"https://api.github.com/users/louiscryan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/louiscryan/grpc-java","description":"The Java gRPC implementation. HTTP/2 based RPC","fork":true,"url":"https://api.github.com/repos/louiscryan/grpc-java","forks_url":"https://api.github.com/repos/louiscryan/grpc-java/forks","keys_url":"https://api.github.com/repos/louiscryan/grpc-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/louiscryan/grpc-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/louiscryan/grpc-java/teams","hooks_url":"https://api.github.com/repos/louiscryan/grpc-java/hooks","issue_events_url":"https://api.github.com/repos/louiscryan/grpc-java/issues/events{/number}","events_url":"https://api.github.com/repos/louiscryan/grpc-java/events","assignees_url":"https://api.github.com/repos/louiscryan/grpc-java/assignees{/user}","branches_url":"https://api.github.com/repos/louiscryan/grpc-java/branches{/branch}","tags_url":"https://api.github.com/repos/louiscryan/grpc-java/tags","blobs_url":"https://api.github.com/repos/louiscryan/grpc-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/louiscryan/grpc-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/louiscryan/grpc-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/louiscryan/grpc-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/louiscryan/grpc-java/statuses/{sha}","languages_url":"https://api.github.com/repos/louiscryan/grpc-java/languages","stargazers_url":"https://api.github.com/repos/louiscryan/grpc-java/stargazers","contributors_url":"https://api.github.com/repos/louiscryan/grpc-java/contributors","subscribers_url":"https://api.github.com/repos/louiscryan/grpc-java/subscribers","subscription_url":"https://api.github.com/repos/louiscryan/grpc-java/subscription","commits_url":"https://api.github.com/repos/louiscryan/grpc-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/louiscryan/grpc-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/louiscryan/grpc-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/louiscryan/grpc-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/louiscryan/grpc-java/contents/{+path}","compare_url":"https://api.github.com/repos/louiscryan/grpc-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/louiscryan/grpc-java/merges","archive_url":"https://api.github.com/repos/louiscryan/grpc-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/louiscryan/grpc-java/downloads","issues_url":"https://api.github.com/repos/louiscryan/grpc-java/issues{/number}","pulls_url":"https://api.github.com/repos/louiscryan/grpc-java/pulls{/number}","milestones_url":"https://api.github.com/repos/louiscryan/grpc-java/milestones{/number}","notifications_url":"https://api.github.com/repos/louiscryan/grpc-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/louiscryan/grpc-java/labels{/name}","releases_url":"https://api.github.com/repos/louiscryan/grpc-java/releases{/id}","created_at":"2015-02-26T18:33:49Z","updated_at":"2015-05-01T20:01:16Z","pushed_at":"2015-06-25T17:17:12Z","git_url":"git://github.com/louiscryan/grpc-java.git","ssh_url":"git@github.com:louiscryan/grpc-java.git","clone_url":"https://github.com/louiscryan/grpc-java.git","svn_url":"https://github.com/louiscryan/grpc-java","homepage":"","size":3010,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"grpc:master","ref":"master","sha":"ae2b5bfda7c199770e4607b710fa9615d11f734e","user":{"login":"grpc","id":7802525,"avatar_url":"https://avatars.githubusercontent.com/u/7802525?v=3","gravatar_id":"","url":"https://api.github.com/users/grpc","html_url":"https://github.com/grpc","followers_url":"https://api.github.com/users/grpc/followers","following_url":"https://api.github.com/users/grpc/following{/other_user}","gists_url":"https://api.github.com/users/grpc/gists{/gist_id}","starred_url":"https://api.github.com/users/grpc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grpc/subscriptions","organizations_url":"https://api.github.com/users/grpc/orgs","repos_url":"https://api.github.com/users/grpc/repos","events_url":"https://api.github.com/users/grpc/events{/privacy}","received_events_url":"https://api.github.com/users/grpc/received_events","type":"Organization","site_admin":false},"repo":{"id":27729926,"name":"grpc-java","full_name":"grpc/grpc-java","owner":{"login":"grpc","id":7802525,"avatar_url":"https://avatars.githubusercontent.com/u/7802525?v=3","gravatar_id":"","url":"https://api.github.com/users/grpc","html_url":"https://github.com/grpc","followers_url":"https://api.github.com/users/grpc/followers","following_url":"https://api.github.com/users/grpc/following{/other_user}","gists_url":"https://api.github.com/users/grpc/gists{/gist_id}","starred_url":"https://api.github.com/users/grpc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grpc/subscriptions","organizations_url":"https://api.github.com/users/grpc/orgs","repos_url":"https://api.github.com/users/grpc/repos","events_url":"https://api.github.com/users/grpc/events{/privacy}","received_events_url":"https://api.github.com/users/grpc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/grpc/grpc-java","description":"The Java gRPC implementation. HTTP/2 based RPC","fork":false,"url":"https://api.github.com/repos/grpc/grpc-java","forks_url":"https://api.github.com/repos/grpc/grpc-java/forks","keys_url":"https://api.github.com/repos/grpc/grpc-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/grpc/grpc-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/grpc/grpc-java/teams","hooks_url":"https://api.github.com/repos/grpc/grpc-java/hooks","issue_events_url":"https://api.github.com/repos/grpc/grpc-java/issues/events{/number}","events_url":"https://api.github.com/repos/grpc/grpc-java/events","assignees_url":"https://api.github.com/repos/grpc/grpc-java/assignees{/user}","branches_url":"https://api.github.com/repos/grpc/grpc-java/branches{/branch}","tags_url":"https://api.github.com/repos/grpc/grpc-java/tags","blobs_url":"https://api.github.com/repos/grpc/grpc-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/grpc/grpc-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/grpc/grpc-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/grpc/grpc-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/grpc/grpc-java/statuses/{sha}","languages_url":"https://api.github.com/repos/grpc/grpc-java/languages","stargazers_url":"https://api.github.com/repos/grpc/grpc-java/stargazers","contributors_url":"https://api.github.com/repos/grpc/grpc-java/contributors","subscribers_url":"https://api.github.com/repos/grpc/grpc-java/subscribers","subscription_url":"https://api.github.com/repos/grpc/grpc-java/subscription","commits_url":"https://api.github.com/repos/grpc/grpc-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/grpc/grpc-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/grpc/grpc-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/grpc/grpc-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/grpc/grpc-java/contents/{+path}","compare_url":"https://api.github.com/repos/grpc/grpc-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/grpc/grpc-java/merges","archive_url":"https://api.github.com/repos/grpc/grpc-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/grpc/grpc-java/downloads","issues_url":"https://api.github.com/repos/grpc/grpc-java/issues{/number}","pulls_url":"https://api.github.com/repos/grpc/grpc-java/pulls{/number}","milestones_url":"https://api.github.com/repos/grpc/grpc-java/milestones{/number}","notifications_url":"https://api.github.com/repos/grpc/grpc-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/grpc/grpc-java/labels{/name}","releases_url":"https://api.github.com/repos/grpc/grpc-java/releases{/id}","created_at":"2014-12-08T19:00:00Z","updated_at":"2015-06-26T01:28:42Z","pushed_at":"2015-06-26T17:02:04Z","git_url":"git://github.com/grpc/grpc-java.git","ssh_url":"git@github.com:grpc/grpc-java.git","clone_url":"https://github.com/grpc/grpc-java.git","svn_url":"https://github.com/grpc/grpc-java","homepage":"","size":19060,"stargazers_count":746,"watchers_count":746,"language":"Java","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":168,"mirror_url":null,"open_issues_count":85,"forks":168,"open_issues":85,"watchers":746,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/562"},"html":{"href":"https://github.com/grpc/grpc-java/pull/562"},"issue":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/562"},"comments":{"href":"https://api.github.com/repos/grpc/grpc-java/issues/562/comments"},"review_comments":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/562/comments"},"review_comment":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/grpc/grpc-java/pulls/562/commits"},"statuses":{"href":"https://api.github.com/repos/grpc/grpc-java/statuses/20474d3e192845430b05ba27cd4c20acd2587606"}}}}