/
server.go
88 lines (65 loc) 路 1.7 KB
/
server.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package server
import (
"context"
"fmt"
"sync"
"time"
"github.com/CGA1123/tomato/pb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
var (
Duration = 25 * time.Minute
)
type Server struct {
pb.UnimplementedTomatoServiceServer
mut sync.Mutex
ends time.Time
tomato *time.Timer
}
func New() *Server {
return &Server{}
}
func (s *Server) stop() time.Duration {
if s.tomato == nil {
return time.Duration(0)
}
remaining := s.remaining()
s.tomato = nil
s.ends = time.Now()
return remaining
}
func (s *Server) start() (time.Time, error) {
if s.tomato != nil {
return time.Now(), fmt.Errorf("tomato is still runnning")
}
s.tomato = time.AfterFunc(Duration, func() { s.stop() })
s.ends = time.Now().Add(Duration)
return s.ends, nil
}
func (s *Server) remaining() time.Duration {
if s.tomato == nil {
return time.Duration(0)
}
return time.Until(s.ends)
}
func (s *Server) Start(ctx context.Context, _ *emptypb.Empty) (*timestamppb.Timestamp, error) {
s.mut.Lock()
defer s.mut.Unlock()
ends, err := s.start()
return timestamppb.New(ends), err
}
func (s *Server) Stop(ctx context.Context, _ *emptypb.Empty) (*durationpb.Duration, error) {
s.mut.Lock()
defer s.mut.Unlock()
return durationpb.New(s.stop()), nil
}
func (s *Server) Running(ctx context.Context, _ *emptypb.Empty) (*wrapperspb.BoolValue, error) {
running := s.tomato != nil
return wrapperspb.Bool(running), nil
}
func (s *Server) Remaining(ctx context.Context, _ *emptypb.Empty) (*durationpb.Duration, error) {
return durationpb.New(s.remaining()), nil
}