Skip to content
This repository has been archived by the owner on Nov 23, 2018. It is now read-only.

Adding support for context.Context #9

Merged
merged 13 commits into from
Oct 26, 2015
Merged

Adding support for context.Context #9

merged 13 commits into from
Oct 26, 2015

Conversation

xiam
Copy link
Contributor

@xiam xiam commented Oct 19, 2015

Hey @pkieltyka,

This 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.

See: #8

}

// TODO: how can we check if a store has been opened...?
type storeWrapper struct {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a "opened" property here, if required.

for n := i - 1; n >= 0; n-- {
c.stores[n].Put(key, val) // errors..?
}
nextStore := make(chan Store, len(c.stores))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @VojtechVitek,

I had to modify your snippet a bit, what do you think of the new location of the range over putBack? Otherwise it just stays blocked waiting for a value to arrive.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did it block because the channel was unbuffered?
putBack := make(chan Store, len(c.stores)) shouldn't block, I can't imagine how.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking that back ... obviously not, let me see the code again :) I was throwing it of top of my head. But this looks good already :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I have a working snippet for you... without firstVal channel and the extra goroutine:

package main

import (
    "errors"
    "fmt"
    "time"

    "golang.org/x/net/context"
)

func Get(ctx context.Context) (string, error) {
    nextStore := make(chan string, 3)
    for _, store := range []string{"first", "second", "third"} {
        nextStore <- store
    }
    close(nextStore)

    putBack := make(chan string, 3)

    for {
        select {
        case <-ctx.Done():
            return "", ctx.Err()
        case store, ok := <-nextStore:
            if !ok {
                return "", errors.New("not found")
            }

            if store != "fourth" { // change this --- simulating what store returns FOUND
                putBack <- store
                break
            }
            close(putBack)

            for store := range putBack {
                go fmt.Printf("putting back to %v store\n", store)
            }

            return fmt.Sprintf("found in %v", store), nil
        }
    }

    panic("unreachable")
}

func main() {
    ctx, _ := context.WithTimeout(context.Background(), time.Second)
    val, err := Get(ctx)
    fmt.Println(val, err)

    // wait for fmt.Printf() from goroutines
    time.Sleep(time.Second)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! makes sense, the close(putBack) is what makes the range not to lock. I like it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried closing the channel and everything worked fine :).

@xiam
Copy link
Contributor Author

xiam commented Oct 24, 2015

All right, I also updated all the other stores, their tests and the example. Now everything is working as before.

@VojtechVitek
Copy link
Collaborator

LGTM :)

@xiam can you add document changing the .Get() and .Put() API in the project README and reference the last commit/tag of the old API?

pkieltyka pushed a commit that referenced this pull request Oct 26, 2015
Adding support for context.Context
@pkieltyka pkieltyka merged commit 9001c39 into master Oct 26, 2015
@pkieltyka pkieltyka deleted the issue-8 branch October 26, 2015 14:56
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants