-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.go
69 lines (58 loc) · 1.31 KB
/
worker.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
package client
import (
"sync"
)
var workers map[string]Worker
// Worker is in icarus-satellite.
//
// What worker needs to do is to handle server's subtask and do actual response.
// All datum are transfered in []string so the worker need to understand server's
// request correctly and generate suitable response. Errors should be coded in
// response so worker cannot generate any Go-style errors.
type Worker interface {
Login(data []string) []string
ListCourse(data []string) []string
Elect(data []string) []string
}
var workerIniter sync.Once
func initWorker() {
workerIniter.Do(func() {
workers = make(map[string]Worker)
})
}
func RegisterWorker(handle string, w Worker) error {
initWorker()
if len(handle) > handleNameLengthLimit {
return ErrHandleNameTooLong
}
_, ok := workers[handle]
if ok {
return ErrHandleExists
}
workers[handle] = w
return nil
}
func RegisteredWorker() map[string]Worker {
initWorker()
res := make(map[string]Worker)
for k, v := range workers {
res[k] = v
}
return res
}
func RegisteredWorkerList() []string {
initWorker()
res := make([]string, 0)
for k, _ := range workers {
res = append(res, k)
}
return res
}
func GetWorker(handle string) (Worker, error) {
initWorker()
w, ok := workers[handle]
if !ok {
return nil, ErrHandleNotFound
}
return w, nil
}