-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_server.go
49 lines (41 loc) · 1.11 KB
/
grpc_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
package main
import (
"context"
"log"
"github.com/Enan01/easyid/api"
)
type GrpcServer struct{}
func (s *GrpcServer) Next(ctx context.Context, in *api.UserId) (*api.ID, error) {
id, err := LocalIDGenerators.GetByUserId(ctx, in.GetUserId()).Next(ctx)
if err != nil {
return nil, err
}
return &api.ID{
Id: id,
}, nil
}
func (s *GrpcServer) NextByUserIds(ctx context.Context, in *api.UserIds) (*api.IDs, error) {
log.Printf("GRPC 批量获取 用户ID[%v]", in.GetUserIds())
// userIds 去重
uids := make(map[uint64]struct{})
for _, uid := range in.GetUserIds() {
uids[uid] = struct{}{}
}
ids := &api.IDs{Ids: make(map[uint64]uint64)}
for uid := range uids {
id, err := LocalIDGenerators.GetByUserId(ctx, uid).Next(ctx)
if err != nil {
return nil, err
}
ids.Ids[uid] = id
}
return ids, nil
}
func (s *GrpcServer) SlaveNextByUserIds(ctx context.Context, in *api.UserIds) (*api.IDs, error) {
log.Printf("GRPC slave 节点请求 批量获取 用户ID[%v]", in.GetUserIds())
ids, err := NextByUserIds(ctx, in.GetUserIds())
if err != nil {
return nil, err
}
return &api.IDs{Ids: ids}, nil
}