第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

強(qiáng)制 Gitlab 在 Go 失敗時(shí)重試 webhook

強(qiáng)制 Gitlab 在 Go 失敗時(shí)重試 webhook

Go
米琪卡哇伊 2022-06-01 09:56:02
我想監(jiān)視 Gitlab 項(xiàng)目中的每個(gè)事件并將它們存儲(chǔ)在外部服務(wù)中。為此,我使用 Gitlab Webhooks。我在 Go 中創(chuàng)建了一個(gè)小型本地 HTTP 服務(wù)器,它監(jiān)聽 Gitlab 的 POST 并將它們轉(zhuǎn)發(fā)到外部服務(wù)。Hooks 包含我需要的所有信息,所以看起來(lái)這個(gè)架構(gòu)很好:Gitlab > HTTPServer > External Service.我的問(wèn)題是當(dāng)外部服務(wù)關(guān)閉時(shí),我無(wú)法讓 Gitlab 重試失敗的請(qǐng)求。正如文檔所說(shuō):GitLab 忽略端點(diǎn)返回的 HTTP 狀態(tài)代碼。您的端點(diǎn)應(yīng)始終返回有效的 HTTP 響應(yīng)。如果你不這樣做,GitLab 會(huì)認(rèn)為鉤子失敗并重試。令人驚訝的是,Gitlab 沒有正確的方法來(lái)請(qǐng)求 webhook 重試。我必須明確返回?zé)o效的 http 響應(yīng)。此外,我找不到一個(gè) API 端點(diǎn)來(lái)列出所有失敗的 webhook 并要求重新發(fā)送。問(wèn)題:如何使用標(biāo)準(zhǔn)的“net/http”庫(kù)顯式返回?zé)o效的 HTTP 響應(yīng),以強(qiáng)制 Gitlab 重試 Webhooks?
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

正如評(píng)論中所寫,webhook 只是一個(gè)事件發(fā)生的通知,并且可能會(huì)發(fā)送一些數(shù)據(jù),通常作為 JSON 數(shù)據(jù)。


您有責(zé)任保留事件本身以及您想要/需要處理的隨它發(fā)送的數(shù)據(jù)。您將在下面找到一個(gè)注釋示例。請(qǐng)注意,這不包括增量退避,但這應(yīng)該很容易添加:


package main


import (

    "encoding/json"

    "flag"

    "io"

    "log"

    "net/http"

    "os"

    "path/filepath"


    "github.com/joncrlsn/dque"

)


var (

    bind        string

    queueDir    string

    segmentSize int

)


// You might want to add request headers and stuff

type webhookContent struct {

    Foo string

    Bar int

}


func init() {

    flag.StringVar(&bind, "bind", ":8080", "The address to bind to")

    flag.StringVar(&queueDir, "path", "./queue", "path to store the queue in")

    flag.IntVar(&segmentSize, "size", 50, "number of entries for the queue")

}


// The "webserver" component

func runserver(q *dque.DQue) {


    http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {

        // A new decoder for each call, as we want to have a new LimitReader

        // for each call. This is a simple, albeit a bit crude method to prevent

        // accidental or malicious overload of your server.

        dec := json.NewDecoder(io.LimitReader(r.Body, 4096))


        defer r.Body.Close()


        c := &webhookContent{}

        if err := dec.Decode(c); err != nil {

            log.Printf("reading body: %s", err)

            http.Error(w, "internal error", http.StatusInternalServerError)

            return

        }


        // When the content is successfully decoded, we can persist it into

        // our queue.

        if err := q.Enqueue(c); err != nil {

            log.Printf("enqueueing webhook data: %s", err)

            // PROPER ERROR HANDLING IS MISSING HERE

        }

    })


    http.ListenAndServe(bind, nil)

}


func main() {

    flag.Parse()


    var (

        q   *dque.DQue

        err error

    )


    if !dirExists(queueDir) {

        if err = os.MkdirAll(queueDir, 0750); err != nil {

            log.Fatalf("creating queue dir: %s", err)

        }

    }


    if !dirExists(filepath.Join(queueDir, "webhooks")) {

        q, err = dque.New("webhooks", queueDir, segmentSize, func() interface{} { return &webhookContent{} })

    } else {

        q, err = dque.Open("webhooks", queueDir, segmentSize, func() interface{} { return &webhookContent{} })

    }


    if err != nil {

        log.Fatalf("setting up queue: %s", err)

    }


    defer q.Close()


    go runserver(q)


    var (

        // Placeholder during event loop

        i interface{}

        // Payload

        w *webhookContent

        // Did the type assertion succeed

        ok bool

    )


    for {

        // We peek only. The semantic of this is that

        // you can already access the next item in the queue

        // without removing it from the queue and "mark" it as read.

        // We use PeekBlock since we want to wait for an item in the

        // queue to be available.

        if i, err = q.PeekBlock(); err != nil {

            // If we can not peek, something is SERIOUSLY wrong.

            log.Fatalf("reading from queue: %s", err)

        }


        if w, ok = i.(*webhookContent); !ok {

            // If the type assertion fails, something is seriously wrong, too.

            log.Fatalf("reading from queue: %s", err)

        }


        if err = doSomethingUseful(w); err != nil {

            log.Printf("Something went wrong: %s", err)

            log.Println("I strongly suggest entering an incremental backoff!")

            continue

        }


        // We did something useful, so we can dequeue the item we just processed from the queue.

        q.Dequeue()

    }


}


func doSomethingUseful(w *webhookContent) error {

    log.Printf("Instead of this log message, you can do something useful with: %#v", w)

    return nil

}


func dirExists(path string) bool {

    fileInfo, err := os.Stat(path)

    if err == nil {

        return fileInfo.IsDir()

    }

    return false

}

現(xiàn)在,當(dāng)您執(zhí)行以下操作時(shí):


$ curl -X POST --data '{"Foo":"Baz","Bar":42}' http://localhost:8080/webhook

你應(yīng)該得到一個(gè)日志條目


2020/04/18 11:34:23 Instead of this log message, you can do something useful with: &main.webhookContent{Foo:"Baz", Bar:42}



查看完整回答
反對(duì) 回復(fù) 2022-06-01
  • 1 回答
  • 0 關(guān)注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)