nats.io中關(guān)于包裝類型的使用技巧

  1. 首先定義Optionsstruct
type Options struct {
    // NatsURL is an URL (or comma separated list of URLs) to a node or nodes
    // in the cluster.
    NatsURL string

    // NatsConn is a user provided low-level NATS connection that the streaming
    // connection will use to communicate with the cluster. When set, closing
    // the NATS streaming connection does NOT close this NATS connection.
    // It is the responsibility of the application to manage the lifetime of
    // the supplied NATS connection.
    NatsConn *nats.Conn

    // ConnectTimeout is the timeout for the initial Connect(). This value is also
    // used for some of the internal request/replies with the cluster.
    ConnectTimeout time.Duration

    // AckTimeout is how long to wait when a message is published for an ACK from
    // the cluster. If the library does not receive an ACK after this timeout,
    // the Publish() call (or the AckHandler) will return ErrTimeout.
    AckTimeout time.Duration

    // DiscoverPrefix is the prefix connect requests are sent to for this cluster.
    // The default is "_STAN.discover".
    DiscoverPrefix string

    // MaxPubAcksInflight specifies how many messages can be published without
    // getting ACKs back from the cluster before the Publish() or PublishAsync()
    // calls block.
    MaxPubAcksInflight int

    // DEPRECATED: Please use PingInterval instead
    PingIterval int

    // PingInterval is the interval at which client sends PINGs to the server
    // to detect the loss of a connection.
    PingInterval int

    // PingMaxOut specifies the maximum number of PINGs without a corresponding
    // PONG before declaring the connection permanently lost.
    PingMaxOut int

    // ConnectionLostCB specifies the handler to be invoked when the connection
    // is permanently lost.
    ConnectionLostCB ConnectionLostHandler
}
  1. 定義Option類型的字段
type Option func(*Options) error
  1. 定義Option類型的包裝
// NatsURL is an Option to set the URL the client should connect to.
// The url can contain username/password semantics. e.g. nats://derek:pass@localhost:4222
// Comma separated arrays are also supported, e.g. urlA, urlB.
func NatsURL(u string) Option {
    return func(o *Options) error {
        o.NatsURL = u
        return nil
    }
}

// ConnectWait is an Option to set the timeout for establishing a connection.
func ConnectWait(t time.Duration) Option {
    return func(o *Options) error {
        o.ConnectTimeout = t
        return nil
    }
}

// PubAckWait is an Option to set the timeout for waiting for an ACK for a
// published message.
func PubAckWait(t time.Duration) Option {
    return func(o *Options) error {
        o.AckTimeout = t
        return nil
    }
}

// MaxPubAcksInflight is an Option to set the maximum number of published
// messages without outstanding ACKs from the server.
func MaxPubAcksInflight(max int) Option {
    return func(o *Options) error {
        o.MaxPubAcksInflight = max
        return nil
    }
}

// NatsConn is an Option to set the underlying NATS connection to be used
// by a streaming connection object. When such option is set, closing the
// streaming connection does not close the provided NATS connection.
func NatsConn(nc *nats.Conn) Option {
    return func(o *Options) error {
        o.NatsConn = nc
        return nil
    }
}

// Pings is an Option to set the ping interval and max out values.
// The interval needs to be at least 1 and represents the number
// of seconds.
// The maxOut needs to be at least 2, since the count of sent PINGs
// increase whenever a PING is sent and reset to 0 when a response
// is received. Setting to 1 would cause the library to close the
// connection right away.
func Pings(interval, maxOut int) Option {
    return func(o *Options) error {
        // For tests, we may pass negative value that will be interpreted
        // by the library as milliseconds. If this test boolean is set,
        // do not check values.
        if !testAllowMillisecInPings {
            if interval < 1 || maxOut <= 2 {
                return fmt.Errorf("invalid ping values: interval=%v (min>0) maxOut=%v (min=2)", interval, maxOut)
            }
        }
        o.PingInterval = interval
        o.PingMaxOut = maxOut
        return nil
    }
}

// SetConnectionLostHandler is an Option to set the connection lost handler.
// This callback will be invoked should the client permanently lose
// contact with the server (or another client replaces it while being
// disconnected). The callback will not be invoked on normal Conn.Close().
func SetConnectionLostHandler(handler ConnectionLostHandler) Option {
    return func(o *Options) error {
        o.ConnectionLostCB = handler
        return nil
    }
}
  1. 方法接收
func Connect(stanClusterID, clientID string, options ...Option) (Conn, error) {
    ...
}

這樣,通過(guò)options接收的值,遍歷調(diào)用,驗(yàn)證值得正確性顷牌。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市塞淹,隨后出現(xiàn)的幾起案子窟蓝,更是在濱河造成了極大的恐慌,老刑警劉巖饱普,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件运挫,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡套耕,警方通過(guò)查閱死者的電腦和手機(jī)谁帕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)箍铲,“玉大人雇卷,你說(shuō)我怎么就攤上這事鬓椭〉吆铮” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵小染,是天一觀的道長(zhǎng)翘瓮。 經(jīng)常有香客問(wèn)我,道長(zhǎng)裤翩,這世上最難降的妖魔是什么资盅? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任调榄,我火速辦了婚禮,結(jié)果婚禮上呵扛,老公的妹妹穿的比我還像新娘每庆。我一直安慰自己,他們只是感情好今穿,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開(kāi)白布缤灵。 她就那樣靜靜地躺著,像睡著了一般蓝晒。 火紅的嫁衣襯著肌膚如雪腮出。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,036評(píng)論 1 285
  • 那天芝薇,我揣著相機(jī)與錄音胚嘲,去河邊找鬼。 笑死洛二,一個(gè)胖子當(dāng)著我的面吹牛馋劈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播晾嘶,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼侣滩,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了变擒?” 一聲冷哼從身側(cè)響起君珠,我...
    開(kāi)封第一講書(shū)人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎娇斑,沒(méi)想到半個(gè)月后策添,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡毫缆,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年唯竹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片苦丁。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡浸颓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出旺拉,到底是詐尸還是另有隱情产上,我是刑警寧澤,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布蛾狗,位于F島的核電站晋涣,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏沉桌。R本人自食惡果不足惜谢鹊,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一算吩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧佃扼,春花似錦偎巢、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至翠订,卻和暖如春巢音,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背尽超。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工官撼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人似谁。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓傲绣,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親巩踏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子秃诵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容