package iroh

import (
	
	
	
	

	quic 
)

// maxAdmitting bounds the incoming connections an endpoint admits at once:
// those whose handshake or [EndpointHooks.AfterHandshake] hooks have not
// finished. Upstream Rust iroh has no such bound of its own; its QUIC stack
// allows 65536 incoming connections.
const maxAdmitting = 1024

// errAdmissionAbandoned is the cause of an admission abandoned to make room
// for a newer one.
var errAdmissionAbandoned = errors.New("iroh: handshake abandoned for a newer one")

// admissions tracks the incoming connections an endpoint is admitting, oldest
// first. Endpoint.Accept, StreamListener, and Router all admit through it.
//
// When full, admit abandons the oldest admission instead of waiting for one
// to finish: it cancels that admission's context and closes its connection.
// A peer that stalls its handshake, or is held in a hook, can then delay a
// new peer by nothing, however many such peers there are; the cost is that
// a flood of new handshakes can crowd out a slow legitimate one.
type admissions struct {
	mu   sync.Mutex
	max  int
	list list.List // of *admission
}

type admission struct {
	abandon   func() // closes the connection
	cancel    context.CancelCauseFunc
	elem      *list.Element // nil once removed
	abandoned bool
}

// admit registers qc and returns the context under which to finish its
// handshake and run its hooks, and a func to call once that is done. The
// func reports errAdmissionAbandoned if the admission was abandoned, in which
// case qc is already closed or being closed and the caller must not use the
// connection. It may be called more than once.
func ( *admissions) ( context.Context,  *quic.Conn) (context.Context, func() error) {
	return .add(, func() { .CloseWithError(0, "handshake abandoned") })
}

// add is admit with abandon in place of closing a connection.
func ( *admissions) ( context.Context,  func()) (context.Context, func() error) {
	,  := context.WithCancelCause()
	 := &admission{abandon: , cancel: }
	.mu.Lock()
	var  *admission
	if .list.Len() >= .max {
		 = .list.Remove(.list.Front()).(*admission)
		.elem = nil
		// Record the abandonment before unlocking: the oldest's finisher
		// may run before the cancel below and must see it.
		.abandoned = true
	}
	.elem = .list.PushBack()
	.mu.Unlock()
	if  != nil {
		.abandon()
		.cancel(errAdmissionAbandoned)
	}
	return , func() error {
		.mu.Lock()
		if .elem != nil {
			.list.Remove(.elem)
			.elem = nil
		}
		 := .abandoned
		.mu.Unlock()
		(nil)
		if  {
			return errAdmissionAbandoned
		}
		return nil
	}
}

// len returns the number of connections being admitted.
func ( *admissions) () int {
	.mu.Lock()
	defer .mu.Unlock()
	return .list.Len()
}