Beispiel #1
0
func (c *Client) lock(keys []string, wait, release time.Duration) (err error) {
	if c.tcpConn == nil {
		if err = c.Connect(); err != nil {
			return err
		}
	}

	request := &dlock.Request{
		Type: dlock.RequestType_Lock.Enum(),
		Lock: &dlock.RequestLock{
			Keys:         keys,
			WaitMicro:    new(uint64),
			ReleaseMicro: new(uint64),
		},
	}
	*request.Lock.WaitMicro = uint64(wait / time.Microsecond)
	*request.Lock.ReleaseMicro = uint64(release / time.Microsecond)
	if err = dlock.SendMessage(c.w, request); err != nil {
		return err
	}
	if err = c.w.Flush(); err != nil {
		return err
	}

	response := &dlock.Response{}
	if err = dlock.ReadMessage(c.r, response, c.ConfigMaxMessage); err != nil {
		return err
	}
	if response.GetStatus() != dlock.ResponseStatus_Ok {
		return errors.New(fmt.Sprintf("Remote error locking keys %v: %s %s failed keys: %v",
			keys, response.GetStatus().String(), response.GetErrorText(), response.Keys))
	}

	return nil
}
Beispiel #2
0
func (c *Client) Ping() (err error) {
	defer c.profileTime("Client.Ping", time.Now())
	if c.tcpConn == nil {
		if err = c.Connect(); err != nil {
			return err
		}
	}

	request := &dlock.Request{
		Type: dlock.RequestType_Ping.Enum(),
	}
	if err = dlock.SendMessage(c.w, request); err != nil {
		return err
	}
	if err = c.w.Flush(); err != nil {
		return err
	}

	response := &dlock.Response{}
	if err = dlock.ReadMessage(c.r, response, c.ConfigMaxMessage); err != nil {
		return err
	}
	if response.GetStatus() != dlock.ResponseStatus_Ok {
		return errors.New(fmt.Sprintf("Ping: Remote error: %s %s",
			response.GetStatus().String(), response.GetErrorText()))
	}

	return nil
}
Beispiel #3
0
func TestFunctionalLock01(t *testing.T) {
	server := initTestServer(t, 10*time.Millisecond)

	conn1, err := net.Dial("tcp", server.listeners[0].Addr().String())
	assertNil(err)
	err = conn1.SetDeadline(time.Now().Add(100 * time.Millisecond))
	assertNil(err)

	request1 := &dlock.Request{
		Type: dlock.RequestType_Lock.Enum(),
		Lock: &dlock.RequestLock{
			Keys: []string{"q"},
		},
	}
	err = dlock.SendMessage(conn1, request1)
	assertNil(err)

	response1 := &dlock.Response{}
	err = dlock.ReadMessage(conn1, response1, server.ConfigMaxMessage)
	assertNil(err)
	if response1.GetStatus() != dlock.ResponseStatus_Ok {
		t.Fatal("Status != Ok:", response1.GetStatus().String())
	}

	conn2, err := net.Dial("tcp", server.listeners[0].Addr().String())
	assertNil(err)
	err = conn2.SetDeadline(time.Now().Add(100 * time.Millisecond))
	assertNil(err)

	request2 := &dlock.Request{
		Type: dlock.RequestType_Lock.Enum(),
		Lock: &dlock.RequestLock{
			Keys: []string{"q"},
		},
	}
	err = dlock.SendMessage(conn2, request2)
	assertNil(err)

	conn1.Close()

	response2 := &dlock.Response{}
	err = dlock.ReadMessage(conn2, response2, server.ConfigMaxMessage)
	assertNil(err)
	if response1.GetStatus() != dlock.ResponseStatus_Ok {
		t.Fatal("Status != Ok:", response1.GetStatus().String())
	}
}
Beispiel #4
0
func (conn *Connection) writeLoop() {
	defer conn.ioWait.Done()

	var err error
	for response := range conn.Wch {
		conn.funResetWriteTimeout()
		err = dlock.SendMessage(conn.w, response)
		if err != nil {
			log.Printf("Connection.writeLoop: %s #%d response.RequestId=%d response.Status=%s SendMessage() error: %s",
				conn.clientId, conn.messageCount, response.GetRequestId(), response.GetStatus().String(), err.Error())
			return
		}

		err = conn.w.Flush()
		if err != nil {
			log.Printf("Connection.writeLoop: %s #%d response.RequestId=%d response.Status=%s Flush() error: %s",
				conn.clientId, conn.messageCount, response.GetRequestId(), response.GetStatus().String(), err.Error())
			return
		}
	}
}