golang 使用chan(select)、和goroutine实现:耗时代码块的执行不影响http服务及时响应

简单示例:

package main

import (
	"fmt"
	"math/rand"
	"net/http"
	"time"
)

var myServer = MyServer{}

func main() {	
	_ = http.ListenAndServe(":8080", myServer)
}

type MyServer struct{}

//所有的请求的
func (recv MyServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
	c1 := make(chan int, 1)
	go goRequest(c1)
	//读取chan数据等待1s,超过1s会打印出timeout 1
	select {
	case res := <-c1:
		fmt.Println(res)
	case <-time.After(time.Second * 1):
		fmt.Println("timeout 1")
	}
	endTag := "http Connect done !  " + time.Now().Format("2006-01-02 15:04:05")
	fmt.Println(endTag)
	resp.Write([]byte(endTag))
}

func goRequest(ch chan int) {
	staReqTime := time.Now().UnixNano() / 1e6
	rand.Seed(time.Now().UnixNano())
	proX := rand.Intn(10)
	fmt.Println("proX:", proX)
	if proX < 5 {
		//模拟耗时3秒
		time.Sleep(time.Second * 3)
	}
	endReqTime := time.Now().UnixNano() / 1e6
	costTime := endReqTime - staReqTime
	if costTime > 60 {
		//耗用超过60ms,视为超时
		ch <- 0
	} else {
		ch <- 1
	}
	fmt.Println("goRequest end !costTime:", costTime, time.Now().Format("2006-01-02 15:04:05"))
}

测试结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: package mainimport ( "fmt" "net/http" )func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello World") }) http.ListenAndServe(":8080", nil) } ### 回答2: 当使用Go语言编写一个简单的HTTP服务器时,我们需要使用`net/http`包。下面是一个例子,展示了如何使用Go语言创建HTTP服务器并提供一个简单的"Hello, World!"页面: ```go package main import ( "net/http" ) func main() { http.HandleFunc("/", helloHandler) // 将根路径"/"绑定到helloHandler函数 http.ListenAndServe(":8080", nil) // 在本地监听8080端口并开始接收HTTP请求 } func helloHandler(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, World!")) // 返回"Hello, World!"作为响应 } ``` 在上述代码中,我们定义了一个`helloHandler`函数,该函数会处理来自根路径的HTTP请求,并向客户端(通过`w http.ResponseWriter`)返回一个包含"Hello, World!"的响应。`http.HandleFunc`函数用于将根路径"/"绑定到`helloHandler`函数。 然后,我们使用`http.ListenAndServe`函数来监听端口8080,并启动HTTP服务器,以便开始接收来自客户端的请求。这可以通过访问http://localhost:8080/来测试。会返回"Hello, World!"的响应。 此代码片段只是一个简单的示例,你可以在这个基础上进行更多的HTTP路由和请求处理的开发。 ### 回答3: package main import ( "fmt" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") } func main() { http.HandleFunc("/", helloHandler) err := http.ListenAndServe(":8080", nil) if err != nil { fmt.Println("Error starting HTTP server:", err) } } 以上是一个使用Golang实现的简单的HTTP服务代码。 在main函数中,我们调用了http包中的HandleFunc方法,将根路径"/"与一个名为helloHandler的处理函数关联起来。这个处理函数接收两个参数,一个是http.ResponseWriter,用于构建HTTP响应,另一个是*http.Request,用于获取HTTP请求的信息。 helloHandler函数中使用了fmt包中的Fprintf方法,将字符串"Hello, World!"写入到http.ResponseWriter中,作为HTTP响应返回给客户端。 最后,我们调用了http包中的ListenAndServe方法,指定监听的端口号为8080,并将nil作为第二个参数传递,该参数表示使用默认的路由器。如果启动HTTP服务失败,将打印错误信息到控制台。 通过运行以上代码,可以在本地启动一个简单的HTTP服务器,当访问http://localhost:8080时,服务器将返回"Hello, World!"字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值