github.com/searKing/golang/go@v1.2.117/net/once_close_listener.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 net
     6  
     7  import (
     8  	"net"
     9  	"sync"
    10  )
    11  
    12  type onceCloseListener struct {
    13  	net.Listener
    14  	once     sync.Once
    15  	closeErr error
    16  }
    17  
    18  func (oc *onceCloseListener) Close() error {
    19  	oc.once.Do(oc.close)
    20  	return oc.closeErr
    21  }
    22  
    23  func (oc *onceCloseListener) close() {
    24  	oc.closeErr = oc.Listener.Close()
    25  }
    26  
    27  // OnceCloseListener wraps a net.Listener, protecting it from
    28  // multiple Close calls.
    29  func OnceCloseListener(l net.Listener) net.Listener {
    30  	return &onceCloseListener{Listener: l}
    31  }