code: func OpenSegmentWAL(dir string, logger log.Logger, flushInterval time.Duration, r prometheus.Registerer) (*SegmentWAL, error) {
  	if err := os.MkdirAll(dir, 0777); err != nil {
  		return nil, err
  	}
  	df, err := fileutil.OpenDir(dir)
  	if err != nil {
  		return nil, err
  	}
  	if logger == nil {
  		logger = log.NewNopLogger()
  	}
  
  	w := &SegmentWAL{
  		dirFile:       df,
  		logger:        logger,
  		flushInterval: flushInterval,
  		donec:         make(chan struct{}),
  		stopc:         make(chan struct{}),
  		actorc:        make(chan func() error, 1),
  		segmentSize:   walSegmentSizeBytes,
  		crc32:         newCRC32(),
  	}
  	w.metrics = newWalMetrics(w, r)
  
  	fns, err := sequenceFiles(w.dirFile.Name())
  	if err != nil {
  		return nil, err
  	}
  
  	for i, fn := range fns {
  		f, err := w.openSegmentFile(fn)
  		if err == nil {
  			w.files = append(w.files, newSegmentFile(f))
  			continue
  		}
  		level.Warn(logger).Log("msg", "invalid segment file detected, truncating WAL", "err", err, "file", fn)
  
  		for _, fn := range fns[i:] {
  			if err := os.Remove(fn); err != nil {
  				return w, errors.Wrap(err, "removing segment failed")
  			}
  		}
  		break
  	}
  
  	go w.run(flushInterval)
  
  	return w, nil
  }
docs: // OpenSegmentWAL opens or creates a write ahead log in the given directory.
  // The WAL must be read completely before new data is written.