This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.go
163 lines (148 loc) · 3.85 KB
/
resources.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"encoding/json"
"errors"
"github.com/dgryski/go-jump"
"strconv"
)
// struct for sending request
// to workers
type Message struct {
data Resource // new resource state
ch chan bool // channel for getting result
}
// Basic resource to store/access/change
type Resource struct {
Id int
Free bool // is resource owned?
Owner string // name of owner, '' if free
}
type Resources struct {
members []Resource // collection of Resources
input []chan Message // input channels for change requests
freeList chan int // freeList chan of free resources
}
// Simple and fast hashring
func chooseWorker(i int, n int) int {
i64 := uint64(i)
place := jump.Hash(i64, n)
return int(place)
}
// Worker that proccess change requests
func (a *Resources) worker(c <-chan Message) {
for msg := range c {
if len(a.members) < msg.data.Id || msg.data.Id < 0 {
// resource is alredy in use or wrong id
msg.ch <- false
} else {
// set new resource state
current := a.members[msg.data.Id].Free
if msg.data.Free != current {
a.members[msg.data.Id] = msg.data
if msg.data.Free == true {
// new state is free? add it to free queue
a.freeList <- msg.data.Id
}
msg.ch <- true
} else {
msg.ch <- false
}
}
close(msg.ch)
}
}
// Take free resource from queue and send new resource state (free)
// to worker, then check result from channel
func (a *Resources) tryAllocate(name string, workers int) (int, error) {
select {
case i := <-a.freeList:
output := make(chan bool)
res := Resource{Id: i, Free: false, Owner: name}
msg := Message{data: res, ch: output}
place := chooseWorker(i, workers)
a.input[place] <- msg
result := <-output
if result == true {
return i + 1, nil
}
default:
return 0, errors.New("Failed")
}
return 0, errors.New("Failed")
}
// Send resuurce state (busy) to worker and check result from channel
func (a *Resources) tryDeallocate(id int, workers int) error {
output := make(chan bool)
res := Resource{Id: id - 1, Free: true, Owner: ""}
msg := Message{data: res, ch: output}
place := chooseWorker(id-1, workers)
a.input[place] <- msg
result := <-output
if result == false {
return errors.New("Failed")
}
return nil
}
type list struct {
Allocated map[string]string `json:"allocated"`
Deallocated []string `json:"dealloced"`
}
// shows all resources as JSON
func (a *Resources) List() string {
allocated := make(map[string]string)
var deallocated []string
for i := range a.members {
id := "r" + (strconv.Itoa(a.members[i].Id + 1))
if a.members[i].Free {
deallocated = append(deallocated, id)
} else {
allocated[id] = a.members[i].Owner
}
}
list := list{allocated, deallocated}
json_list, _ := json.Marshal(list)
return string(json_list)
}
// Create queue of free resources and create workers
func (a *Resources) Init(c Config) {
a.freeList = make(chan int, c.Limit)
for i := 0; i < c.Limit; i++ {
r := Resource{i, true, ""}
a.freeList <- i
a.members = append(a.members, r)
}
for i := 0; i < c.Workers; i++ {
ch := make(chan Message, 10)
a.input = append(a.input, ch)
go a.worker(ch)
}
}
type found_list []string
// Search for resources with name == s
func (a *Resources) Search(s string) string {
// My own json generator, yeah
var found found_list
for i := range a.members {
if !a.members[i].Free && a.members[i].Owner == s {
id := "r" + (strconv.Itoa(a.members[i].Id + 1))
found = append(found, id)
}
}
if len(found) > 0 {
json_found, _ := json.Marshal(found)
return string(json_found)
} else {
return "\"[]\""
}
}
// Send request to free all resources
func (a *Resources) Reset(workers int) {
for i := range a.members {
output := make(chan bool)
res := Resource{Id: i, Free: true, Owner: ""}
msg := Message{data: res, ch: output}
place := chooseWorker(i, workers)
a.input[place] <- msg
<-output
}
}