add file lock to promise one instance for a path

This commit is contained in:
siddontang 2014-09-24 15:51:09 +08:00
parent b7de9e6354
commit 0a64b592d5
2 changed files with 22 additions and 5 deletions

View File

@ -16,3 +16,4 @@ go get github.com/siddontang/go/bson
go get github.com/siddontang/go/log go get github.com/siddontang/go/log
go get github.com/siddontang/go/snappy go get github.com/siddontang/go/snappy
go get github.com/siddontang/go/num go get github.com/siddontang/go/num
go get github.com/siddontang/go/filelock

View File

@ -2,10 +2,14 @@ package ledis
import ( import (
"fmt" "fmt"
"github.com/siddontang/go/filelock"
"github.com/siddontang/go/log" "github.com/siddontang/go/log"
"github.com/siddontang/ledisdb/config" "github.com/siddontang/ledisdb/config"
"github.com/siddontang/ledisdb/rpl" "github.com/siddontang/ledisdb/rpl"
"github.com/siddontang/ledisdb/store" "github.com/siddontang/ledisdb/store"
"io"
"os"
"path"
"sync" "sync"
"time" "time"
) )
@ -31,6 +35,8 @@ type Ledis struct {
// for readonly mode, only replication can write // for readonly mode, only replication can write
readOnly bool readOnly bool
lock io.Closer
} }
func Open(cfg *config.Config) (*Ledis, error) { func Open(cfg *config.Config) (*Ledis, error) {
@ -42,18 +48,23 @@ func Open2(cfg *config.Config, flags int) (*Ledis, error) {
cfg.DataDir = config.DefaultDataDir cfg.DataDir = config.DefaultDataDir
} }
ldb, err := store.Open(cfg) os.MkdirAll(cfg.DataDir, 0755)
if err != nil {
return nil, err var err error
}
l := new(Ledis) l := new(Ledis)
if l.lock, err = filelock.Lock(path.Join(cfg.DataDir, "LOCK")); err != nil {
return nil, err
}
l.readOnly = (flags&ROnlyMode > 0) l.readOnly = (flags&ROnlyMode > 0)
l.quit = make(chan struct{}) l.quit = make(chan struct{})
l.ldb = ldb if l.ldb, err = store.Open(cfg); err != nil {
return nil, err
}
if cfg.UseReplication { if cfg.UseReplication {
if l.r, err = rpl.NewReplication(cfg); err != nil { if l.r, err = rpl.NewReplication(cfg); err != nil {
@ -91,6 +102,11 @@ func (l *Ledis) Close() {
l.r.Close() l.r.Close()
l.r = nil l.r = nil
} }
if l.lock != nil {
l.lock.Close()
l.lock = nil
}
} }
func (l *Ledis) Select(index int) (*DB, error) { func (l *Ledis) Select(index int) (*DB, error) {