Skip to content

Commit

Permalink
net: support multi/single checkout and return
Browse files Browse the repository at this point in the history
  • Loading branch information
boz committed Apr 26, 2017
1 parent 18fcf7d commit 78f8d68
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 107 deletions.
56 changes: 49 additions & 7 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,65 @@ func main() {
secs := time.Duration(rand.Intn(5)) * time.Second
time.Sleep(secs)

items, err := client.Checkout()
if err != nil {
log.WithError(err).Error("checkout")
return
var redisUrl string
var pgUrl string

if i%2 == 0 {
// multi checkout
items, err := client.CheckoutBatch()
if err != nil {
log.WithError(err).Error("checkout multi")
return
}
defer func() {
if err := client.ReturnBatch(items); err != nil {
log.WithError(err).Error("return multi")
}
}()

pgUrl = items["postgres"].Url
redisUrl = items["redis"].Url

} else {
// single checkout

rparam, err := client.Checkout("redis")
if err != nil {
log.WithError(err).Error("checkout redis")
return
}
defer func() {
if err := client.Return("redis", rparam); err != nil {
log.WithError(err).Error("return redis")
}
}()

redisUrl = rparam.Url

pgparam, err := client.Checkout("postgres")
if err != nil {
log.WithError(err).Error("checkout postgres")
return
}
defer func() {
if err := client.Return("postgres", rparam); err != nil {
log.WithError(err).Error("return postgres")
}
}()

pgUrl = pgparam.Url
}
defer client.Return(items)

// connect to redis instance
rconn, err := redis.DialURL(items["redis"].Url)
rconn, err := redis.DialURL(redisUrl)
if err != nil {
log.WithError(err).Error("dialing redis")
return
}
defer rconn.Close()

// connect to pg
pconn, err := sql.Open("postgres", items["postgres"].Url)
pconn, err := sql.Open("postgres", pgUrl)
if err != nil {
log.WithError(err).Error("dialing postgres")
return
Expand Down
29 changes: 1 addition & 28 deletions net/_testdata/config.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,14 @@
{
"pools": {
"postgres": {
"size": 2,
"image": "postgres",
"port": 5432,
"params": {
"username": "postgres",
"password": "",
"database": "postgres",
"url": "postgres://{{.Username}}:{{.Password}}@{{.Hostname}}:{{.Port}}/{{.Database}}?sslmode=disable"
},
"actions": {
"initialize": {
"type": "postgres.exec",
"query": "create table users (id serial primary key, name varchar(255), unique(name))"
},
"healthcheck": {
"type": "postgres.ping",
"retries": 20
},
"reset": {
"type": "postgres.truncate"
}
}
},
"redis": {
"size": 2,
"size": 1,
"image": "redis",
"port": 6379,
"params": {
"database": "0",
"url": "redis://{{.Hostname}}:{{.Port}}/{{.Database}}"
},
"actions": {
"initialize": {
"type": "redis.exec"
},
"healthcheck": {
"type": "redis.ping"
},
Expand Down
70 changes: 55 additions & 15 deletions net/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"

"github.com/boz/ephemerald"
"github.com/boz/ephemerald/params"
)

var (
DefaultConnectAddress = net.JoinHostPort("localhost", strconv.Itoa(DefaultPort))
)

type ClientBuilder struct {
address string
}
Expand Down Expand Up @@ -43,14 +40,14 @@ func (b *ClientBuilder) Create() (*Client, error) {
return &Client{b.address}, nil
}

func (c *Client) Checkout(names ...string) (params.Set, error) {
func (c *Client) CheckoutBatch(names ...string) (params.Set, error) {
ps := params.Set{}

req, err := http.NewRequest("PUT", c.url(rpcCheckoutName), &bytes.Buffer{})

req, err := http.NewRequest("PUT", c.url(rpcCheckoutPath), &bytes.Buffer{})
if err != nil {
return ps, err
}
req.Header.Add("Content-Type", rpcContentType)

client := &http.Client{}
resp, err := client.Do(req)
Expand All @@ -59,22 +56,43 @@ func (c *Client) Checkout(names ...string) (params.Set, error) {
}
defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&ps)
return ps, err
}

func (c *Client) Checkout(name string) (params.Params, error) {
params := params.Params{}

req, err := http.NewRequest("PUT", c.url(rpcCheckoutPath, name), &bytes.Buffer{})
if err != nil {
return ps, err
return params, err
}
req.Header.Add("Content-Type", rpcContentType)

err = json.Unmarshal(buf, &ps)
return ps, err
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return params, err
}
defer resp.Body.Close()

dec := json.NewDecoder(resp.Body)
err = dec.Decode(&params)
return params, err
}

func (c *Client) Return(ps params.Set) error {
func (c *Client) ReturnBatch(ps params.Set) error {
buf, err := json.Marshal(ps)
if err != nil {
return err
}
req, err := http.NewRequest("DELETE", c.url(rpcReturnPath), bytes.NewBuffer(buf))
if err != nil {
return err
}
req.Header.Add("Content-Type", rpcContentType)

req, err := http.NewRequest("PUT", c.url(rpcReturnName), bytes.NewBuffer(buf))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand All @@ -84,6 +102,28 @@ func (c *Client) Return(ps params.Set) error {
return nil
}

func (c *Client) url(path string) string {
func (c *Client) Return(name string, item ephemerald.Item) error {

url := c.url(rpcReturnPath, name, item.ID())

req, err := http.NewRequest("DELETE", url, &bytes.Buffer{})
if err != nil {
return err
}
req.Header.Add("Content-Type", rpcContentType)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}

func (c *Client) url(path string, parts ...string) string {
for _, part := range parts {
path = path + "/" + url.QueryEscape(part)
}
return fmt.Sprintf("http://%v%v", c.address, path)
}
10 changes: 8 additions & 2 deletions net/net.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package net

const (
rpcCheckoutName = "/checkout"
rpcReturnName = "/return"
DefaultPort = 6000
DefaultListenAddress = ":6000"
DefaultConnectAddress = "localhost:6000"

rpcCheckoutPath = "/checkout"
rpcReturnPath = "/return"

rpcContentType = "application/json"
)
53 changes: 19 additions & 34 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package net_test

import (
"context"
"database/sql"
"fmt"
"testing"

"github.com/Sirupsen/logrus"
Expand All @@ -17,7 +15,6 @@ import (
"github.com/boz/ephemerald/net"
"github.com/boz/ephemerald/params"
redigo "github.com/garyburd/redigo/redis"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -58,48 +55,36 @@ func TestClientServer(t *testing.T) {
Create()
require.NoError(t, err)

pset, err := client.Checkout()
require.NoError(t, err)
defer func() {
assert.NoError(t, client.Return(pset))
func() {
pset, err := client.CheckoutBatch()
require.NoError(t, err)
defer func() {
require.NoError(t, client.ReturnBatch(pset))
}()

rparam, ok := pset["redis"]
require.True(t, ok)
doTestOperation(t, rparam, "multi")
}()

doTestOperation(t, pset, "main")

for i := 0; i < 1; i++ {
func(count int) {
pset, err := client.Checkout()
require.NoError(t, err)
defer func() {
assert.NoError(t, client.Return(pset))
}()
doTestOperation(t, pset, fmt.Sprintf("child %v", count))
}(i)
}
func() {
rparam, err := client.Checkout("redis")
require.NoError(t, err)
defer func() {
require.NoError(t, client.Return("redis", rparam))
}()
doTestOperation(t, rparam, "single")
}()
}

func doTestOperation(t *testing.T, pset params.Set, message string) {
rparam, ok := pset["redis"]
require.True(t, ok, message)
func doTestOperation(t *testing.T, rparam params.Params, message string) {
require.NotNil(t, rparam, message)
require.NotEmpty(t, rparam.Url, message)

pgparam, ok := pset["postgres"]
require.True(t, ok, message)
require.NotNil(t, pgparam, message)
require.NotEmpty(t, pgparam.Url, message)

rdb, err := redigo.DialURL(rparam.Url)
require.NoError(t, err, message)
defer rdb.Close()

pg, err := sql.Open("postgres", pgparam.Url)
require.NoError(t, err, message)
defer pg.Close()

_, err = rdb.Do("PING")
require.NoError(t, err, message)

err = pg.Ping()
require.NoError(t, err, message)
}
Loading

0 comments on commit 78f8d68

Please sign in to comment.