This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
117 lines (104 loc) · 2.18 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package dynamodbtest
import (
"bufio"
"errors"
"fmt"
"log"
"net"
"os"
"os/exec"
"strings"
"sync/atomic"
"time"
)
var (
ConnectTimeout = 10 * time.Second
ErrConnectTimeout = errors.New("[dynamodbtest] timeout starting server")
ErrGopath = errors.New("[dynamodbtest] GOPATH must be set")
)
// LogOutput must be set before calling New()
var LogOutput bool
// DB represents a DynamoDB Local process
type DB struct {
addr string
cmd *exec.Cmd
}
// New returns a started DynamoDB local instance
func New() (*DB, error) {
port := newPort()
addr := fmt.Sprintf("localhost:%d", port)
// if $GOPATH is composed of multiple paths, use the first one (fix for godep)
gopath := strings.Split(os.Getenv("GOPATH"), ":")[0]
if gopath == "" {
return nil, ErrGopath
}
path := gopath + "/src/github.com/groupme/dynamodbtest/"
db := &DB{
addr: addr,
cmd: exec.Command(
"java",
"-Djava.library.path="+path+"DynamoDbLocal_lib",
"-jar",
path+"DynamoDBLocal.jar",
"-port",
fmt.Sprintf("%d", port),
"-inMemory",
),
}
// log output
if LogOutput {
cmdReader, err := db.cmd.StderrPipe()
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
log.Printf("[dynamodbtest:%d] %s\n", port, scanner.Text())
}
}()
}
// start command
err := db.cmd.Start()
if err != nil {
return nil, err
}
// try to connect
connected := make(chan bool)
go func() {
// periodically check if connectable
ticker := time.NewTicker(time.Millisecond * 10)
for _ = range ticker.C {
c, err := net.Dial("tcp", db.addr)
if c != nil {
c.Close()
}
if err == nil {
connected <- true
return
}
}
}()
select {
case <-connected:
return db, nil
case <-time.After(ConnectTimeout):
db.Close()
return nil, ErrConnectTimeout
}
}
func (db *DB) Close() error {
err := db.cmd.Process.Kill()
fmt.Println("Killing dynamodb pid=", db.cmd.Process.Pid, " err=", err)
_, err = db.cmd.Process.Wait()
return err
}
func (db *DB) URL() string {
return fmt.Sprint("http://", db.addr)
}
var portCount int64
func newPort() int {
port := 8000 + portCount
atomic.AddInt64(&portCount, 1)
return int(port)
}