Example #1
0
func NewDeviceSessionLocker(sid string) Locker {
	locker, err := redsync.NewMutex(fmt.Sprintf(RedisSessionDeviceLocker, sid), RedixAddrPool)
	if err != nil {
		panic(err)
	}
	return locker
}
Example #2
0
func getRedlock(name string, addresses string, ttl int, tries int, delay int) *redsync.Mutex {
	m, err := redsync.NewMutex(name, parseAddresses(addresses))
	if err != nil {
		fatal(err)
	}
	m.Delay = time.Duration(delay) * time.Second
	m.Tries = tries
	m.Expiry = time.Duration(ttl) * time.Second
	return m
}
Example #3
0
func TestMutex(t *testing.T) {
	done := make(chan bool)
	chErr := make(chan error)

	for i := 0; i < 4; i++ {
		go func() {
			m, err := redsync.NewMutex("RedsyncMutex", addrs)
			if err != nil {
				chErr <- err
				return
			}

			f := 0
			for j := 0; j < 32; j++ {
				err := m.Lock()
				if err == redsync.ErrFailed {
					f++
					if f > 2 {
						chErr <- err
						return
					}
					continue
				}
				if err != nil {
					chErr <- err
					return
				}

				time.Sleep(1 * time.Millisecond)

				m.Unlock()

				time.Sleep(time.Duration(rand.Int31n(128)) * time.Millisecond)
			}
			done <- true
		}()
	}
	for i := 0; i < 4; i++ {
		select {
		case <-done:
		case err := <-chErr:
			t.Fatal(err)
		}
	}
}
Example #4
0
func ExampleMutex() {
	m, err := redsync.NewMutex("FlyingSquirrels", []net.Addr{
		&net.TCPAddr{Port: 63790},
		&net.TCPAddr{Port: 63791},
		&net.TCPAddr{Port: 63792},
		&net.TCPAddr{Port: 63793},
	})
	if err != nil {
		panic(err)
	}

	err = m.Lock()
	if err != nil {
		panic(err)
	}
	defer m.Unlock()

	// Output:
}