Created
December 9, 2016 00:48
-
-
Save davisford/db624ebf7e07060a0ad000183b652e8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// super quick hack to test mongodb replica set. once the replica set is running | |
// attemtp rs.stepDown() or just kill the primary and observe the behavior. | |
package main | |
import ( | |
"fmt" | |
mgo "gopkg.in/mgo.v2" | |
"time" | |
) | |
type Person struct { | |
Number int | |
} | |
func main() { | |
session, err := mgo.Dial("mongodb://192.168.99.100:30001,192.168.99.100:30002,192.168.99.100:30003/test") | |
if err != nil { | |
panic(err) | |
} | |
// goroutine pings every 2 seconds and will refresh if it gets an error | |
go func() { | |
for { | |
time.Sleep(time.Duration(10 * time.Second)) | |
copiedSession := session.Clone() | |
defer copiedSession.Close() | |
if err := copiedSession.Ping(); err != nil { | |
fmt.Printf("error pinging server: %#v\n", err) | |
session.Refresh() | |
} else { | |
fmt.Println("ping") | |
} | |
} | |
}() | |
var count = 0 | |
// goroutine attempts a write every 2 seconds to see if we can recover from primary takedown | |
go func() { | |
for { | |
time.Sleep(time.Duration(1 * time.Second)) | |
count = count + 1 | |
copiedSession := session.Clone() | |
defer copiedSession.Close() | |
var person = &Person{ | |
Number: count, | |
} | |
if err := copiedSession.DB("test").C("people").Insert(person); err != nil { | |
fmt.Printf("error writing to collection: %#v\n", err) | |
} else { | |
fmt.Println(count) | |
} | |
} | |
}() | |
// never quit | |
for { | |
time.Sleep(time.Duration(1 * time.Minute)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment