github.com/searKing/golang/go@v1.2.117/net/tcp/struct.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 "time"
     8  
     9  func (srv *Server) initialReadLimitSize() int64 {
    10  	return int64(srv.maxBytes()) + 4096 // bufio slop
    11  }
    12  
    13  const DefaultMaxBytes = 1 << 20 // 1 MB
    14  func (srv *Server) maxBytes() int {
    15  	if srv.MaxBytes > 0 {
    16  		return srv.MaxBytes
    17  	}
    18  	return DefaultMaxBytes
    19  }
    20  func (srv *Server) idleTimeout() time.Duration {
    21  	if srv.IdleTimeout != 0 {
    22  		return srv.IdleTimeout
    23  	}
    24  	return srv.ReadTimeout
    25  }
    26  func (srv *Server) readTimeout() time.Duration {
    27  	if srv.ReadTimeout != 0 {
    28  		return srv.ReadTimeout
    29  	}
    30  	return srv.ReadTimeout
    31  }
    32  
    33  func (srv *Server) shuttingDown() bool {
    34  	return srv.inShutdown.Load()
    35  }
    36  func (srv *Server) getDoneChan() <-chan struct{} {
    37  	srv.mu.Lock()
    38  	defer srv.mu.Unlock()
    39  	return srv.getDoneChanLocked()
    40  }
    41  
    42  func (srv *Server) getDoneChanLocked() chan struct{} {
    43  	if srv.doneChan == nil {
    44  		srv.doneChan = make(chan struct{})
    45  	}
    46  	return srv.doneChan
    47  }
    48  
    49  func (srv *Server) closeDoneChanLocked() {
    50  	ch := srv.getDoneChanLocked()
    51  	select {
    52  	case <-ch:
    53  		// Already closed. Don't close again.
    54  	default:
    55  		// Safe to close here. We're the only closer, guarded
    56  		// by s.mu.
    57  		close(ch)
    58  	}
    59  }