🐛 Fixed memleak
This commit is contained in:
		
							
								
								
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					tmp/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*.exe
 | 
				
			||||||
@@ -5,9 +5,9 @@ export default function () {
 | 
				
			|||||||
    for (let i = 0; i < 250; i++) {
 | 
					    for (let i = 0; i < 250; i++) {
 | 
				
			||||||
        var rand = Math.random()
 | 
					        var rand = Math.random()
 | 
				
			||||||
        if (rand > 0.5) {
 | 
					        if (rand > 0.5) {
 | 
				
			||||||
            http.get(`http://127.0.0.1:8080/get/${random_string(1)}`);
 | 
					            http.get(`http://127.0.0.1:8082/get/${random_string(1)}`);
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            http.post(`http://127.0.0.1:8080/set/${random_string(1)}`, random_string(5));
 | 
					            http.post(`http://127.0.0.1:8082/set/${random_string(1)}`, random_string(5));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +0,0 @@
 | 
				
			|||||||
import http from "k6/http";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export default function () {
 | 
					 | 
				
			||||||
    http.get(`http://localhost:8082/`);
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
@@ -1,41 +0,0 @@
 | 
				
			|||||||
module main
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import vweb
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const (
 | 
					 | 
				
			||||||
	port = 8082
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
struct App {
 | 
					 | 
				
			||||||
	vweb.Context
 | 
					 | 
				
			||||||
mut:
 | 
					 | 
				
			||||||
	state shared State
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
struct State {
 | 
					 | 
				
			||||||
mut:
 | 
					 | 
				
			||||||
	kv_store map[string]string = {}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn main() {
 | 
					 | 
				
			||||||
	mut app := &App{}
 | 
					 | 
				
			||||||
	println('stupid kvstore')
 | 
					 | 
				
			||||||
	vweb.run(app, port)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
['/get/:key']
 | 
					 | 
				
			||||||
pub fn (mut app App) kv_get(key string) vweb.Result {
 | 
					 | 
				
			||||||
	lock app.state {
 | 
					 | 
				
			||||||
		res := app.state.kv_store[key] or { return app.not_found() }
 | 
					 | 
				
			||||||
		return app.ok(res)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return app.not_found()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
['/set/:key'; post]
 | 
					 | 
				
			||||||
fn (mut app App) kv_set(key string) vweb.Result {
 | 
					 | 
				
			||||||
	lock app.state {
 | 
					 | 
				
			||||||
		app.state.kv_store[key] = app.req.data
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return app.text('OK')
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
							
								
								
									
										29
									
								
								stupid-kv.v
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								stupid-kv.v
									
									
									
									
									
								
							@@ -1,10 +1,9 @@
 | 
				
			|||||||
module main
 | 
					module main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import vweb
 | 
					import vweb
 | 
				
			||||||
import json
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	port = 8080
 | 
						port = 8082
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct App {
 | 
					struct App {
 | 
				
			||||||
@@ -15,45 +14,31 @@ mut:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
struct State {
 | 
					struct State {
 | 
				
			||||||
mut:
 | 
					mut:
 | 
				
			||||||
	kv_store string
 | 
						kv_store map[string]string = {}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() {
 | 
				
			||||||
	mut app := &App{}
 | 
						mut app := &App{}
 | 
				
			||||||
	println('stupid kvstore')
 | 
						println('stupid kvstore')
 | 
				
			||||||
	lock app.state {
 | 
						lock app.state {
 | 
				
			||||||
		app.state.kv_store = '{}'
 | 
							app.state.kv_store = {}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	//vweb.run(app, port)
 | 
					 | 
				
			||||||
	vweb.run_at(app, family: .ip, host: 'localhost', port: port)?
 | 
						vweb.run_at(app, family: .ip, host: 'localhost', port: port)?
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
['/get/:key']
 | 
					['/get/:key']
 | 
				
			||||||
pub fn (mut app App) kv_get(key string) vweb.Result {
 | 
					pub fn (mut app App) kv_get(key string) vweb.Result {
 | 
				
			||||||
	lock app.state {
 | 
						lock app.state {
 | 
				
			||||||
		// data := json.decode(map[string]string, app.state.kv_store) or {
 | 
							res := app.state.kv_store[key] or { return app.not_found() }
 | 
				
			||||||
		// 	return app.server_error(500)
 | 
							return app.ok(res)
 | 
				
			||||||
		// }
 | 
					 | 
				
			||||||
		// if key in data {
 | 
					 | 
				
			||||||
		// 	r := data[key]
 | 
					 | 
				
			||||||
		// 	return app.ok(r)
 | 
					 | 
				
			||||||
		// } else {
 | 
					 | 
				
			||||||
		// 	return app.not_found()
 | 
					 | 
				
			||||||
		// }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		return app.ok(app.state.kv_store)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return app.server_error(500)
 | 
						return app.not_found()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
['/set/:key'; post]
 | 
					['/set/:key'; post]
 | 
				
			||||||
fn (mut app App) kv_set(key string) vweb.Result {
 | 
					fn (mut app App) kv_set(key string) vweb.Result {
 | 
				
			||||||
	lock app.state {
 | 
						lock app.state {
 | 
				
			||||||
		// mut data := json.decode(map[string]string, app.state.kv_store) or {
 | 
							app.state.kv_store[key] = app.req.data
 | 
				
			||||||
		// 	return app.server_error(500)
 | 
					 | 
				
			||||||
		// }
 | 
					 | 
				
			||||||
		// data[key] = app.req.data
 | 
					 | 
				
			||||||
		app.state.kv_store = app.req.data//json.encode(data)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return app.text('OK')
 | 
						return app.text('OK')
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,17 +0,0 @@
 | 
				
			|||||||
module main
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import 	vweb
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
struct App {
 | 
					 | 
				
			||||||
	vweb.Context
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
fn main() {
 | 
					 | 
				
			||||||
	mut app := &App{}
 | 
					 | 
				
			||||||
	vweb.run_at(app, family: .ip, host: 'localhost', port: 8082) ?
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub fn (mut app App) index() vweb.Result {
 | 
					 | 
				
			||||||
	return app.text('World')
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user