关于区块链:设置apiserver中间层的重定向实现ipfs存查可控

7次阅读

共计 983 个字符,预计需要花费 3 分钟才能阅读完成。

1. 起因阐明

ipfs 公有网络实现文件类型的存取,但针对具体利用,肯定水平上需限度用户存取,保障不呈现有限存查的状况。

2. 设置路由

在 apiserver 中的 router 文件夹中配置 http 拜访的路由信息

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

}

正文完
 0