{"action":"created","comment":{"url":"https://api.github.com/repos/pressly/chainstore/pulls/comments/42830895","id":42830895,"diff_hunk":"@@ -1,147 +1,231 @@\n package chainstore\n \n import (\n-\t\"errors\"\n-\t\"io/ioutil\"\n+\t\"fmt\"\n \t\"regexp\"\n+\t\"sync\"\n+\n+\t\"golang.org/x/net/context\"\n )\n \n-var (\n-\tErrInvalidKey = errors.New(\"Invalid key\")\n+type storeFn func(s Store) error\n \n-\tKeyInvalidator = regexp.MustCompile(`(i?)[^a-z0-9\\/_\\-:\\.]`)\n+var (\n+\tkeyInvalidator = regexp.MustCompile(`(i?)[^a-z0-9\\/_\\-:\\.]`)\n )\n \n const (\n-\tMaxKeyLen = 256\n+\tmaxKeyLen = 256\n )\n \n type Store interface {\n \tOpen() error\n \tClose() error\n-\tPut(key string, val []byte) error\n-\tGet(key string) ([]byte, error)\n-\tDel(key string) error\n+\tPut(ctx context.Context, key string, val []byte) error\n+\tGet(ctx context.Context, key string) ([]byte, error)\n+\tDel(ctx context.Context, key string) error\n }\n \n-// TODO: how can we check if a store has been opened...?\n+type storeWrapper struct {\n+\tStore\n+\terrE  error\n+\terrMu sync.RWMutex\n+}\n \n+func (s *storeWrapper) err() error {\n+\ts.errMu.RLock()\n+\tdefer s.errMu.RUnlock()\n+\treturn s.errE\n+}\n+\n+func (s *storeWrapper) setErr(err error) {\n+\tif err == nil {\n+\t\treturn\n+\t}\n+\ts.errMu.Lock()\n+\tdefer s.errMu.Unlock()\n+\ts.errE = err\n+}\n+\n+// Chain represents a store chain.\n type Chain struct {\n-\tstores []Store\n-\tasync  bool\n+\tstores []*storeWrapper\n }\n \n+// New creates a new store chain backed by the passed stores.\n func New(stores ...Store) Store {\n-\treturn &Chain{stores, false}\n-\t// TODO: make the chain..\n-\t// call Open(), but in case of error..?\n+\tc := &Chain{\n+\t\tstores: make([]*storeWrapper, 0, len(stores)),\n+\t}\n+\tfor _, s := range stores {\n+\t\tc.stores = append(c.stores, &storeWrapper{Store: s})\n+\t}\n+\treturn c\n }\n \n-func Async(stores ...Store) Store {\n-\treturn &Chain{stores, true}\n-}\n+// Open opens all the stores.\n+func (c *Chain) Open() error {\n \n-func (c *Chain) Open() (err error) {\n-\tfor _, s := range c.stores {\n-\t\terr = s.Open()\n-\t\tif err != nil {\n-\t\t\treturn // return first error that comes up\n-\t\t}\n+\tif err := c.firstErr(); err != nil {\n+\t\treturn fmt.Errorf(\"Open failed due to a previous error: %q\", err)\n \t}\n-\treturn\n+\n+\tvar wg sync.WaitGroup\n+\n+\tfor i := range c.stores {\n+\t\twg.Add(1)\n+\t\tgo func(wg *sync.WaitGroup, c *Chain, i int) {\n+\t\t\ts := c.stores[i]\n+\n+\t\t\ts.setErr(s.Open())\n+\n+\t\t\twg.Done()\n+\t\t}(&wg, c, i)\n+\t}\n+\n+\twg.Wait()\n+\n+\treturn c.firstErr()\n }\n \n-func (c *Chain) Close() (err error) {\n-\tfor _, s := range c.stores {\n-\t\terr = s.Close()\n-\t\t// TODO: we shouldn't stop on first error.. should keep trying to close\n-\t\t// and record errors separately\n-\t\tif err != nil {\n-\t\t\treturn\n-\t\t}\n+// Close closes all the stores.\n+func (c *Chain) Close() error {\n+\tif err := c.firstErr(); err != nil {\n+\t\treturn fmt.Errorf(\"Close failed due to a previous error: %q\", err)\n+\t}\n+\n+\tvar wg sync.WaitGroup\n+\n+\tfor i := range c.stores {\n+\t\twg.Add(1)\n+\t\tgo func(wg *sync.WaitGroup, s *storeWrapper) {\n+\t\t\ts.setErr(s.Close())\n+\t\t\twg.Done()\n+\t\t}(&wg, c.stores[i])\n \t}\n-\treturn\n+\n+\twg.Wait()\n+\n+\treturn c.firstErr()\n }\n \n-func (c *Chain) Put(key string, val []byte) (err error) {\n-\tif !IsValidKey(key) {\n+func (c *Chain) Put(ctx context.Context, key string, val []byte) (err error) {\n+\tif !isValidKey(key) {\n \t\treturn ErrInvalidKey\n \t}\n \n-\tfn := func() (err error) {\n-\t\tfor _, s := range c.stores {\n-\t\t\terr = s.Put(key, val)\n-\t\t\tif err != nil {\n-\t\t\t\treturn\n-\t\t\t}\n-\t\t}\n-\t\treturn\n+\tif err := c.firstErr(); err != nil {\n+\t\treturn fmt.Errorf(\"Open failed due to a previous error: %q\", err)\n \t}\n-\tif c.async {\n-\t\tgo fn()\n-\t} else {\n-\t\terr = fn()\n+\n+\tfn := func(s Store) error {\n+\t\treturn s.Put(ctx, key, val)\n \t}\n-\treturn\n+\n+\treturn c.doWithContext(ctx, fn)\n }\n \n-func (c *Chain) Get(key string) (val []byte, err error) {\n-\tif !IsValidKey(key) {\n+func (c *Chain) Get(ctx context.Context, key string) (val []byte, err error) {\n+\tif !isValidKey(key) {\n \t\treturn nil, ErrInvalidKey\n \t}\n \n-\tfor i, s := range c.stores {\n-\t\tval, err = s.Get(key)\n-\t\tif err != nil {\n-\t\t\treturn\n-\t\t}\n+\tif err := c.firstErr(); err != nil {\n+\t\treturn nil, fmt.Errorf(\"Open failed due to a previous error: %q\", err)\n+\t}\n \n-\t\tif len(val) > 0 {\n-\t\t\tif i > 0 {\n-\t\t\t\t// put the value in all other stores up the chain\n-\t\t\t\tfn := func() {\n-\t\t\t\t\tfor n := i - 1; n >= 0; n-- {\n-\t\t\t\t\t\tc.stores[n].Put(key, val) // errors..?\n-\t\t\t\t\t}\n-\t\t\t\t}\n-\t\t\t\t// if c.async { } else { } ....?\n-\t\t\t\tgo fn()\n+\terrCh := make(chan error, 1)\n+\n+\tgo func() {\n+\t\tfor i := range c.stores {\n+\t\t\ts := c.stores[i]\n+\n+\t\t\tval, err = s.Get(ctx, key)\n+\t\t\tif err != nil {\n+\t\t\t\terrCh <- err\n+\t\t\t\treturn\n \t\t\t}\n \n-\t\t\t// return the first value found on the chain\n-\t\t\treturn\n+\t\t\tif val != nil {\n+\t\t\t\tfor j := i - 1; j >= 0; j++ {\n+\t\t\t\t\tgo c.stores[i].Put(ctx, key, val)\n+\t\t\t\t}\n+\t\t\t\terrCh <- nil\n+\t\t\t\treturn\n+\t\t\t}\n \t\t}\n+\t\terrCh <- ErrNoSuchKey\n+\t}()\n+\n+\tselect {\n+\tcase <-ctx.Done():\n+\t\tc.Close() // Close should unlock pending requests.","path":"chainstore.go","position":null,"original_position":230,"commit_id":"5699eaf99214de20a2afa958a55f4c3d5021be8e","original_commit_id":"a42cbf8e12b5564d32fb9e9036d8029d4599b875","user":{"login":"xiam","id":385670,"avatar_url":"https://avatars.githubusercontent.com/u/385670?v=3","gravatar_id":"","url":"https://api.github.com/users/xiam","html_url":"https://github.com/xiam","followers_url":"https://api.github.com/users/xiam/followers","following_url":"https://api.github.com/users/xiam/following{/other_user}","gists_url":"https://api.github.com/users/xiam/gists{/gist_id}","starred_url":"https://api.github.com/users/xiam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xiam/subscriptions","organizations_url":"https://api.github.com/users/xiam/orgs","repos_url":"https://api.github.com/users/xiam/repos","events_url":"https://api.github.com/users/xiam/events{/privacy}","received_events_url":"https://api.github.com/users/xiam/received_events","type":"User","site_admin":false},"body":"I rewrote this part using @VojtechVitek's suggestion of a default timeout, I think it's better now. I also added a test for that one in `mockstore`. How does it look?","created_at":"2015-10-23T03:42:54Z","updated_at":"2015-10-23T03:42:54Z","html_url":"https://github.com/pressly/chainstore/pull/9#discussion_r42830895","pull_request_url":"https://api.github.com/repos/pressly/chainstore/pulls/9","_links":{"self":{"href":"https://api.github.com/repos/pressly/chainstore/pulls/comments/42830895"},"html":{"href":"https://github.com/pressly/chainstore/pull/9#discussion_r42830895"},"pull_request":{"href":"https://api.github.com/repos/pressly/chainstore/pulls/9"}}},"pull_request":{"url":"https://api.github.com/repos/pressly/chainstore/pulls/9","id":48069886,"html_url":"https://github.com/pressly/chainstore/pull/9","diff_url":"https://github.com/pressly/chainstore/pull/9.diff","patch_url":"https://github.com/pressly/chainstore/pull/9.patch","issue_url":"https://api.github.com/repos/pressly/chainstore/issues/9","number":9,"state":"open","locked":false,"title":"Adding support for context.Context","user":{"login":"xiam","id":385670,"avatar_url":"https://avatars.githubusercontent.com/u/385670?v=3","gravatar_id":"","url":"https://api.github.com/users/xiam","html_url":"https://github.com/xiam","followers_url":"https://api.github.com/users/xiam/followers","following_url":"https://api.github.com/users/xiam/following{/other_user}","gists_url":"https://api.github.com/users/xiam/gists{/gist_id}","starred_url":"https://api.github.com/users/xiam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xiam/subscriptions","organizations_url":"https://api.github.com/users/xiam/orgs","repos_url":"https://api.github.com/users/xiam/repos","events_url":"https://api.github.com/users/xiam/events{/privacy}","received_events_url":"https://api.github.com/users/xiam/received_events","type":"User","site_admin":false},"body":"Hey @pkieltyka,\r\n\r\nThis work in progress features code changes for chainstore and a new `mockstore` store that I'm using to test the chainstore with `context.Context`. The intention of this PR is to review the code changes in chainstore and see if we can start updating the other stores to reflect changes in the interface.\r\n\r\nSee: https://github.com/pressly/chainstore/issues/8","created_at":"2015-10-19T13:16:28Z","updated_at":"2015-10-23T03:42:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"665ef608d6cc5e0b84496a576ef7c8cc5bc8e07f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/pressly/chainstore/pulls/9/commits","review_comments_url":"https://api.github.com/repos/pressly/chainstore/pulls/9/comments","review_comment_url":"https://api.github.com/repos/pressly/chainstore/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pressly/chainstore/issues/9/comments","statuses_url":"https://api.github.com/repos/pressly/chainstore/statuses/5699eaf99214de20a2afa958a55f4c3d5021be8e","head":{"label":"pressly:issue-8","ref":"issue-8","sha":"5699eaf99214de20a2afa958a55f4c3d5021be8e","user":{"login":"pressly","id":18835,"avatar_url":"https://avatars.githubusercontent.com/u/18835?v=3","gravatar_id":"","url":"https://api.github.com/users/pressly","html_url":"https://github.com/pressly","followers_url":"https://api.github.com/users/pressly/followers","following_url":"https://api.github.com/users/pressly/following{/other_user}","gists_url":"https://api.github.com/users/pressly/gists{/gist_id}","starred_url":"https://api.github.com/users/pressly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pressly/subscriptions","organizations_url":"https://api.github.com/users/pressly/orgs","repos_url":"https://api.github.com/users/pressly/repos","events_url":"https://api.github.com/users/pressly/events{/privacy}","received_events_url":"https://api.github.com/users/pressly/received_events","type":"Organization","site_admin":false},"repo":{"id":19953633,"name":"chainstore","full_name":"pressly/chainstore","owner":{"login":"pressly","id":18835,"avatar_url":"https://avatars.githubusercontent.com/u/18835?v=3","gravatar_id":"","url":"https://api.github.com/users/pressly","html_url":"https://github.com/pressly","followers_url":"https://api.github.com/users/pressly/followers","following_url":"https://api.github.com/users/pressly/following{/other_user}","gists_url":"https://api.github.com/users/pressly/gists{/gist_id}","starred_url":"https://api.github.com/users/pressly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pressly/subscriptions","organizations_url":"https://api.github.com/users/pressly/orgs","repos_url":"https://api.github.com/users/pressly/repos","events_url":"https://api.github.com/users/pressly/events{/privacy}","received_events_url":"https://api.github.com/users/pressly/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pressly/chainstore","description":"Lightweight key-value interface to a bunch of storage engines with middleware support, organized as a chain of operations; written in Go","fork":false,"url":"https://api.github.com/repos/pressly/chainstore","forks_url":"https://api.github.com/repos/pressly/chainstore/forks","keys_url":"https://api.github.com/repos/pressly/chainstore/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pressly/chainstore/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pressly/chainstore/teams","hooks_url":"https://api.github.com/repos/pressly/chainstore/hooks","issue_events_url":"https://api.github.com/repos/pressly/chainstore/issues/events{/number}","events_url":"https://api.github.com/repos/pressly/chainstore/events","assignees_url":"https://api.github.com/repos/pressly/chainstore/assignees{/user}","branches_url":"https://api.github.com/repos/pressly/chainstore/branches{/branch}","tags_url":"https://api.github.com/repos/pressly/chainstore/tags","blobs_url":"https://api.github.com/repos/pressly/chainstore/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pressly/chainstore/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pressly/chainstore/git/refs{/sha}","trees_url":"https://api.github.com/repos/pressly/chainstore/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pressly/chainstore/statuses/{sha}","languages_url":"https://api.github.com/repos/pressly/chainstore/languages","stargazers_url":"https://api.github.com/repos/pressly/chainstore/stargazers","contributors_url":"https://api.github.com/repos/pressly/chainstore/contributors","subscribers_url":"https://api.github.com/repos/pressly/chainstore/subscribers","subscription_url":"https://api.github.com/repos/pressly/chainstore/subscription","commits_url":"https://api.github.com/repos/pressly/chainstore/commits{/sha}","git_commits_url":"https://api.github.com/repos/pressly/chainstore/git/commits{/sha}","comments_url":"https://api.github.com/repos/pressly/chainstore/comments{/number}","issue_comment_url":"https://api.github.com/repos/pressly/chainstore/issues/comments{/number}","contents_url":"https://api.github.com/repos/pressly/chainstore/contents/{+path}","compare_url":"https://api.github.com/repos/pressly/chainstore/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pressly/chainstore/merges","archive_url":"https://api.github.com/repos/pressly/chainstore/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pressly/chainstore/downloads","issues_url":"https://api.github.com/repos/pressly/chainstore/issues{/number}","pulls_url":"https://api.github.com/repos/pressly/chainstore/pulls{/number}","milestones_url":"https://api.github.com/repos/pressly/chainstore/milestones{/number}","notifications_url":"https://api.github.com/repos/pressly/chainstore/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pressly/chainstore/labels{/name}","releases_url":"https://api.github.com/repos/pressly/chainstore/releases{/id}","created_at":"2014-05-19T18:53:03Z","updated_at":"2015-10-17T15:19:05Z","pushed_at":"2015-10-23T03:30:17Z","git_url":"git://github.com/pressly/chainstore.git","ssh_url":"git@github.com:pressly/chainstore.git","clone_url":"https://github.com/pressly/chainstore.git","svn_url":"https://github.com/pressly/chainstore","homepage":"","size":259,"stargazers_count":64,"watchers_count":64,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":8,"forks":3,"open_issues":8,"watchers":64,"default_branch":"master"}},"base":{"label":"pressly:master","ref":"master","sha":"907f0f81699a835e86a81f11a3d3cd4d7215c360","user":{"login":"pressly","id":18835,"avatar_url":"https://avatars.githubusercontent.com/u/18835?v=3","gravatar_id":"","url":"https://api.github.com/users/pressly","html_url":"https://github.com/pressly","followers_url":"https://api.github.com/users/pressly/followers","following_url":"https://api.github.com/users/pressly/following{/other_user}","gists_url":"https://api.github.com/users/pressly/gists{/gist_id}","starred_url":"https://api.github.com/users/pressly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pressly/subscriptions","organizations_url":"https://api.github.com/users/pressly/orgs","repos_url":"https://api.github.com/users/pressly/repos","events_url":"https://api.github.com/users/pressly/events{/privacy}","received_events_url":"https://api.github.com/users/pressly/received_events","type":"Organization","site_admin":false},"repo":{"id":19953633,"name":"chainstore","full_name":"pressly/chainstore","owner":{"login":"pressly","id":18835,"avatar_url":"https://avatars.githubusercontent.com/u/18835?v=3","gravatar_id":"","url":"https://api.github.com/users/pressly","html_url":"https://github.com/pressly","followers_url":"https://api.github.com/users/pressly/followers","following_url":"https://api.github.com/users/pressly/following{/other_user}","gists_url":"https://api.github.com/users/pressly/gists{/gist_id}","starred_url":"https://api.github.com/users/pressly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pressly/subscriptions","organizations_url":"https://api.github.com/users/pressly/orgs","repos_url":"https://api.github.com/users/pressly/repos","events_url":"https://api.github.com/users/pressly/events{/privacy}","received_events_url":"https://api.github.com/users/pressly/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pressly/chainstore","description":"Lightweight key-value interface to a bunch of storage engines with middleware support, organized as a chain of operations; written in Go","fork":false,"url":"https://api.github.com/repos/pressly/chainstore","forks_url":"https://api.github.com/repos/pressly/chainstore/forks","keys_url":"https://api.github.com/repos/pressly/chainstore/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pressly/chainstore/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pressly/chainstore/teams","hooks_url":"https://api.github.com/repos/pressly/chainstore/hooks","issue_events_url":"https://api.github.com/repos/pressly/chainstore/issues/events{/number}","events_url":"https://api.github.com/repos/pressly/chainstore/events","assignees_url":"https://api.github.com/repos/pressly/chainstore/assignees{/user}","branches_url":"https://api.github.com/repos/pressly/chainstore/branches{/branch}","tags_url":"https://api.github.com/repos/pressly/chainstore/tags","blobs_url":"https://api.github.com/repos/pressly/chainstore/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pressly/chainstore/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pressly/chainstore/git/refs{/sha}","trees_url":"https://api.github.com/repos/pressly/chainstore/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pressly/chainstore/statuses/{sha}","languages_url":"https://api.github.com/repos/pressly/chainstore/languages","stargazers_url":"https://api.github.com/repos/pressly/chainstore/stargazers","contributors_url":"https://api.github.com/repos/pressly/chainstore/contributors","subscribers_url":"https://api.github.com/repos/pressly/chainstore/subscribers","subscription_url":"https://api.github.com/repos/pressly/chainstore/subscription","commits_url":"https://api.github.com/repos/pressly/chainstore/commits{/sha}","git_commits_url":"https://api.github.com/repos/pressly/chainstore/git/commits{/sha}","comments_url":"https://api.github.com/repos/pressly/chainstore/comments{/number}","issue_comment_url":"https://api.github.com/repos/pressly/chainstore/issues/comments{/number}","contents_url":"https://api.github.com/repos/pressly/chainstore/contents/{+path}","compare_url":"https://api.github.com/repos/pressly/chainstore/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pressly/chainstore/merges","archive_url":"https://api.github.com/repos/pressly/chainstore/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pressly/chainstore/downloads","issues_url":"https://api.github.com/repos/pressly/chainstore/issues{/number}","pulls_url":"https://api.github.com/repos/pressly/chainstore/pulls{/number}","milestones_url":"https://api.github.com/repos/pressly/chainstore/milestones{/number}","notifications_url":"https://api.github.com/repos/pressly/chainstore/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pressly/chainstore/labels{/name}","releases_url":"https://api.github.com/repos/pressly/chainstore/releases{/id}","created_at":"2014-05-19T18:53:03Z","updated_at":"2015-10-17T15:19:05Z","pushed_at":"2015-10-23T03:30:17Z","git_url":"git://github.com/pressly/chainstore.git","ssh_url":"git@github.com:pressly/chainstore.git","clone_url":"https://github.com/pressly/chainstore.git","svn_url":"https://github.com/pressly/chainstore","homepage":"","size":259,"stargazers_count":64,"watchers_count":64,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":8,"forks":3,"open_issues":8,"watchers":64,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pressly/chainstore/pulls/9"},"html":{"href":"https://github.com/pressly/chainstore/pull/9"},"issue":{"href":"https://api.github.com/repos/pressly/chainstore/issues/9"},"comments":{"href":"https://api.github.com/repos/pressly/chainstore/issues/9/comments"},"review_comments":{"href":"https://api.github.com/repos/pressly/chainstore/pulls/9/comments"},"review_comment":{"href":"https://api.github.com/repos/pressly/chainstore/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pressly/chainstore/pulls/9/commits"},"statuses":{"href":"https://api.github.com/repos/pressly/chainstore/statuses/5699eaf99214de20a2afa958a55f4c3d5021be8e"}}}}