func main() { give := make(chan []byte) get := make(chan []byte) go func() { q := new(list.List) for { if q.Len() == 0 { q.PushFront(make([]byte, 100)) } e := q.Front() select { case s := <-give: q.PushFront(s) case get <- e.Value.([]byte): q.Remove(e) } } }() // Gets a new buffer from the recycler. buffer := <-get // Give it back to the recycler. give <- buffer // Get the recycled buffer again. buffer = <-get }
func StackSpec(c Context) { stack := new(list.List) c.Specify("An empty stack", func() { c.Specify("contains no elements", func() { c.Expect(stack.Len()).Equals(0) }) }) c.Specify("When elements are pushed onto a stack", func() { stack.PushFront("pushed first") stack.PushFront("pushed last") c.Specify("then it contains some elements", func() { c.Expect(stack.Len()).NotEquals(0) }) c.Specify("the element pushed last is popped first", func() { poppedFirst := stack.Remove(stack.Front()) c.Expect(poppedFirst).Equals("pushed last") }) c.Specify("the element pushed first is popped last", func() { stack.Remove(stack.Front()) poppedLast := stack.Remove(stack.Front()) c.Expect(poppedLast).Equals("pushed first") }) }) }
func houseKeeping(get, give chan interface{}, factory func() interface{}) { q := new(list.List) for { if q.Len() == 0 { atomic.AddInt64(&makes, 1) q.PushFront(queued{when: time.Now(), data: factory()}) } element := q.Front() timeout := time.NewTimer(time.Minute) select { case b := <-give: timeout.Stop() q.PushFront(queued{when: time.Now(), data: b}) case get <- element.Value.(queued).data: timeout.Stop() q.Remove(element) case <-timeout.C: e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(queued).when) > time.Minute { q.Remove(e) e.Value = nil } e = n } } } }
func CreateObject(l *list.List, newId, protectorId *protected_objects.ObjectIdMessage, encKey *tao.Keys, program *auth.Prin, domain *tao.Domain, newType string, newVal []byte) error { if !domain.Guard.IsAuthorized(*program, "CREATE", []string{protectorId.String()}) { return errors.New("program not authorized to create requested secret") } _, _, err := readObjRec(l, encKey, newId) if err == nil { return errors.New("creating object with existing id") } protectorType, protectorKey, err := readObjRec(l, encKey, protectorId) if err != nil { return err } if *protectorType != "key" { return errors.New("creating object protected by object type not key") } new := protected_objects.ObjectMessage{ ObjId: newId, ObjVal: newVal, ObjType: &newType} pNew, err := protected_objects.MakeProtectedObject(new, *protectorId.ObjName, *protectorId.ObjEpoch, protectorKey) l.PushFront(*pNew) return nil }
func Insert(e Elem, L *list.List) int { if L.Len() == 0 { L.PushFront(e) return L.Len() } front := L.Front() if e.GetTime() < front.Value.(Elem).GetTime() { L.InsertBefore(e, front) return L.Len() } el := L.Back() Loop: for { if el.Value.(Elem).GetTime() > e.GetTime() { el = el.Prev() } else { break Loop } } L.InsertAfter(e, el) return L.Len() }
func bufferRecycler(give, get chan *bytes.Buffer) { q := new(list.List) for { //This means that we are getting more than we are giving and memory is not being //cleaned up properly if q.Len() > 1000 { log.Warnf("Memory Recycler Overload (high memory use): %d", q.Len()) } if q.Len() == 0 { q.PushFront(&bytes.Buffer{}) } e := q.Front() select { case s := <-give: s.Reset() q.PushFront(s) case get <- e.Value.(*bytes.Buffer): q.Remove(e) } } }
func recycler(give, get chan []byte) { q := new(list.List) for { //This means that we are getting more than we are giving and memory is not being //cleaned up properly if q.Len() > 1000 { log.Warnf("Memory Recycler Overload (high memory use): %d", q.Len()) } if q.Len() == 0 { q.PushFront(make([]byte, defaultMemorySize)) } e := q.Front() select { case s := <-give: q.PushFront(s[:0]) case get <- e.Value.([]byte): q.Remove(e) } } }
func (server *Server) buildArgumentsTrim(arguments *list.List, params imageserver.Params) error { trim, _ := params.GetBool("trim") if trim { // We must execute trim first. (order of operations) arguments.PushFront("-trim") } return nil }
func HandleResponse(r *ReplyInfo, completed *int, jobs *list.List) { if r.OK { //add to our completed count *completed++ } else { //add job back to queue, it failed. jobs.PushFront(r.job) } }
func dfsrp(g *Digraph, s int, marked map[int]bool, l *list.List) { marked[s] = true for _, v := range g.Adj(s) { if !marked[v.vertex] { dfsrp(g, v.vertex, marked, l) } } l.PushFront(s) // Reverse post }
func Bish_mech(bpos pos) list.List { options := list.List{} // positions of the form row+i,col+i, where i is (TODO: fix this)> -min(row,col) and < // for now lazy implementation that will have options removed as out of board later for i := -7; i < 8; i++ { options.PushFront(pos{bpos.row + i, bpos.col + i}) } return options }
func Pawn_mech(ppos pos) list.List { options := list.List{} // +1 fwd, TODO: +2 fwd if on homeRow // TODO: en passant options.PushFront(pos{ppos.row + 1, ppos.col}) // pawn attack options.PushFront(pos{ppos.row + 1, ppos.col + 1}) options.PushFront(pos{ppos.row + 1, ppos.col - 1}) return options }
// Add the indicated protected object to the list. func AddObject(l *list.List, obj ObjectMessage) error { for e := l.Front(); e != nil; e = e.Next() { o := e.Value.(ObjectMessage) if o.ObjId.ObjName == obj.ObjId.ObjName && o.ObjId.ObjEpoch == obj.ObjId.ObjEpoch { return nil } } l.PushFront(interface{}(obj)) return nil }
func King_mech(kpos pos) list.List { options := list.List{} // TODO: casteling, requires remembering if King ever has been in check for a := -1; a < 2; a++ { for b := -1; b < 2; b++ { options.PushFront(pos{kpos.row + a, kpos.col + b}) // TODO: remove no-move move, (a=0,b=0) } } return options }
func Rook_mech(rpos pos) list.List { options := list.List{} for newCol := 1; newCol < 9; newCol++ { options.PushFront(pos{rpos.row, newCol}) } for newRow := 1; newRow < 9; newRow++ { options.PushFront(pos{newRow, rpos.col}) } //TODO: this will include current position twice and should have current position removed from options by filtering! return options }
//重用client结构 func MakeMessageRecycler() (get, put chan *Message) { get = make(chan *Message) put = make(chan *Message) go func() { queue := new(list.List) for { if queue.Len() == 0 { queue.PushFront(MessageQueued{ when: time.Now(), message: &Message{}, }) } ct := queue.Front() timeout := time.NewTimer(time.Minute) select { case b := <-put: timeout.Stop() b.Mid = 0 b.Uid = 0 b.Content = "" b.Type = 0 b.Time = 0 b.From = 0 b.To = 0 b.Group = 0 mq := MessageQueued{ when: time.Now(), message: b, } queue.PushFront(mq) case get <- ct.Value.(MessageQueued).message: timeout.Stop() queue.Remove(ct) case <-timeout.C: ct := queue.Front() for ct != nil { n := ct.Next() if time.Since(ct.Value.(MessageQueued).when) > time.Minute { queue.Remove(ct) ct.Value = nil } ct = n } } } }() return }
func makeApplication(exprs *list.List) (result Expression) { switch len := exprs.Len(); true { case len >= 2: x1,_ := listPopFront(exprs).(Expression); x2,_ := listPopFront(exprs).(Expression); exprs.PushFront(Application{x1,x2}); result = makeApplication(exprs); case len == 1: result,_ = listPopFront(exprs).(Expression); } return; }
func TestList(t *testing.T) { var myInstance *MyType myInstance = new(MyType) myInstance.name = "hello" var myList list.List myList.Init() myList.PushFront(myInstance) x := myList.Front().Value.(*MyType) fmt.Println((*x).name) }
//notifyExpired func func (self *TimeWheel) notifyExpired(idx int) { var remove *list.List self.lock.RLock() slots := self.wheel[idx] for e := slots.hooks.Back(); nil != e; e = e.Prev() { sj := e.Value.(*slotJob) sj.ttl-- //ttl expired if sj.ttl <= 0 { if nil == remove { remove = list.New() } //记录删除 remove.PushFront(e) self.slotJobWorkers <- true //async go func() { defer func() { if err := recover(); nil != err { //ignored log.Error("TimeWheel|notifyExpired|Do|ERROR|%s\n", err) } <-self.slotJobWorkers }() sj.do() sj.ch <- true close(sj.ch) // log.Debug("TimeWheel|notifyExpired|%d\n", sj.ttl) }() } } self.lock.RUnlock() if nil != remove { //remove for e := remove.Back(); nil != e; e = e.Prev() { re := e.Value.(*list.Element) self.lock.Lock() slots.hooks.Remove(e.Value.(*list.Element)) delete(self.hashWheel, re.Value.(*slotJob).id) self.lock.Unlock() } } }
func verifyImagesAreRemoved(imageManager *dockerImageManager, imageIds ...string) error { var imagesNotRemovedList *list.List = list.New() for _, imageId := range imageIds { _, ok := imageManager.getImageState(imageId) if ok { imagesNotRemovedList.PushFront(imageId) } } if imagesNotRemovedList.Len() > 0 { return errors.New(fmt.Sprintf("Image states still exist for: %v", imagesNotRemovedList)) } else { return nil } }
func verifyImagesAreNotRemoved(imageManager *dockerImageManager, imageIds ...string) error { var imagesRemovedList *list.List = list.New() for _, imageId := range imageIds { _, ok := imageManager.getImageState(imageId) if !ok { imagesRemovedList.PushFront(imageId) } } if imagesRemovedList.Len() > 0 { return errors.New(fmt.Sprintf("Could not find images: %v in ImageManager", imagesRemovedList)) } else { return nil } }
func bufferPool(bufsz int64) (sp *bp) { sp = &bp{ get: make(chan []byte), give: make(chan []byte), quit: make(chan struct{}), timeout: time.Minute, sizech: make(chan int64), } go func() { q := new(list.List) for { if q.Len() == 0 { q.PushFront(qb{when: time.Now(), s: make([]byte, bufsz)}) sp.makes++ } e := q.Front() timeout := time.NewTimer(sp.timeout) select { case b := <-sp.give: timeout.Stop() q.PushFront(qb{when: time.Now(), s: b}) case sp.get <- e.Value.(qb).s: timeout.Stop() q.Remove(e) case <-timeout.C: // free unused slices older than timeout e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(qb).when) > sp.timeout { q.Remove(e) e.Value = nil } e = n } case sz := <-sp.sizech: // update buffer size, free buffers bufsz = sz case <-sp.quit: logger.debugPrintf("%d buffers of %d MB allocated", sp.makes, bufsz/(1*mb)) return } } }() return sp }
func newBufferPool(bufsz int64) (np *bp) { np = &bp{ get: make(chan *bytes.Buffer), give: make(chan *bytes.Buffer), quit: make(chan struct{}), timeout: time.Minute, } go func() { q := new(list.List) for { if q.Len() == 0 { size := bufsz + 100*kb // allocate overhead to avoid slice growth q.PushFront(qBuf{when: time.Now(), buffer: bytes.NewBuffer(makeBuffer(int64(size)))}) np.makes++ } e := q.Front() timeout := time.NewTimer(np.timeout) select { case b := <-np.give: timeout.Stop() q.PushFront(qBuf{when: time.Now(), buffer: b}) case np.get <- e.Value.(qBuf).buffer: timeout.Stop() q.Remove(e) case <-timeout.C: // free unused buffers e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(qBuf).when) > np.timeout { q.Remove(e) e.Value = nil } e = n } case <-np.quit: logger.debugPrintf("%d buffers of %d MB allocated", np.makes, bufsz/(1*mb)) return } } }() return np }
func New(alloc func(size, acap int64) interface{}, size, acap int64, timeout time.Duration) (as *Pool) { as = &Pool{Take: make(chan interface{}), Give: make(chan interface{}), Size: make(chan int64), Quit: make(chan struct{}), Exit: make(chan struct{}), size: size, acap: acap} // timeout: timeout) // size: size, acap: acap} // start buffer manager go func() { q := new(list.List) for { if q.Len() == 0 { b := alloc(as.size, as.acap) //fmt.Printf("Make: len=%d, cap=%d, cnt=%d, qlen=%d\n", len(s), cap(s), as.Cnt, q.Len()) q.PushFront(qb{when: time.Now(), b: b}) as.Cnt++ } e := q.Front() timeout := time.NewTimer(as.timeout) select { case b := <-as.Give: timeout.Stop() //fmt.Printf("Give: len=%d, cap=%d, cnt=%d, qlen=%d\n", len(b), cap(b), as.Cnt, q.Len()) q.PushFront(qb{when: time.Now(), b: b}) case as.Take <- e.Value.(qb).b: //fmt.Printf("Take: cnt=%d, qlen=%d\n", as.Cnt, q.Len()) timeout.Stop() q.Remove(e) case <-timeout.C: // free unused slices older than timeout e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(qb).when) > as.timeout { q.Remove(e) e.Value = nil } e = n } case sz := <-as.Size: // update buffer size, free buffers as.size = sz case <-as.Quit: fmt.Printf("autobuf: Cnt=%d\n", as.Cnt) as.Exit <- struct{}{} return } } }() return as }
/* Make the recycler. Any buffer in the pool (queue) that is older than a minute is considered unlikely to be used again and can be discarded */ func makeRecycler() (get, give chan []byte) { get = make(chan []byte) // Channel to get buffers from the pool give = make(chan []byte) // Channel to give buffers back to the pool // Start function as a goroutine go func() { q := new(list.List) for { if q.Len() == 0 { // If the queue is empty, create a new buffer and add it to the queue q.PushFront(queued{when: time.Now(), slice: makeBuffer()}) } e := q.Front() // Start a 1 minute long timer timeout := time.NewTimer(time.Minute) select { // If there is a buffer in give, add it to the queue case b := <-give: timeout.Stop() q.PushFront(queued{when: time.Now(), slice: b}) // If there is a buffer in queue, send it to the get channel case get <- e.Value.(queued).slice: timeout.Stop() q.Remove(e) // Time is up! Any buffer older than a minute is discarded. case <-timeout.C: // Go through list checking if creation time is more than 1 minute ago e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(queued).when) > time.Minute { q.Remove(e) e.Value = nil } e = n } } } }() // Start function as a goroutine with no params return // Return the get and give channels (named return values) }
func newBufferPool(bufsz int64) (np *bp) { np = new(bp) np.get = make(chan *bytes.Buffer) np.give = make(chan *bytes.Buffer) np.quit = make(chan bool) go func() { q := new(list.List) for { if q.Len() == 0 { size := bufsz + 100*1024 // allocate overhead to avoid slice growth q.PushFront(qBuf{when: time.Now(), buffer: bytes.NewBuffer(makeBuffer(int64(size)))}) np.makes++ } e := q.Front() timeout := time.NewTimer(time.Minute) select { case b := <-np.give: timeout.Stop() q.PushFront(qBuf{when: time.Now(), buffer: b}) case np.get <- e.Value.(qBuf).buffer: timeout.Stop() q.Remove(e) case <-timeout.C: // free unused buffers e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(qBuf).when) > time.Minute { q.Remove(e) e.Value = nil } e = n } case <-np.quit: debugf("%d buffers of %d MB allocated", np.makes, bufsz/(1*1024*1024)) return } } }() return np }
// DocIdsMaker function description : DocId的内存池 // params : // return : func DocIdsMaker() (get, give chan []DocIdNode) { get = make(chan []DocIdNode) give = make(chan []DocIdNode) go func() { q := new(list.List) for { if q.Len() == 0 { q.PushFront(queued{when: time.Now(), slice: makeDocIdSlice()}) } e := q.Front() timeout := time.NewTimer(time.Minute) select { case b := <-give: timeout.Stop() //fmt.Printf("Recive Buffer...\n") //b=b[:MAX_DOCID_LEN] q.PushFront(queued{when: time.Now(), slice: b}) case get <- e.Value.(queued).slice[:0]: timeout.Stop() //fmt.Printf("Sent Buffer...\n") q.Remove(e) case <-timeout.C: e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(queued).when) > time.Minute { q.Remove(e) e.Value = nil } e = n } } } }() return }
func (p *putter) makeRecycler() (get, give chan *bytes.Buffer) { get = make(chan *bytes.Buffer) give = make(chan *bytes.Buffer) go func() { q := new(list.List) for { if q.Len() == 0 { size := p.bufsz + 1*kb q.PushFront(queued{when: time.Now(), buffer: bytes.NewBuffer(makeBuffer(int64(size)))}) //q.PushFront(queued{when: time.Now(), buffer: bytes.NewBuffer(nil)}) //log.Println("Make buffer:", size) p.makes++ } e := q.Front() timeout := time.NewTimer(time.Minute) select { case b := <-give: timeout.Stop() q.PushFront(queued{when: time.Now(), buffer: b}) q_len_max = max(q_len_max, q.Len()) case get <- e.Value.(queued).buffer: timeout.Stop() q.Remove(e) // free unused buffers case <-timeout.C: e := q.Front() for e != nil { n := e.Next() if time.Since(e.Value.(queued).when) > time.Minute { q.Remove(e) e.Value = nil } e = n } } } }() return }
func (b *bfs) shortestPath() *list.List { marked := make([]bool, b.graph.vertices) edgeTo := make([]int, b.graph.vertices) queue := new(queue) targetFound := false var target int queue.enqueue(b.initVertice) marked[b.initVertice] = true for queue.size > 0 && !targetFound { currentVertice := queue.dequeue().(int) adjacents := b.graph.adjacents(currentVertice) for e := adjacents.Front(); e != nil; e = e.Next() { v := e.Value.(int) if !marked[v] { marked[v] = true edgeTo[v] = currentVertice queue.enqueue(v) } if _, ok := b.targetVertices[v]; ok { targetFound = true target = v } } } result := new(list.List) if targetFound { result.PushFront(target) for v := edgeTo[target]; v != b.initVertice; v = edgeTo[v] { result.PushFront(v) } } return result }
func BatchManager(getBatches, returnBatches chan *Batch, stats chan<- NamedValue, config *ShuttleConfig) { q := new(list.List) ticker := time.Tick(time.Minute) for { if q.Len() == 0 { q.PushFront(queued{when: time.Now(), batch: NewBatch(config)}) } e := q.Front() select { case batch := <-returnBatches: //I've been given a batch back, queue it stats <- NewNamedValue("batch.msg.age.range", batch.MsgAgeRange()) batch.Reset() q.PushFront(queued{when: time.Now(), batch: batch}) case getBatches <- e.Value.(queued).batch: //I've sent the current batch out, remove it from the queue q.Remove(e) case <-ticker: //Periodically go through the queued batches and throw //out ones that have been queued for too long in an effort //to expire old batches that were created because of bursts stats <- NewNamedValue("batch-manager.list.length", float64(q.Len())) removed := 0 for e := q.Front(); e != nil; e = e.Next() { age := time.Since(e.Value.(queued).when) if age > time.Minute { removed += 1 q.Remove(e) e.Value = nil } stats <- NewNamedValue("batch-manager.batch.queued.age", age.Seconds()) } stats <- NewNamedValue("batch-manager.list.removed", float64(removed)) } } }