github.com/searKing/golang/go@v1.2.117/net/tcp/once.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package tcp
     6  
     7  import (
     8  	"net"
     9  	"sync"
    10  )
    11  
    12  // onceCloseListener wraps a net.Listener, protecting it from
    13  // multiple Close calls.
    14  type onceCloseListener struct {
    15  	net.Listener
    16  	once     sync.Once
    17  	closeErr error
    18  }
    19  
    20  func (oc *onceCloseListener) Close() error {
    21  	oc.once.Do(oc.close)
    22  	return oc.closeErr
    23  }
    24  
    25  func (oc *onceCloseListener) close() { oc.closeErr = oc.Listener.Close() }