{"action":"created","comment":{"url":"https://api.github.com/repos/grpc/grpc-java/pulls/comments/34395223","id":34395223,"diff_hunk":"@@ -0,0 +1,496 @@\n+package io.grpc;\n+\n+import com.google.common.base.Preconditions;\n+import com.google.common.util.concurrent.MoreExecutors;\n+\n+import java.io.Closeable;\n+import java.io.IOException;\n+import java.util.ArrayDeque;\n+import java.util.ArrayList;\n+import java.util.concurrent.Callable;\n+import java.util.concurrent.Executor;\n+import java.util.concurrent.ExecutorService;\n+import java.util.concurrent.Executors;\n+import java.util.concurrent.ScheduledExecutorService;\n+import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;\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+ * -- Add note that in the case of cascading closure that listeners to the parent are always\n+ * notified before listeners on the children.\n+ *\n+ */\n+public class Context {\n+\n+  /**\n+   * Stack of context objects which is used to record attach & detach history on a thread.\n+   */\n+  private static final ThreadLocal<ArrayDeque<Context>> contextStack =\n+      new ThreadLocal<ArrayDeque<Context>>() {\n+    @Override\n+    protected ArrayDeque<Context> initialValue() {\n+      return new ArrayDeque<Context>();\n+    }\n+  };\n+\n+  private static final Object[][] EMPTY_ENTRIES = new Object[0][2];\n+\n+  /**\n+   * The logical root context which is {@link #current()} if no other context is bound. This context\n+   * is not cancellable and so will not cascade cancellation or retain listeners.\n+   */\n+  public static final Context ROOT = new Context(null);\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+   * 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+   * <p>Will never return {@link CancellableContext} even if one is attached, instead a\n+   * {@link Context} is returned with the same properties and lifetime. This is to avoid\n+   * code stealing the ability to cancel arbitrarily.\n+   */\n+  public static Context current() {\n+    ArrayDeque<Context> stack = contextStack.get();\n+    if (stack.isEmpty()) {\n+      return ROOT;\n+    }\n+    return stack.peekLast();\n+  }\n+\n+  private final Context parent;\n+  private final Object[][] keyValueEntries;\n+  private final ArrayList<ExecutableListener> listeners;\n+  private final boolean cascadesCancellation;\n+\n+  /**\n+   * Construct a context that cannot be cancelled and will not cascade cancellation from its parent.\n+   */\n+  private Context(Context parent) {\n+    this.parent = parent;\n+    keyValueEntries = EMPTY_ENTRIES;\n+    listeners = null;\n+    cascadesCancellation = false;\n+  }\n+\n+  /**\n+   * Construct a context that cannot be cancelled but will cascade cancellation from its parent if\n+   * it is cancellable.\n+   */\n+  private Context(Context parent, Object[][] keyValueEntries) {\n+    this.parent = parent;\n+    this.keyValueEntries = keyValueEntries;\n+    cascadesCancellation = true;\n+    listeners = new ArrayList<ExecutableListener>();\n+    if (parent.canBeCancelled()) {\n+      parent.addListener(new CancellationListener() {\n+        @Override\n+        public void cancelled(Context context) {\n+          notifyAndClearListeners();\n+        }\n+      }, MoreExecutors.directExecutor());\n+    }\n+  }\n+\n+  protected boolean canBeCancelled() {\n+    // A context is cancellable if it cascades from its parent and its parent is\n+    // cancellable.\n+    return (cascadesCancellation && this.parent != null && this.parent.canBeCancelled());\n+  }\n+\n+  /**\n+   * Create a new context which is independently cancellable, the new context will cascade\n+   * cancellation from its parent.\n+   */\n+  public CancellableContext withCancellation() {\n+    return new CancellableContext(this);\n+  }\n+\n+  /**\n+   * Create a new context which cannot be cancelled and will not propagate the cancellation of its\n+   * parent.\n+   */\n+  public Context withoutCancellation() {\n+    return new Context(this);\n+  }\n+\n+  /**\n+   * Create a new context with the given key value set. The new context will cascade cancellation\n+   * from its parent.\n+   */\n+  public <V> Context withValue(Key<V> k1, V v1) {\n+    return new Context(this, new Object[][]{{k1, v1}});\n+  }\n+\n+  /**\n+   * Create a new context with the given key value set. The new context will cascade cancellation\n+   * from its parent.\n+   */\n+  public <V1, V2> Context withValues(Key<V1> k1, V1 v1, Key<V2> k2, V2 v2) {\n+    return new Context(this, new Object[][]{{k1, v1}, {k2, v2}});\n+  }\n+\n+  /**\n+   * Create a new context with the given key value set. The new context will cascade cancellation\n+   * from its parent.\n+   */\n+  public <V1, V2, V3> Context withValues(Key<V1> k1, V1 v1, Key<V2> k2, V2 v2, Key<V3> k3, V3 v3) {\n+    return new Context(this, new Object[][]{{k1, v1}, {k2, v2}, {k3, v3}});\n+  }\n+\n+  /**\n+   * Attach this context to the thread and make it {@link #current}, the previously current context\n+   * will be restored when detach is called. It is allowed to attach contexts where\n+   * {@link #isCancelled()} is {@code true}.\n+   */\n+  public void attach() {\n+    contextStack.get().addLast(this);\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.\n+   *\n+   * @throws java.lang.IllegalStateException if this context is not {@link #current()}.\n+   */\n+  public void detach() {\n+    ArrayDeque<Context> stack = contextStack.get();\n+    if (stack.isEmpty() && this == ROOT) {\n+      // trying to detach root so just ignore.\n+      return;\n+    }\n+    if (stack.peekLast() != this) {\n+      throw new IllegalStateException(\"Cannot detach a context that is not current\");\n+    }\n+    stack.removeLast();\n+  }\n+\n+  /**\n+   * Is this context cancelled.\n+   */\n+  public boolean isCancelled() {\n+    if (parent == null || !cascadesCancellation) {\n+      return false;\n+    } else {\n+      return parent.isCancelled();\n+    }\n+  }\n+\n+  /**\n+   * Return the cause of the cancellation or {@code null} if context was cancelled without a cause.\n+   */\n+  @Nullable\n+  public Throwable cause() {\n+    if (parent == null || !cascadesCancellation) {\n+      return null;\n+    } else {\n+      return parent.cause();\n+    }\n+  }\n+\n+\n+  /**\n+   * Add a listener that will be notified when the context becomes cancelled.\n+   */\n+  public void addListener(final CancellationListener cancellationListener,\n+                          final Executor executor) {\n+    Preconditions.checkNotNull(cancellationListener);\n+    Preconditions.checkNotNull(executor);\n+    if (canBeCancelled()) {","path":"core/src/main/java/io/grpc/Context.java","position":276,"original_position":216,"commit_id":"6640976f92f48f9cade35b291cfccf7d04213ab5","original_commit_id":"e508cea11e2aa3fe5def67c9e0842bdd52efc6ca","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":"I've adjusted the tests to assert this expectation","created_at":"2015-07-10T20:53:30Z","updated_at":"2015-07-10T20:53:30Z","html_url":"https://github.com/grpc/grpc-java/pull/562#discussion_r34395223","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/34395223"},"html":{"href":"https://github.com/grpc/grpc-java/pull/562#discussion_r34395223"},"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-07-10T20:53:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"62d4c77572ce7465acb80c9ba032f4a2309fccb8","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/6640976f92f48f9cade35b291cfccf7d04213ab5","head":{"label":"louiscryan:ContextAPI","ref":"ContextAPI","sha":"6640976f92f48f9cade35b291cfccf7d04213ab5","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-07-10T19:33:03Z","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":3164,"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":"652abe2f63fa6a797221cea48000ec3e662fa1e0","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-07-09T14:56:27Z","pushed_at":"2015-07-10T19:33: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":19272,"stargazers_count":774,"watchers_count":774,"language":"Java","has_issues":true,"has_downloads":false,"has_wiki":false,"has_pages":false,"forks_count":175,"mirror_url":null,"open_issues_count":88,"forks":175,"open_issues":88,"watchers":774,"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/6640976f92f48f9cade35b291cfccf7d04213ab5"}}}}