Golang http.RoundTripper 笔记

柯里露台
• 阅读 5268
RoundTripper is an interface representing the ability to execute a single HTTP transaction, obtaining the Response for a given Request.

对于http客户端,可以使用不同的实现了 RoundTripper 接口的Transport实现来配置它的行为

RoundTripper 有点像 http.Client 的中间件

接口定义

type RoundTripper interface { 
       RoundTrip(*Request) (*Response, error)
}

需要实现RoundTrip函数

type SomeClient struct {}

func (s *SomeClient) RoundTrip(r *http.Request)(*Response, error) {
    //Something comes here...Maybe
}

场景

原文: https://lanre.wtf/blog/2017/0...

  • 缓存 responses,比如 app需要访问 Github api,获取 trending repos,这个数据变动不频繁,假设30分钟变动一次,你显然不希望每次都要点击api都要来请求Github api,解决这个问题的方法是实现这样的http.RoundTripper

    • 有缓存时从缓存取出response数据
    • 过期,数据通过重新请求api获取
  • 根据需要设置http header, 一个容易想到的例子go-github一个Github的 api的go客户端。某些github api不需要认证,有些需要认证则需要提供自己的http client,比如 ghinstallation,下面是ghinstallation 的 RoundTrip 函数实现,设置 Authorization 头

Golang http.RoundTripper 笔记

  • 限速(Rate limiting) 控制请求速率

实际的例子

实现http.RoundTripper 缓存 http response的逻辑。

一个http server的实现

import (
    "fmt"
    "net/http"
)

func main() {
    // server/main.go
    mux := http.NewServeMux()

    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // This is here so we can actually see that the responses that have been cached don't get here
        fmt.Println("The request actually got here")

        w.Write([]byte("You got here"))
    })

    http.ListenAndServe(":8000", mux)
}

http client中创建新的 http.Transport 实现 http.RoundTripper接口

主程序main实现
https://github.com/adelowo/ro...

func main() {
    cachedTransport := newTransport()

    // cachedTransport 是自定义实现http.RoundTripper接口的 Transport
    client := &http.Client{
        Transport: cachedTransport,
        Timeout:   time.Second * 5,
    }

    // 每5秒清除缓存
    cacheClearTicker := time.NewTicker(time.Second * 5)

    //每秒请求一次,可以看出response是从缓存获取还是从服务器请求
    reqTicker := time.NewTicker(time.Second * 1)

    terminateChannel := make(chan os.Signal, 1)

    signal.Notify(terminateChannel, syscall.SIGTERM, syscall.SIGHUP)

    req, err := http.NewRequest(http.MethodGet, "http://localhost:8000", strings.NewReader(""))

    if err != nil {
        panic("Whoops")
    }

    for {
        select {
        case <-cacheClearTicker.C:
            // Clear the cache so we can hit the original server
            cachedTransport.Clear()

        case <-terminateChannel:
            cacheClearTicker.Stop()
            reqTicker.Stop()
            return

        case <-reqTicker.C:

            resp, err := client.Do(req)

            if err != nil {
                log.Printf("An error occurred.... %v", err)
                continue
            }

            buf, err := ioutil.ReadAll(resp.Body)

            if err != nil {
                log.Printf("An error occurred.... %v", err)
                continue
            }

            fmt.Printf("The body of the response is \"%s\" \n\n", string(buf))
        }
    }
}

cacheTransport 中 RoundTrip 函数实现读取缓存中的reponse

func (c *cacheTransport) RoundTrip(r *http.Request) (*http.Response, error) {

    // Check if we have the response cached..
    // If yes, we don't have to hit the server
    // We just return it as is from the cache store.
    if val, err := c.Get(r); err == nil {
        fmt.Println("Fetching the response from the cache")
        return cachedResponse([]byte(val), r)
    }

    // Ok, we don't have the response cached, the store was probably cleared.
    // Make the request to the server.
    resp, err := c.originalTransport.RoundTrip(r)

    if err != nil {
        return nil, err
    }

    // Get the body of the response so we can save it in the cache for the next request.
    buf, err := httputil.DumpResponse(resp, true)

    if err != nil {
        return nil, err
    }

    // Saving it to the cache store
    c.Set(r, string(buf))

    fmt.Println("Fetching the data from the real source")
    return resp, nil
}

运行结果

Golang http.RoundTripper 笔记

links:

点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
7个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
3年前
typeScript数据类型
//布尔类型letisDone:booleanfalse;//数字类型所有数字都是浮点数numberletdecLiteral:number6;lethexLiteral:number0xf00d;letbinaryLiteral:number0b101
Wesley13 Wesley13
3年前
VBox 启动虚拟机失败
在Vbox(5.0.8版本)启动Ubuntu的虚拟机时,遇到错误信息:NtCreateFile(\\Device\\VBoxDrvStub)failed:0xc000000034STATUS\_OBJECT\_NAME\_NOT\_FOUND(0retries) (rc101)Makesurethekern
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
PHP创建多级树型结构
<!lang:php<?php$areaarray(array('id'1,'pid'0,'name''中国'),array('id'5,'pid'0,'name''美国'),array('id'2,'pid'1,'name''吉林'),array('id'4,'pid'2,'n
Wesley13 Wesley13
3年前
Java 初始化执行顺序以及成员变量初始化顺序
一、静态变量初始化顺序大家先看两个例子:(1)!(https://oscimg.oschina.net/oscnet/66be9168f7cdf36484b71f1d67069f12492.jpg)!(https://oscimg.oschina.net/oscnet/bf5d0b172f00f9aa237b3aee5b58cad5d0
Wesley13 Wesley13
3年前
Java日期时间API系列36
  十二时辰,古代劳动人民把一昼夜划分成十二个时段,每一个时段叫一个时辰。二十四小时和十二时辰对照表:时辰时间24时制子时深夜11:00凌晨01:0023:0001:00丑时上午01:00上午03:0001:0003:00寅时上午03:00上午0
Wesley13 Wesley13
3年前
MBR笔记
<bochs:100000000000e\WGUI\Simclientsize(0,0)!stretchedsize(640,480)!<bochs:2b0x7c00<bochs:3c00000003740i\BIOS\$Revision:1.166$$Date:2006/08/1117