package main

import (
    "fmt"
    "net/http"
    "time"
    "context"
    "sync"
)

var Status map[string](chan string) = make(map[string](chan string))
var Timeout = 1000
var Mutex sync.Mutex


func Get(w http.ResponseWriter, r *http.Request) {
    key, ok := r.URL.Query()["key"]
    fmt.Println(key[0])
    if !ok || len(key) < 1 {
        fmt.Println("get params failure")
        return
    }
    Mutex.Lock()
    Status[key[0]] = make(chan string)
    Mutex.Unlock()
    ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Timeout)*time.Second)

    defer func(){
         Mutex.Lock()
         delete(Status, key[0])
         Mutex.Unlock()
         //close(Status[key[0]])
    }()

    defer cancel()
    select {
        case <-ctx.Done():  //timeout超时后,执行此case
            fmt.Fprintln(w, "Time out.")
        case result := <-Status[key[0]]:  //不用context,此处永久阻塞
            fmt.Fprintln(w, result)
    }

}

func Set(w http.ResponseWriter, r *http.Request) {
    fmt.Println(Status)
    value, ok := r.URL.Query()["value"]
    if !ok{
        fmt.Println("get params failure")
        return
    }

    keys, ok := r.URL.Query()["key"]
    fmt.Println(keys[0])
    if !ok{
        fmt.Println("get params failure")
        return
    }
    Mutex.Lock()
    Status[keys[0]] <- value[0]  //chan写key对应value, Get 接口对应pending阻塞将释放,api调用完成.
    Mutex.Unlock()
    defer func(){
        close(Status[keys[0]])
    }()

    fmt.Fprintln(w, "hello set")
}

func OldGet(w http.ResponseWriter, r *http.Request) {
    // Map是不能并发的写操作,但可以并发的读.
    // Status[key[0]] = make(chan string) 初始化通道是map写操作
    // result := <-Status[key[0]] 通道中取值也是map写操作,因通道中值取出后就无值,所以也是写操作
    // Status[key[0]] <- value向Status[key[0]] 通道内写值也map写操作
    // 因此三处操作的是一个map,所以要加同一把锁
    key, ok := r.URL.Query()["key"]
    fmt.Println(key[0])
    if !ok || len(key) < 1 {
        fmt.Println("get params failure")
        return
    }
    Status[key[0]] = make(chan string)
    ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Timeout)*time.Second)
    defer cancel()
    select {
        case <-ctx.Done():  //timeout超时后,执行此case
            fmt.Fprintln(w, "Time out.")
        case result := <-Status[key[0]]:  //不用context,此处永久阻塞
            fmt.Fprintln(w, result)
    }

}




func main() {
    http.HandleFunc("/gets", Get)
    http.HandleFunc("/sets", Set)
    http.ListenAndServe(":8000", nil)
}


有疑问加站长微信联系(非本文作者)