GithubHelp home page GithubHelp logo

Comments (1)

zhuxiujia avatar zhuxiujia commented on June 13, 2024

目前碰到2个致命问题:

  • 1 栈不能自动扩容缩容 或导致栈溢出。(例如我使用reqwest,排查了好久才发现是栈默认容量不够)服务挂了,设置的太大又造成内存浪费。强烈建议作者能做到自动扩容(虽然我不清楚其中难度是不是特别大)
  • 2 框架不支持阻塞IO自动移动到单独的线程池中。(目前async_std是支持的)。这个问题很玄幻了,开发者当然知道使用协程必须保证你的操作必须非阻塞IO,但是不是所有框架都支持了May的。这不是强迫我吧所有开源框架写一遍支持may吗?。举个例子,我在may mini http中调用 reqwest发起http请求,workers设置为1的情况下,发起一个请求,后续的请求就进不来了。因为被上一个请求阻塞住了。(能接受的最大请求数等于设置的workers数量)
    强烈建议作者能做到(阻塞IO自动移动到单独的线程池,虽然我不清楚其中难度是不是特别大)
impl HttpService for HelloWorld {
    fn call(&mut self, _req: Request, rsp: &mut Response) -> io::Result<()> {

        println!("accept:req");

        go!(move ||{
          //127.0.0.1:8081这个端口设置了延迟1000秒才返回,模拟阻塞
         let resp = reqwest::blocking::get("http://127.0.0.1:8081");
          println!("{:?}",resp);
        });



        rsp.body("Hello, world!");
        Ok(())
    }
}

struct HelloWorldFac;

impl HttpServiceFactory for HelloWorldFac {
    type Service = HelloWorld;

    fn new_service(&self) -> Self::Service {
        HelloWorld
    }
}

fn main() {
    may::config().set_workers(1).set_stack_size(0x2000);
    env_logger::init();
    let server = HelloWorldFac.start("127.0.0.1:8080").unwrap();
    server.wait();
}
//main执行后,用浏览器连续5次打开127.0.0.1:8080 只打印了一次 accept:req

http 8081端口代码

use may_minihttp::{HttpServer, HttpService, Request, Response};
use std::io;
use std::thread::sleep;
use std::time::Duration;

/// `HelloWorld` is the *service* that we're going to be implementing to service
/// the HTTP requests we receive.
///
#[derive(Clone)]
struct HelloWorld;

impl HttpService for HelloWorld {
    fn call(&mut self, _req: Request, rsp: &mut Response) -> io::Result<()> {
        rsp.body("Hello, world!");
        sleep(Duration::from_secs(1024));//使用std库睡眠线程,模拟阻塞
        Ok(())
    }
}

fn main() {
    env_logger::init();
    let server = HttpServer(HelloWorld).start("127.0.0.1:8081").unwrap();
    server.wait();
}

from may.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.