1.原因说明

ipfs私有网络实现文件类型的存取,但针对具体应用,一定程度上需限制用户存取,保证不出现无限存查的情况。

2.设置路由

在apiserver中的router文件夹中配置http访问的路由信息
image
router.GET("getHashIpfs/:hash",handler.GetRequest) //通过apiserver 调用ipfs-api
当通过hash查询并读取文件时,跳转到hander文件夹中的获取请求函数。

3.设置重定向函数

首先获取传入的hash值,再通过重定向函数定向到私有ipfs

func GetRequest(c *gin.Context) {
   key := c.Param("hash")
   c.Redirect(http.StatusMovedPermanently, "http://192.168.177.130:8080/ipfs/"+key)
}

引入包说明:

import (
 "net/http"
 )

设置访问频率(参考)

package main

import (

"fmt"
"sync"
"time"

)

var (

working chan int //goroutine计数器 用于限制最大并发数
wg      sync.WaitGroup

)

func main() {

jobList := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} //要工作的任务清单

//每秒3个请求 最大同时4个请求
duration := time.Second
concurrency := 3
concurrencyMax := 4

ticker := time.NewTicker(duration)
working = make(chan int, concurrencyMax)

//通过限定1个周期派发3个任务来实现限制请求次数
k := 0 //用于控制周期内发送次数
for c, job := range jobList {
    working <- c //计数器+1 可能会发生阻塞

    wg.Add(1)
    go work(job)

    k++
    if k == concurrency {
        <-ticker.C //等待一个周期 可能会白等
        k = 0
    }
}
wg.Wait()

}

func work(j int64) {

defer wg.Done()

fmt.Println("doing work#", j)
<-time.After(5 * time.Second) //假设5秒完成

//工作完成后计数器减1
<-working

}


Musmdic丶
4 声望0 粉丝