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 }
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 }
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()) } }
func (conn *Connection) readLoop() { defer conn.server.releaseClient(&conn.clientId) defer conn.ioWait.Done() defer close(conn.Rch) var err error for { conn.messageCount++ conn.funResetIdleTimeout() _, err = conn.r.Peek(4) if err != nil { log.Printf("Connection.readLoop: %s #%d peek error: %s", conn.clientId, conn.messageCount, err.Error()) return } request := &dlock.Request{} conn.funResetReadTimeout() err = dlock.ReadMessage(conn.r, request, conn.server.ConfigMaxMessage) if err == nil && request.Lock != nil && len(request.Lock.Keys) > 1 { sort.Strings(request.Lock.Keys) } if err != nil { // On EOF, close silently. if err == io.EOF { return } log.Printf("Connection.readLoop: %s #%d read error: %s", conn.clientId, conn.messageCount, err.Error()) return } conn.LastRequestTime = time.Now() conn.Rch <- request } }