本文整理匯總了Golang中container/ring.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: addToKillRing
// addToKillRing adds some text to the kill ring. If mode is 0 it adds it to a
// new node in the end of the kill ring, and move the current pointer to the new
// node. If mode is 1 or 2 it appends or prepends the text to the current entry
// of the killRing.
func (s *State) addToKillRing(text []rune, mode int) {
// Don't use the same underlying array as text
killLine := make([]rune, len(text))
copy(killLine, text)
// Point killRing to a newNode, procedure depends on the killring state and
// append mode.
if mode == 0 { // Add new node to killRing
if s.killRing == nil { // if killring is empty, create a new one
s.killRing = ring.New(1)
} else if s.killRing.Len() >= KillRingMax { // if killring is "full"
s.killRing = s.killRing.Next()
} else { // Normal case
s.killRing.Link(ring.New(1))
s.killRing = s.killRing.Next()
}
} else {
if s.killRing == nil { // if killring is empty, create a new one
s.killRing = ring.New(1)
s.killRing.Value = []rune{}
}
if mode == 1 { // Append to last entry
killLine = append(s.killRing.Value.([]rune), killLine...)
} else if mode == 2 { // Prepend to last entry
killLine = append(killLine, s.killRing.Value.([]rune)...)
}
}
// Save text in the current killring node
s.killRing.Value = killLine
}
示例2: ThreeDRotate
func ThreeDRotate(op ThreeDOperation) error {
newFaceRing := ring.New(8)
newEdgeRing := ring.New(12)
trx := ThreeDTransformer{
newFaceRing, newEdgeRing}
for _, faceColor := range op.ent[op.cubeId].faceMap[op.face] {
trx.faceRing.Value = faceColor
trx.faceRing = trx.faceRing.Next()
}
for _, edgeColorPtr := range op.ent[op.cubeId].edgeMap[op.face] {
trx.edgeRing.Value = *edgeColorPtr
trx.edgeRing = trx.edgeRing.Next()
}
trx.faceRing = trx.faceRing.Move(2 * op.direction)
trx.edgeRing = trx.edgeRing.Move(3 * op.direction)
for i, _ := range op.ent[op.cubeId].faceMap[op.face] {
if v, ok := trx.faceRing.Value.(Color); ok {
op.ent[op.cubeId].faceMap[op.face][i] = v
}
trx.faceRing = trx.faceRing.Next()
}
for i, _ := range op.ent[op.cubeId].edgeMap[op.face] {
if v, ok := trx.edgeRing.Value.(Color); ok {
*op.ent[op.cubeId].edgeMap[op.face][i] = v
}
trx.edgeRing = trx.edgeRing.Next()
}
return nil
}
示例3: valueBuffer
// Process values coming from ADC
func valueBuffer(d *EKReceiver) {
last_sample := make([]time.Time, 31)
for i := 0; i < 31; i++ {
d.ValueBufferRaw[i] = ring.New(RAW_BUFFER_SIZE)
d.ValueBuffer[i] = ring.New(BUFFER_SIZE)
}
for {
v := <-d.buffer_chan
//Move head forwards
d.ValueBufferRaw[v.Channel] = d.ValueBufferRaw[v.Channel].Next()
d.ValueBufferRaw[v.Channel].Value = ChannelData{v.Abstimestamp, v.Value} //Set value
// FIR Calculation
p0 := d.ValueBufferRaw[v.Channel]
i := 0
out := float64(0)
for ; i < d.FIRTaps && p0.Value != nil; i++ {
out += p0.Value.(ChannelData).Value
p0 = p0.Prev()
}
out = out / float64(i)
if v.Abstimestamp.Sub(last_sample[v.Channel]) >= d.SampleTime {
d.ValueBuffer[v.Channel] = d.ValueBuffer[v.Channel].Next()
d.ValueBuffer[v.Channel].Value = ChannelData{v.Abstimestamp, out}
last_sample[v.Channel] = v.Abstimestamp
}
}
}
示例4: sendproxy
// Return a channel which serves as a sending proxy to `out`.
// Use a goroutine to receive values from `out` and store them
// in an expanding buffer, so that sending to `out` never blocks.
// See this discussion:
// <http://rogpeppe.wordpress.com/2010/02/10/unlimited-buffering-with-low-overhead>
func sendproxy(out chan<- uint64) chan<- uint64 {
proxy := make(chan uint64, 1024)
go func() {
n := 1024 // the allocated size of the circular queue
first := ring.New(n)
last := first
var c chan<- uint64
var e uint64
for {
c = out
if first == last {
// buffer empty: disable output
c = nil
} else {
e = first.Value.(uint64)
}
select {
case e = <-proxy:
last.Value = e
if last.Next() == first {
// buffer full: expand it
last.Link(ring.New(n))
n *= 2
}
last = last.Next()
case c <- e:
first = first.Next()
}
}
}()
return proxy
}
示例5: main
func main() {
r := ring.New(5)
r.Value = 1
r.Next().Value = 2
r.Prev().Value = 5
plus := ring.New(1)
plus.Value = 10
r.Link(plus)
fmt.Println("len: ", r.Len())
r.Do(printe)
}
示例6: init
func init() {
testBackend.groups["alt.test"] = &groupStorage{
group: &nntp.Group{"alt.test", "A test.",
0, 0, 0, nntp.PostingNotPermitted},
articles: ring.New(maxArticles),
}
testBackend.groups["misc.test"] = &groupStorage{
group: &nntp.Group{"misc.test", "More testing.",
0, 0, 0, nntp.PostingPermitted},
articles: ring.New(maxArticles),
}
}
示例7: NewSimpleMovingPercentile
// Create a new simple moving percentile expvar.Var. It will be
// published under `name` and maintain `size` values for
// calculating the percentile.
//
// percentile must be between 0 and 1
//
// An empty name will cause it to not be published
func NewSimpleMovingPercentile(name string, percentile float64, size int) *SimpleMovingStat {
sm := new(SimpleMovingStat)
sm.size = size
sm.mutex = new(sync.Mutex)
sm.values = ring.New(size)
sm.calculate = func(s *SimpleMovingStat) float64 {
ary := make([]float64, 0)
s.values.Do(func(val interface{}) {
if val != nil {
ary = append(ary, val.(float64))
}
})
length := len(ary)
if length == 0 {
return 0.0
}
sort.Float64s(ary)
mid := int(float64(len(ary)) * percentile)
return ary[mid]
}
if name != "" {
expvar.Publish(name, sm)
}
return sm
}
示例8: main
func main() {
sourceAddress := ":3000"
ports := []string{
":3333",
":3334",
}
hostRing := ring.New(len(ports))
for _, port := range ports {
url, _ := url.Parse("http://127.0.0.1" + port)
hostRing.Value = url
hostRing = hostRing.Next()
}
mutex := sync.Mutex{}
director := func(request *http.Request) {
mutex.Lock()
defer mutex.Unlock()
request.URL.Scheme = "http"
request.URL.Host = hostRing.Value.(*url.URL).Host
hostRing = hostRing.Next()
fmt.Println(hostRing)
}
proxy := &httputil.ReverseProxy{Director: director}
server := http.Server{
Addr: sourceAddress,
Handler: proxy,
}
server.ListenAndServe()
}
示例9: testRing
func testRing() {
fmt.Println("testRing -----------------------------------------------------")
// 循環列表
size := 10
// 聲明和初始化
r := ring.New(size)
fmt.Println("len=", r.Len(), ",r=", r, ",r.Prev()=", r.Prev())
for num, e := 0, r.Prev(); e != nil; e = e.Next() {
e.Value = num
num++
if num >= size {
break
}
}
for num, e := 0, r.Prev(); e != nil; e = e.Next() {
fmt.Println("num =", num, ",e.value=", e.Value)
num++
if num >= size+1 {
break
}
}
}
示例10: NewLoadBalancer
//NewLoadBalancer creates a new instance of a Round Robin load balancer
func (rrf *RoundRobinLoadBalancerFactory) NewLoadBalancer(backendName, caCertPath string, servers []config.ServerConfig) (LoadBalancer, error) {
var rrlb RoundRobinLoadBalancer
if backendName == "" {
return nil, fmt.Errorf("Expected non-empty backend name")
}
if len(servers) == 0 {
return nil, fmt.Errorf("Expected at least one server in servers argument")
}
rrlb.backend = backendName
rrlb.servers = ring.New(len(servers))
for _, s := range servers {
lbEndpoint := new(LoadBalancerEndpoint)
lbEndpoint.Address = fmt.Sprintf("%s:%d", s.Address, s.Port)
metrics.SetGauge([]string{"endpoint", lbEndpoint.Address}, 1.0)
lbEndpoint.PingURI = s.PingURI
lbEndpoint.Up = true
lbEndpoint.CACertPath = caCertPath
log.Info("Spawing health check for address ", lbEndpoint.Address)
healthCheckFunction := MakeHealthCheck(lbEndpoint, s, true)
go healthCheckFunction()
log.Info("Adding server with address ", lbEndpoint.Address)
rrlb.servers.Value = lbEndpoint
rrlb.servers = rrlb.servers.Next()
}
return &rrlb, nil
}
示例11: initRedis
//初始化redis連接池
func initRedis(confs []*config.Redis) {
ids = ring.New(len(confs))
for _, conf := range confs {
if !conf.Enable {
continue
}
id := &IdAvailable{Id: conf.Id}
ids.Value = id
spec := redis.DefaultSpec().Db(conf.DB).Password(conf.Password).Host(conf.Host).Port(conf.Port)
//log.Info("redis init at ", spec)
c := make(chan *RedisClient, PoolSize)
for j := 0; j < PoolSize; j++ {
if client, err := redis.NewSynchClientWithSpec(spec); err != nil {
goto err
} else {
c <- &RedisClient{Id: conf.Id, Redis: client}
}
}
pool[conf.Id] = c
id.IsAvailable = true
ids = ids.Next()
err: //如果創建CLIENT時出錯,就拋棄這個台機器
errRedis[conf.Id] = conf
ids = ids.Next()
}
}
示例12: main
func main() {
// Creates a circular list for round-robin HTTP roundtrip.
hosts := []string{
":4000",
":5000",
":6000",
}
hostRing := ring.New(len(hosts))
for _, host := range hosts {
hostRing.Value = host
hostRing = hostRing.Next()
}
// Locks by mutex because hostConverter will be executed in parallel.
mutex := sync.Mutex{}
hostConverter := func(originalHost string) string {
mutex.Lock()
defer mutex.Unlock()
host := hostRing.Value.(string)
hostRing = hostRing.Next()
return host
}
// Runs a reverse-proxy server on http://localhost:3000/
proxy := entoverse.NewProxyWithHostConverter(hostConverter)
http.ListenAndServe(":3000", proxy)
}
示例13: TestDo
func TestDo(t *testing.T) {
r := ring.New(5) // 5個の要素
if r.Len() != 5 {
t.Errorf("length = %d", r.Len())
}
i := 0
for initialValue := r.Next(); initialValue != r; initialValue = initialValue.Next() {
value := HogeValue(i)
initialValue.Value = &value
i++
}
for p := r.Next(); p != r; p = p.Next() {
t.Logf("value = %d", *p.Value.(*HogeValue))
}
r.Do(func(v interface{}) {
hoge, ok := v.(*HogeValue)
if ok {
hoge.Add(1)
}
})
i = 1
for p := r.Next(); p != r; p = p.Next() {
check := p.Value.(*HogeValue)
if int(*check) != i {
t.Errorf("check = %d, i = %d", *check, i)
}
i++
}
}
示例14: main
func main() {
myArr := string([]rune{
0x49, 0x53, 0x54, 0x53, 0x2d, 0x59, 0x41, 0x52,
0x41, 0x2d, 0x45, 0x4e, 0x55, 0x52, 0x0a,
})
myStr := "Haha!!!! Strings won't find this flag. You need to use something else!!"
promiseRing := ring.New(15)
result := make([]byte, len(myStr))
restore := result
fmt.Println(myStr)
for i := 0; i < promiseRing.Len(); i++ {
result = restore
for j := 0; j < len(myStr); j++ {
result[j] = myStr[j] ^ myArr[i]
}
promiseRing.Value = b64.StdEncoding.EncodeToString(result)
promiseRing = promiseRing.Next()
}
fmt.Println("============")
promiseRing.Do(func(x interface{}) {
fmt.Print(x, "\n============\n")
})
}
示例15: main
func main() {
//創建10個元素的閉環
r := ring.New(10)
//給閉環中的元素賦值
for i := 1; i <= r.Len(); i++ {
r.Value = i
r = r.Next()
}
//循環打印閉環中的元素值
r.Do(
func(p interface{}) {
println(p.(int))
})
//獲得當前元素之後的第5個元素
r5 := r.Move(5)
fmt.Println(r5.Value)
fmt.Println(r.Value)
//鏈接當前元素r與r5,相當於刪除了r與r5之間的元素
r1 := r.Link(r5)
fmt.Println(r1.Value)
fmt.Println(r.Value)
}