Skip to content

Commit e6b5ad8

Browse files
committed
chores: update mainstream to v0.1.0-86
1 parent 032bb2f commit e6b5ad8

17 files changed

Lines changed: 180 additions & 50 deletions

hoshi-lang

Submodule hoshi-lang updated from 4028007 to 03bb333

src/kiana/application.hoshi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ alias AfterRequestHook = func(response.Response) : lang.Result<response.Response
2525

2626
struct ApplicationConfig {
2727
host: str.Str,
28-
port: short,
28+
datafield port: short,
2929
route: route.RouteTree,
3030
before_requests: vec.Vec<BeforeRequestHook>,
3131
after_requests: vec.Vec<AfterRequestHook>,
@@ -110,6 +110,9 @@ impl Application {
110110
this.config.add_route(path, handler)
111111
},
112112
serve() : ResultCode {
113+
if (this.backend.init() == false) {
114+
return ResultCode.SOCKET_CREATION_FAIL
115+
}
113116
let sock = socket.socket()
114117
if (sock.is_err()) {
115118
return ResultCode.SOCKET_CREATION_FAIL

src/kiana/backends/legacy.hoshi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ impl LegacyBackend {
1414
}
1515

1616
impl LegacyBackend : taskBackend.TaskBackend {
17-
init() : none {
18-
return
17+
init() : bool {
18+
return true
1919
},
2020
new_task(sock: socket.AbstractSocket, resp : func() : response.Response) : none {
2121
resp().write_to(sock)

src/kiana/backends/taskpool.hoshi

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use taskBackend "kiana/taskBackend"
2+
use request "kiana/request"
3+
use response "kiana/response"
4+
use socket "net/socket"
5+
use threading "threading"
6+
use vec "vec"
7+
use lang "builtin"
8+
9+
struct PoolTask {
10+
sock: socket.AbstractSocket,
11+
resp_func: func() : response.Response,
12+
constructor(sock: socket.AbstractSocket, resp_func: func() : response.Response)
13+
}
14+
15+
impl PoolTask {
16+
constructor(sock: socket.AbstractSocket, resp_func: func() : response.Response) {
17+
this.sock = sock
18+
this.resp_func = resp_func
19+
}
20+
}
21+
22+
struct TaskPoolBackend {
23+
thread_count: int,
24+
tasks: vec.Vec<PoolTask>,
25+
head: int,
26+
mutex: threading.Mutex,
27+
condition: threading.Condition,
28+
workers: vec.Vec<threading.Thread>,
29+
stop_flag: bool,
30+
constructor(),
31+
constructor(thread_count: int),
32+
worker_loop() : none
33+
}
34+
35+
impl TaskPoolBackend {
36+
constructor() {
37+
this.thread_count = threading.hardware_concurrency()
38+
this.tasks = vec.Vec<PoolTask>()
39+
this.head = 0
40+
this.mutex = threading.Mutex()
41+
this.condition = threading.Condition(this.mutex)
42+
this.workers = vec.Vec<threading.Thread>()
43+
this.stop_flag = false
44+
},
45+
constructor(thread_count: int) {
46+
this.thread_count = thread_count
47+
this.tasks = vec.Vec<PoolTask>()
48+
this.head = 0
49+
this.mutex = threading.Mutex()
50+
this.condition = threading.Condition(this.mutex)
51+
this.workers = vec.Vec<threading.Thread>()
52+
this.stop_flag = false
53+
},
54+
worker_loop() : none {
55+
while (true) {
56+
let task_opt : PoolTask = null
57+
58+
this.mutex.lock()
59+
while (this.stop_flag == false && this.head == this.tasks.length) {
60+
this.condition.wait()
61+
}
62+
63+
if (this.stop_flag && this.head == this.tasks.length) {
64+
this.mutex.unlock()
65+
return
66+
}
67+
68+
task_opt = this.tasks[this.head]
69+
this.head += 1
70+
71+
if (this.head == this.tasks.length) {
72+
this.tasks.length = 0
73+
this.head = 0
74+
}
75+
76+
this.mutex.unlock()
77+
78+
// Execute task
79+
if (task_opt != null) {
80+
task_opt.resp_func().write_to(task_opt.sock)
81+
task_opt.sock.close()
82+
}
83+
}
84+
}
85+
}
86+
87+
impl TaskPoolBackend : taskBackend.TaskBackend {
88+
init() : bool {
89+
for (let i = 0; i < this.thread_count; i += 1) {
90+
let thread = threading.Thread(func[this]() : none {
91+
this.this.worker_loop()
92+
})
93+
if (thread.start().is_err()) {
94+
return false
95+
}
96+
this.workers.push(thread)
97+
}
98+
return true
99+
},
100+
101+
new_task(sock: socket.AbstractSocket, resp : func() : response.Response) : none {
102+
this.mutex.lock()
103+
this.tasks.push(PoolTask(sock, resp))
104+
this.condition.signal()
105+
this.mutex.unlock()
106+
},
107+
108+
stop() : none {
109+
this.mutex.lock()
110+
this.stop_flag = true
111+
this.condition.signal() // Might need notify_all if kiana supported it, but signal in a loop/multiple times
112+
this.mutex.unlock()
113+
114+
// Signal everyone
115+
for (let i = 0; i < this.workers.length; i += 1) {
116+
this.mutex.lock()
117+
this.condition.signal()
118+
this.mutex.unlock()
119+
}
120+
121+
for (let i = 0; i < this.workers.length; i += 1) {
122+
this.workers[i].join()
123+
}
124+
}
125+
}

src/kiana/backends/thread.hoshi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ impl ThreadingBackend {
1515
}
1616

1717
impl ThreadingBackend : taskBackend.TaskBackend {
18-
init() : none {
19-
return
18+
init() : bool {
19+
return true
2020
},
2121
new_task(sock: socket.AbstractSocket, resp : func() : response.Response) : none {
2222
threading.Thread(func [sock, resp] () : none {

src/kiana/route.hoshi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct RouteTree {
3232
node_type: RouteTreeNodeType,
3333
matching_pattern: str.Str,
3434
handler: RouteHandler,
35-
is_end: bool,
35+
datafield is_end: bool,
3636
full_path: str.Str,
3737

3838
constructor(pattern: str.Str, node_type: RouteTreeNodeType),

src/kiana/session.hoshi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ use hashMap "hashMap"
33

44
struct Session {
55
data: hashMap.HashMap<str.Str, str.Str>,
6-
expires: int
6+
datafield expires: int
77
}

src/logger.hoshi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Logger {
1818
}
1919

2020
struct ConsoleLogger {
21-
level: Severity,
21+
datafield level: Severity,
2222
constructor(level: Severity)
2323
}
2424

src/net/cookie.hoshi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use lang "builtin"
99

1010
struct Cookie {
1111
sections: hashMap.HashMap<str.Str, str.Str>,
12-
section_secure: bool,
13-
section_http_only: bool,
12+
datafield section_secure: bool,
13+
datafield section_http_only: bool,
1414
constructor(),
1515
constructor(sections: hashMap.HashMap<str.Str, str.Str>),
1616
put(key: str.Str, value: str.Str) : Cookie,
@@ -135,8 +135,8 @@ struct CookieSection {
135135
path: str.Str,
136136
expires: time.Date,
137137
max_age: time.Duration,
138-
secure: bool,
139-
http_only: bool,
138+
datafield secure: bool,
139+
datafield http_only: bool,
140140
constructor(),
141141
constructor(key: str.Str, value: str.Str, domain: str.Str, path: str.Str, expires: time.Date, max_age: time.Duration, secure: bool, http_only: bool),
142142
expires(e: time.Date) : CookieSection,

src/net/http.hoshi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ struct RequestReader {
214214
socket: socket.AbstractSocket,
215215
cached_recv: io.ArrayBuffer,
216216
headers: hashMap.HashMap<str.Str, str.Str>,
217-
is_chunked: bool,
218-
content_length: int,
219-
read_pos: int,
220-
chunk_remaining: int,
221-
is_eof_flag: bool,
217+
datafield is_chunked: bool,
218+
datafield content_length: int,
219+
datafield read_pos: int,
220+
datafield chunk_remaining: int,
221+
datafield is_eof_flag: bool,
222222
constructor(socket: socket.AbstractSocket, cached_recv: io.ArrayBuffer, headers: hashMap.HashMap<str.Str, str.Str>),
223223
recv_next_raw() : lang.Result<io.ArrayBuffer, HTTPNetworkError>,
224224
read(size: int) : file.ReadBufferView,

0 commit comments

Comments
 (0)