File: rate.go

package info (click to toggle)
golang-github-farsightsec-go-nmsg 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 500 kB
  • sloc: sh: 21; makefile: 3
file content (39 lines) | stat: -rw-r--r-- 1,079 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Copyright (c) 2017 by Farsight Security, Inc.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package nmsg

/*
#cgo pkg-config: libnmsg
#cgo LDFLAGS: -lnmsg
#include <nmsg.h>
#include <stdlib.h>
*/
import "C"
import "runtime"

// A Rate provides Rate limiting across one or more outputs.
type Rate struct{ rate C.nmsg_rate_t }

// NewRate initializes and returns a rate context. The rate parameter
// specifies the target rate of packets (containers and fragments) sent
// on all outputs using the Rate. The freq parameter specifies how often
// (in packets) to check the rate limit.
func NewRate(rate, freq uint) *Rate {
	r := &Rate{C.nmsg_rate_init(C.uint(rate), C.uint(freq))}
	runtime.SetFinalizer(r, func(r *Rate) {
		C.nmsg_rate_destroy(&r.rate)
	})
	return r
}

// Sleep pauses for an appropriate amount of time to maintain the given
// output rate.
func (r *Rate) Sleep() {
	C.nmsg_rate_sleep(r.rate)
}