forked from ehazlett/stellar
/
lock.go
36 lines (29 loc) · 744 Bytes
/
lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package datastore
import (
"context"
"time"
api "github.com/ehazlett/stellar/api/services/datastore/v1"
ptypes "github.com/gogo/protobuf/types"
"github.com/sirupsen/logrus"
)
func (s *service) AcquireLock(ctx context.Context, req *api.AcquireLockRequest) (*ptypes.Empty, error) {
s.lock.Lock()
timeout, err := ptypes.DurationFromProto(req.Timeout)
if err != nil {
return empty, err
}
go func() {
select {
case <-time.After(timeout):
logrus.Warnf("lock timeout occurred (%s)", timeout)
s.lock.Unlock()
case <-s.lockChan:
s.lock.Unlock()
}
}()
return empty, nil
}
func (s *service) ReleaseLock(ctx context.Context, _ *api.ReleaseLockRequest) (*ptypes.Empty, error) {
s.lockChan <- true
return empty, nil
}