// newSegment creates a new segment based on whether we're part of an existing flow
495498
func newSegment(name string, r *http.Request) *awsxray.Segment {
	// attempt to get IDs first
	parentId := getParentId(r.Header)
	traceId := getTraceId(r.Header)

	// now set the trace ID
	traceHdr := r.Header.Get(awsxray.TraceHeader)
	traceHdr = awsxray.SetTraceId(traceHdr, traceId)

	// create segment
	s := &awsxray.Segment{
		Id:        getRandom(8),
		HTTP:      newHTTP(r),
		Name:      name,
		TraceId:   traceId,
		StartTime: float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9,
	}

	// if we have a parent then we are a subsegment
	if len(parentId) > 0 {
		s.ParentId = parentId
		s.Type = "subsegment"
	} else {
		// set a new parent Id
		traceHdr = awsxray.SetParentId(traceHdr, s.Id)
	}

	// now save the header for the future context
	r.Header.Set(awsxray.TraceHeader, traceHdr)

	return s
}
1354951
func (s *GetPartitionsInput) SetSegment(v *Segment) *GetPartitionsInput {
	s.Segment = v
	return s
}
197895
func (s *sender) handleRcvdSegment(seg *segment) {
	// Check if we can extract an RTT measurement from this ack.
	if !seg.parsedOptions.TS && s.rttMeasureSeqNum.LessThan(seg.ackNumber) {
		s.updateRTO(time.Now().Sub(s.rttMeasureTime))
		s.rttMeasureSeqNum = s.sndNxt
	}

	// Update Timestamp if required. See RFC7323, section-4.3.
	if s.ep.sendTSOk && seg.parsedOptions.TS {
		s.ep.updateRecentTimestamp(seg.parsedOptions.TSVal, s.maxSentAck, seg.sequenceNumber)
	}

	// Insert SACKBlock information into our scoreboard.
	if s.ep.sackPermitted {
		for _, sb := range seg.parsedOptions.SACKBlocks {
			// Only insert the SACK block if the following holds
			// true:
			//  * SACK block acks data after the ack number in the
			//    current segment.
			//  * SACK block represents a sequence
			//    between sndUna and sndNxt (i.e. data that is
			//    currently unacked and in-flight).
			//  * SACK block that has not been SACKed already.
			//
			// NOTE: This check specifically excludes DSACK blocks
			// which have start/end before sndUna and are used to
			// indicate spurious retransmissions.
			if seg.ackNumber.LessThan(sb.Start) && s.sndUna.LessThan(sb.Start) && sb.End.LessThanEq(s.sndNxt) && !s.ep.scoreboard.IsSACKED(sb) {
				s.ep.scoreboard.Insert(sb)
				seg.hasNewSACKInfo = true
			}
		}
	}

	// Count the duplicates and do the fast retransmit if needed.
	rtx := s.checkDuplicateAck(seg)

	// Stash away the current window size.
	s.sndWnd = seg.window

	// Ignore ack if it doesn't acknowledge any new data.
	ack := seg.ackNumber
	if (ack - 1).InRange(s.sndUna, s.sndNxt) {
		s.dupAckCount = 0
		// When an ack is received we must reset the timer. We stop it
		// here and it will be restarted later if needed.
		s.resendTimer.disable()

		// See : https://tools.ietf.org/html/rfc1323#section-3.3.
		// Specifically we should only update the RTO using TSEcr if the
		// following condition holds:
		//
		//    A TSecr value received in a segment is used to update the
		//    averaged RTT measurement only if the segment acknowledges
		//    some new data, i.e., only if it advances the left edge of
		//    the send window.
		if s.ep.sendTSOk && seg.parsedOptions.TSEcr != 0 {
			// TSVal/Ecr values sent by Netstack are at a millisecond
			// granularity.
			elapsed := time.Duration(s.ep.timestamp()-seg.parsedOptions.TSEcr) * time.Millisecond
			s.updateRTO(elapsed)
		}
		// Remove all acknowledged data from the write list.
		acked := s.sndUna.Size(ack)
		s.sndUna = ack

		ackLeft := acked
		originalOutstanding := s.outstanding
		for ackLeft > 0 {
			// We use logicalLen here because we can have FIN
			// segments (which are always at the end of list) that
			// have no data, but do consume a sequence number.
			seg := s.writeList.Front()
			datalen := seg.logicalLen()

			if datalen > ackLeft {
				prevCount := s.pCount(seg)
				seg.data.TrimFront(int(ackLeft))
				seg.sequenceNumber.UpdateForward(ackLeft)
				s.outstanding -= prevCount - s.pCount(seg)
				break
			}

			if s.writeNext == seg {
				s.writeNext = seg.Next()
			}
			s.writeList.Remove(seg)
			s.outstanding -= s.pCount(seg)
			seg.decRef()
			ackLeft -= datalen
		}

		// Update the send buffer usage and notify potential waiters.
		s.ep.updateSndBufferUsage(int(acked))

		// Clear SACK information for all acked data.
		s.ep.scoreboard.Delete(s.sndUna)

		// If we are not in fast recovery then update the congestion
		// window based on the number of acknowledged packets.
		if !s.fr.active {
			s.cc.Update(originalOutstanding - s.outstanding)
		}

		// It is possible for s.outstanding to drop below zero if we get
		// a retransmit timeout, reset outstanding to zero but later
		// get an ack that cover previously sent data.
		if s.outstanding < 0 {
			s.outstanding = 0
		}
	}

	// Now that we've popped all acknowledged data from the retransmit
	// queue, retransmit if needed.
	if rtx {
		s.resendSegment()
	}

	// Send more data now that some of the pending data has been ack'd, or
	// that the window opened up, or the congestion window was inflated due
	// to a duplicate ack during fast recovery. This will also re-enable
	// the retransmit timer if needed.
	s.sendData()
}
1209303
func (t *seriesPartitionTracker) SetSegments(n uint64) {
	if !t.enabled {
		return
	}

	labels := t.Labels()
	t.metrics.Segments.With(labels).Set(float64(n))
}
1268537
func (s *CmafPackage) SetSegmentPrefix(v string) *CmafPackage {
	s.SegmentPrefix = &v
	return s
}
270501
func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error {
	if p.head == p.tail && p.count > 0 {
		return ErrPlaylistFull
	}
	seg.SeqId = p.SeqNo
	if p.count > 0 {
		seg.SeqId = p.Segments[(p.capacity+p.tail-1)%p.capacity].SeqId + 1
	}
	p.Segments[p.tail] = seg
	p.tail = (p.tail + 1) % p.capacity
	p.count++
	if p.TargetDuration < seg.Duration {
		p.TargetDuration = math.Ceil(seg.Duration)
	}
	p.buf.Reset()
	return nil
}
802074
func (f *File) NewSections(names ...string) (err error) {
	for _, name := range names {
		if _, err = f.NewSection(name); err != nil {
			return err
		}
	}
	return nil
}
1231328
func (w *IndexWriter) publishFlushedSegment(newSegment *SegmentCommitInfo,
	packet *FrozenBufferedUpdates, globalPacket *FrozenBufferedUpdates) (err error) {
	defer func() {
		atomic.AddInt32(&w.flushCount, 1)
		err = mergeError(err, w.doAfterFlush())
	}()

	// Lock order IW -> BDS
	w.Lock()
	defer w.Unlock()
	w.ClosingControl.ensureOpen(false)
	w.bufferedUpdatesStreamLock.Lock()
	defer w.bufferedUpdatesStreamLock.Unlock()

	if w.infoStream.IsEnabled("IW") {
		w.infoStream.Message("IW", "publishFlushedSegment")
	}

	if globalPacket != nil && globalPacket.any() {
		w.bufferedUpdatesStream.push(globalPacket)
	}
	// Publishing the segment must be synched on IW -> BDS to make sure
	// that no merge prunes away the seg. private delete packet
	var nextGen int64
	if packet != nil && packet.any() {
		nextGen = w.bufferedUpdatesStream.push(packet)
	} else {
		// Since we don't have a delete packet to apply we can get a new
		// generation right away
		nextGen = w.bufferedUpdatesStream.nextGen
	}
	if w.infoStream.IsEnabled("IW") {
		w.infoStream.Message("IW", "publish sets newSegment delGen=%v seg=%v", nextGen, w.readerPool.segmentToString(newSegment))
	}
	newSegment.SetBufferedUpdatesGen(nextGen)
	w.segmentInfos.Segments = append(w.segmentInfos.Segments, newSegment)
	return w._checkpoint()
}
127450
func NewSegmentWriteStateFrom(state *SegmentWriteState,
	segmentSuffix string) *SegmentWriteState {

	return &SegmentWriteState{
		state.infoStream,
		state.Directory,
		state.SegmentInfo,
		state.FieldInfos,
		state.DelCountOnFlush,
		state.SegUpdates,
		nil,
		segmentSuffix,
		state.termIndexInterval,
		state.Context,
	}
}
299954
func NewSegment(a, b vect.Vect, r vect.Float) *Shape {
	shape := newShape()
	seg := &SegmentShape{
		A:      a,
		B:      b,
		Radius: r,
		Shape:  shape,
	}
	shape.ShapeClass = seg
	return shape
}
222369
func NewStream(dataShards, parityShards int, o ...Option) (StreamEncoder, error) {
	enc, err := New(dataShards, parityShards, o...)
	if err != nil {
		return nil, err
	}
	rs := enc.(*reedSolomon)
	r := rsStream{r: rs, bs: 4 << 20}
	r.readShards = readShards
	r.writeShards = writeShards
	return &r, err
}
1244454
func NewSegmenterDirect(buf []byte) *Segmenter {
	return &Segmenter{
		segment:      SegmentWords,
		maxTokenSize: MaxScanTokenSize,
		buf:          buf,
		start:        0,
		end:          len(buf),
		err:          io.EOF,
	}
}
923051
func versionSegments(segments string) []int {
	segments = strings.Trim(segments, "v")
	segments = strings.Split(segments, "-")[0]
	s := strings.Split(segments, ".")
	segmentSlice := make([]int, len(s))
	for i, v := range s {
		segmentSlice[i], _ = strconv.Atoi(v)
	}
	return segmentSlice
}
674393
func NewSeqEnd(tags core.TagSlice) (*SeqEnd, error) {
	point := new(SeqEnd)

	point.InitBaseEntityParser()

	err := point.Parse(tags)
	return point, err
}
1277931
func (s *MssPackage) SetSegmentDurationSeconds(v int64) *MssPackage {
	s.SegmentDurationSeconds = &v
	return s
}
761087
func (p *parser) isHashSegment() bool {
	return p.have(2) && (p.next().Kind == lexer.TokenID) && (p.nextAt(1).Kind == lexer.TokenEquals)
}
54338
func (s *DashIsoGroupSettings) SetSegmentLength(v int64) *DashIsoGroupSettings {
	s.SegmentLength = &v
	return s
}