Skip to content
This repository has been archived by the owner on Mar 9, 2019. It is now read-only.

concurrent writes and deadlocks #739

Open
winteraz opened this issue Jan 10, 2018 · 2 comments
Open

concurrent writes and deadlocks #739

winteraz opened this issue Jan 10, 2018 · 2 comments

Comments

@winteraz
Copy link

Only one read-write transaction is allowed at a time.

From documentation I understand that concurrent writes are not allowed. I'm not sure if this is correct thus this question to clarify: is boltdb designed only for one single writer (globally)? Am I suppose to maintain a lock on any write?
Use case:
I want to store access logs of http requests. Does this mean that I need to use a global channel to serialise the access bolt writes ?


e.g. 

var globalChan = make(chan string, 10)

func init() {
	const bucketName = "widgets"
	tx, err := db.Begin(true)
	if err != nil {
		panic(err)
	}
	_, err = tx.CreateBucketIfNotExists([]byte(bucketName))
	if err != nil {
		return err
	}
	go func() {
		for URL := range globalCHan {
			tx, err := db.Begin(true)
			if err != nil {
				panic(err)
			}
			bk := tx.Bucket([]byte(bucketName))
			// Set the value "bar" for the key "foo".
			if err := b.Put([]byte(time.Now().String()), []byte("bar")); err != nil {
				return err
			}
		}

	}()
}

func HandleFunc(w http.ResponseWriter, r *http.Request) {
    globalChan <- r.URL.String()
}

@mei-rune
Copy link

mei-rune commented Jan 18, 2018

tx.Commit() or tx.Rollback() is missing

@gigimushroom
Copy link

db uses

wlock   sync.Mutex   // Allows only one writer at a time.

You dont need a global channel, just use transaction, only one will be processing, other will block and wait

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants